简体   繁体   English

在Python列表中的每个列表的新行中打印列表

[英]Print list in new line for each list in list in Python

I have a list in list that look like this 我在清单中有一个看起来像这样的清单

[['a', 9] , ['b', 1] , ['c', 4] , . . ., ['z', 2]]

From here, I want to print out each list in the list line by line to look like this 从这里开始,我要逐行打印出列表中的每个列表,如下所示

['a',9]
['b',1]
. 
. 
['z',2]

I knew that I can just use loop to print it out like this: 我知道我可以使用循环将其打印出来,如下所示:

for i in list:
   print(i)

but my question here is that, can it be done without using loop? 但是我的问题是,不使用循环就可以完成吗? Like just a one line that print this without interacting with loop. 就像只打印一行而不与循环交互的一行。 (I plan to use with some alert function so I want it to be in one large message containing all list , not multiple message with one list each) (我计划与某些警报功能一起使用,因此我希望它位于包含所有列表的大消息中,而不是包含一个列表的多条消息中)

I have tried this: 我已经试过了:

print(list, sep='\n')

but it doesn't separate into one line per list. 但不会将每个列表分成一行。 I also tried this: 我也试过这个:

print('\n'.join(list))

and this error occured: 并发生此错误:

TypeError: sequence item 0: expected str instance, list found

which seem not to work with a list in list. 这似乎不适用于列表中的列表。 Any idea? 任何想法?

lst = [['a', 9] , ['b', 1] , ['c', 4]]

print(*lst, sep='\n')

Prints: 印刷品:

['a', 9]
['b', 1]
['c', 4]

You were almost there, just add some list comprehension: 您快到了,只需添加一些列表理解即可:

my_list = [['a', 9] , ['b', 1] , ['c', 4] , . . ., ['z', 2]]
print('\n'.join(str(el) for el in my_list ))

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

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