2013-05-29 54 views
1

編輯:印刷蟒

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

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 

感謝您的閱讀和任何建議/幫助您可以給。

+3

不要忘記[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work):) – TerryA

回答

1

使用換行符加入列表成員。

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

使用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()函數,因爲它實際上會返回結果,因此如果您願意,可以稍後在代碼中使用它。

+2

+1,但'for'循環是最好的,因爲你不必一次性加載整個輸出到內存的副本 – jamylak

+0

@jamylak,該列表已經在內存中。此外,'str.join()'接受任何* iterable *,因此可以像使用生成器表達式一樣快樂地工作。 – Johnsyweb

+0

@Johnsyweb所以你不需要養成複製它的習慣,或者對任何其他數據結構進行非常規的複製 – jamylak