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