gpt4 book ai didi

python - 将理解列表打印为字符串

转载 作者:太空宇宙 更新时间:2023-11-04 08:24:15 25 4
gpt4 key购买 nike

假设我有一个理解列表:

list1 = [("hello","my name"),("is","hayhay")]

我如何使用循环将其打印出来?我试过做

for line in list1:
print(line);

但它不起作用。

最佳答案

如果你想打印每个元组:

for i in list1:
print(i)

('hello', 'my name')
('is', 'hayhay')

如果要打印为一系列字符串:

for i in list1:
for l in i:
print(l)

hello
my name
is
hayhay

如果要打印成统一字符串:

list2 = ""
for i in list1:
for l in i:
list2 += l+" "
print(list2)

hello my name is hayhay

关于python - 将理解列表打印为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58922179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com