【问题标题】:How do I print a list without square brackets in python [duplicate]如何在python中打印没有方括号的列表[重复]
【发布时间】:2018-10-05 14:32:00
【问题描述】:

我有一个外部 .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

【问题讨论】:

  • " ".join(hi[i])
  • 您打印列表的目的是什么?对于最终用户?然后打印列表的内容,而不是列表本身。

标签: python list


【解决方案1】:

如果您的代码完成后只是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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多