How can I remove the brackets and the commas from this List. The purpose of this code is to give an output for each time that the element is shifted
本问题已经有最佳答案,请猛点这里访问。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/bin/python def insertionSort(list): for i in xrange(1,len(list)): value=list[i] pos=i while pos>0 and value <list[pos-1]: list[pos]=list[pos-1] print list pos-=1 list[pos]=value return list m = input() list = [int(i) for i in raw_input().strip().split()] insertionSort(list) |
这是我的输出:
1 2 3 | [2, 4, 6, 8, 8] [2, 4, 6, 6, 8] [2, 4, 4, 6, 8] |
号
但是我需要相同的结果,但是没有括号和逗号。
1 2 3 4 | 2 4 6 8 8 2 4 6 6 8 2 4 4 6 8 2 3 4 6 8 |
我试着用
replace:
1 | print list |
一:
1 | print ' '.join(str(i) for i in list) |
或:
1 | print ' '.join('{:.0f}'.format(i) for i in list) |
或:
1 | print ' '.join('%i' % i for i in list) |
为区分,因为Python是一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/bin/python def insertionSort(list): for i in xrange(1,len(list)): value=list[i] pos=i while pos>0 and value <list[pos-1]: list[pos]=list[pos-1] list_ = map(str, list) print"".join(list_) pos-=1 list[pos]=value list_ = map(str, list) return"".join(list_) m = input() list = [int(i) for i in raw_input().strip().split()] print insertionSort(list) |
谢谢你的帮助,但我这样做的
你可以试试:
1 | print str(list).replace("[","").replace(",","").replace("]","") |
以