简体   繁体   English

Python在打印元组列表时如何删除括号?

[英]Python how to remove brackets when printing a list of tuples?

How do I remove the brackets from this tuple output: [("fred's", 13), ("jack's", 19), ("mark's", 16), ("amy's", 12), ("finlay's", 17)]. 如何从此元组输出中删除括号:[(“ fred's,13),(” jack's,19),(“ mark's,16),(” amy's,12),(“ finlay's,17 )]。

This is the code I used to output the tuple: 这是我用来输出元组的代码:

    file_path = "test.txt"
with open(file_path, 'r') as f:
    file_lines = f.readlines() 
names_and_scores = [(l.strip().split(' ')[0], int(l.strip().split(' ')[2])) for l in file_lines]
names_and_scores.sort(key=lambda x: x[1], reverse=True)
print(names_and_scores[:5])

Maybe something like this? 也许是这样的吗?

print ("".join(str(names_and_scores[:5])).strip("()"))

names_and_scores[:5] is a list of tuples, which is why you see the brackets around the output. names_and_scores[:5]是元组的列表,这就是为什么您在输出周围看到方括号的原因。 To remove the brackets you can convert the list to a string by comma separating all of the individual tuple elements in the list: 要删除括号,您可以通过逗号分隔列表中的所有单个元组元素来将列表转换为字符串:

", ".join(names_and_scores[:5])

You're kind of misunderstanding. 你有点误会 In general, you can never remove characters from a tuple. 通常,您永远不能从元组中删除字符。 Since they are immutable, you can only create a new one and build it using the desired elements of the original. 由于它们是不可变的,因此您只能创建一个新的,并使用原始的所需元素来构建它。 The reason there are square brackets is because the output of your "names and scores" was created using list comprehension. 使用方括号的原因是,您的“姓名和分数”的输出是使用列表推导创建的。 That is to say, it's a list. 也就是说,这是一个列表。 The square brackets are there to tell you it's a list, so you can't "remove them". 方括号告诉您这是一个列表,因此您不能“删除它们”。 If you want to see each tuple without the square brackets you can iterate through the list of tuples and print each tuple. 如果要查看每个不带方括号的元组,可以遍历元组列表并打印每个元组。 That way you'd get something like: 这样一来,您将获得以下信息:

("Fred's", 13)
("Jack's", 12)

If you want to remove the brackets round and square, you can refer to the tuple with indexes for the values. 如果要删除圆括号方括号,则可以使用值的索引引用元组。 You can do: 你可以做:

print("PLAYER:\t\tSCORE:")
for i in names_and_scores[:5]:
    print("{}\t\t{}".format(i[0],i[1]))

This is going to iterate through the desired portion of your list (up till the fifth, it seems) and then print something like... 这将遍历列表的所需部分(似乎一直排到第五位),然后打印类似...

PLAYER:    SCORE:
Jack's     12
Fred's     13

You can ignore the 's with... 您可以使用...忽略的

print("PLAYER:\t\tSCORE:")
    for i in names_and_scores[:5]:
        print("{}\t\t{}".format(i[0][:-2],i[1]))

Here's my code and output: 这是我的代码和输出:

list = [("fred's", 13), ("jack's", 19), ("mark's", 16), ("amy's", 12), ("finlay's", 17)]
print("PLAYER:\t\tSCORE")
for i in list:
        print("{}\t\t{}".format(i[0][:-2],i[1]))



PLAYER:         SCORE
fred            13
jack            19
mark            16
amy             12
finlay          17

The easiest way that i can think of is to iterate through the list 我能想到的最简单的方法是遍历列表

list=[("fred's", 13), ("jack's", 19), ("mark's", 16), ("amy's", 12), ("finlay's", 17)]
for item in list:
    name,number=item
    print(name,number)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM