简体   繁体   English

从单个列表python打印

[英]Printing from a single list python

EDIT: 编辑:

 ['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']

and

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

Is there a code that I can use to produce a result such that the output when printed looks like this 有没有我可以用来产生结果的代码,使得打印时的输出看起来像这样

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

and

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

Thanks for reading and any advice/help you can give. 感谢您的阅读和任何建议/帮助。

Use the str.join() function, which prints each element in a list with the str in between. 使用str.join()函数,该函数将列表中的每个元素与中间的str一起打印。 Here I insert a new line between each element. 在这里,我在每个元素之间插入新行。

>>> 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

Same for the second list: 第二个列表相同:

>>> 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

Or, you can use a for-loop: 或者,您可以使用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

It's probably preferable to use the join() function, as it actually returns the result, so you can use it later in your code if you wish. 使用join()函数可能更可取,因为它实际上会返回结果,因此您可以根据需要在以后的代码中使用它。

Join the list members with the newline character. 用换行符加入列表成员。

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

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

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