简体   繁体   English

如何在python中自动打印消息

[英]How to automate printing messages in python

How do I print, separated by tabs, the values of all the strings in a list without prior knowledge of the number of strings in a list? 如何在不事先知道列表中字符串数的情况下,用制表符分隔列表中所有字符串的值的打印方式?

If I knew that the list has just 3 elements, I could do something like this: 如果我知道列表仅包含3个元素,则可以执行以下操作:

l = ['A', 'B', 'C']
print "\t".join([l[0], l[1], l[2]])

And I would obtain 我会得到

A       B       C

But I have no idea of how to do it for n strings in a automated way 但是我不知道如何自动处理n字符串

l = ['A','B','C']

joined_string = "\\t".join(l)

print joined_string

l = ['A', 'B', 'C']
print "\t".join(l)

Now just in case your lists contains several lists within them: 现在,以防万一您的列表中包含多个列表:

import itertools

l = ['A', 'B', 'C', ['d', 'D']]
foo = [item for item in itertools.chain(*l)]
print "\t".join(foo)

Try: 尝试:

>>> l = ['A', 'B', 'C']
>>> foo = "\t".join(l)
>>> print foo

Will output: 将输出:

A   B   C

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

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