Python 从单个列表打印

Python 从单个列表打印,python,list,printing,Python,List,Printing,编辑: 及 是否有一个代码可以用来生成这样的结果,即打印时的输出如下所示 ['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 1 average is greater

编辑:

是否有一个代码可以用来生成这样的结果,即打印时的输出如下所示

['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 1 average is greater than 1.62644628099
Station 2 average is greater than 1.62644628099
Station 3 average is greater 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介于两者之间。在这里,我在每个元素之间插入新行

print "\n".join(t)
print
print "\n".join(s)
第二个列表也一样:

>>> 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
或者,您可以使用for循环:

>>> 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
可能最好使用join函数,因为它实际上返回结果,因此如果您愿意,可以在以后的代码中使用它。

使用该函数,该函数将打印列表中的每个元素,其中str介于两者之间。在这里,我在每个元素之间插入新行

print "\n".join(t)
print
print "\n".join(s)
第二个列表也一样:

>>> 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
或者,您可以使用for循环:

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

可能最好使用join函数,因为它实际上会返回结果,因此您可以在以后的代码中使用它(如果您愿意)。

不要忘记:不要忘记:+1,但是for循环更可取,因为您不必同时将整个输出的副本加载到内存中once@jamylak,列表已在内存中。此外,str.join接受任何iterable,因此可以与生成器表达式一起愉快地工作。@Johnsyweb因此您不必养成再次复制它或不必要地复制任何其他数据的习惯structures@Johnsyweb还有,str.join从list1Oh创建一个新字符串!我懂了!很抱歉,我完全误解了您的评论。+1但是for循环更可取,因为您不必同时将整个输出的副本加载到内存中once@jamylak,列表已在内存中。此外,str.join接受任何iterable,因此可以与生成器表达式一起愉快地工作。@Johnsyweb因此您不必养成再次复制它或不必要地复制任何其他数据的习惯structures@Johnsyweb还有,str.join从list1Oh创建一个新字符串!我懂了!抱歉,我完全误解了你的评论。