Skip to main content
 首页 » 编程设计

python之从单个列表python打印

2025年05月04日16zdz8207

编辑:

 ['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() 函数可能更可取,因为它实际上会返回结果,因此您可以根据需要稍后在代码中使用它。