繁体   English   中英

从单个列表python打印

[英]Printing from a single list python

编辑:

 ['Station 5 average is less than 1.62644628099', 'Station 6 average is less than 1.62644628099', 'Station 7 average is less than 1.62644628099', 'Station 8 average is less than 1.62644628099', 'Station 9 average is less than 1.62644628099', 'Station 10 average is less than 1.62644628099']

['Station 1 average is greater than 1.62644628099', 'Station 2 average is greater than 1.62644628099', 'Station 3 average is greater than 1.62644628099']

有没有我可以用来产生结果的代码,使得打印时的输出看起来像这样

Station 5 average is less than 1.62644628099
Station 6 average is less than 1.62644628099
Station 7 average is less than 1.62644628099 
Station 8 average is less than 1.62644628099 
Station 9 average is less than 1.62644628099 
Station 10 average is less than 1.62644628099

Station 1 average is greater than 1.62644628099
Station 2 average is greater than 1.62644628099
Station 3 average is greater than 1.62644628099

感谢您的阅读和任何建议/帮助。

使用str.join()函数,该函数将列表中的每个元素与中间的str一起打印。 在这里,我在每个元素之间插入新行。

>>> lst1 = ['Station 5 average is less than 1.62644628099', 'Station 6 average is less than 1.62644628099', 'Station 7 average is less than 1.62644628099', 'Station 8 average is less than 1.62644628099', 'Station 9 average is less than 1.62644628099', 'Station 10 average is less than 1.62644628099']
>>> print '\n'.join(lst1)
Station 5 average is less than 1.62644628099
Station 6 average is less than 1.62644628099
Station 7 average is less than 1.62644628099
Station 8 average is less than 1.62644628099
Station 9 average is less than 1.62644628099
Station 10 average is less than 1.62644628099

第二个列表相同:

>>> lst2 = ['Station 1 average is greater than 1.62644628099', 'Station 2 average is greater than 1.62644628099', 'Station 3 average is greater than 1.62644628099']
>>> print '\n'.join(lst2)
Station 1 average is greater than 1.62644628099
Station 2 average is greater than 1.62644628099
Station 3 average is greater than 1.62644628099

或者,您可以使用for循环:

>>> for i in lst1:
...     print i
... 
Station 5 average is less than 1.62644628099
Station 6 average is less than 1.62644628099
Station 7 average is less than 1.62644628099
Station 8 average is less than 1.62644628099
Station 9 average is less than 1.62644628099
Station 10 average is less than 1.62644628099

>>> for i in lst2:
...     print i
... 
Station 1 average is greater than 1.62644628099
Station 2 average is greater than 1.62644628099
Station 3 average is greater than 1.62644628099

使用join()函数可能更可取,因为它实际上会返回结果,因此您可以根据需要在以后的代码中使用它。

用换行符加入列表成员。

print "\n".join(t)
print
print "\n".join(s)

暂无
暂无

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

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