首页 > 解决方案 > 如何在python中打印没有方括号的列表

问题描述

我有一个外部 .csv 文件,我将其读入二维列表,对其进行排序,然后打印。打印时我不想要方括号,我该如何摆脱它们?

def bubbleSort(hi):
    for passnum in range(len(hi)-1,0,-1):
        for i in range(passnum):
            if hi[i]>hi[i+1]:
                temp = hi[i]
                hi[i] = hi[i+1]
                hi[i+1] = temp

hi =[]

with open('winners.csv', 'r') as textfile:
    for row in reversed(list(csv.reader(textfile))):

        #create the file into an array

        hi.append(row)

#initiate the bubble sort on the array 

bubbleSort(hi)

# print the top 5 winners

for i in range(5):
    print(hi[i])

给出输出:

[score, name]
[score, name]
[score, name]
[score, name]
[score, name]

如果可能的话,我只想去掉括号。所以它看起来像:

score, name
score, name
score, name
score, name
score, name

标签: pythonlist

解决方案


如果这只是print代码完成后的内容,您有几个选择。

lista = [1, 2, 3]

print(' '.join([str(i) for i in lista]))
print(*lista)
for i in lista:
    print(i, end = ' ')
1 2 3

推荐阅读