2012-02-28 38 views
1

我試圖在python中打印一個簡單的表,但以前的答案都不是我正在尋找的東西。任何和所有的幫助將不勝感激。Python中的打印表

我有(的文本)的列表:

texts = [caesar,hamlet,macbeth,emma,persuasion,sense] 

我然後運行一個叫做 '相似性',其比較2個文本功能。我想在表格中打印結果,但在print語句遍歷列表的一個循環後,我似乎無法獲得新行。 [注:我使用Python 2.6.6,因爲我使用的自然語言工具包,用於語言學一個Python模塊]

這裏是我當前的打印語句,它不能正常工作:

for x in texts: 
    for y in texts: 
     print round(similarity(x,y),2), 
     if texts.index(x) == len(texts): 
      print '\n' 

任何指向正確方向的指針都會很棒!

回答

1

只需使用print即可。

另外,你爲什麼不這樣做?

for x in texts: 
    for y in texts: 
     print round(similarity(x,y),2), 
    print 
1

只需移動打印新行至外環:

for x in texts: 
    for y in texts: 
     print "{:8.2f}".format(similarity(x,y)), 
    print 

print語句將打印一個換行符。另請注意,round()不適用於字符串格式化 - 請改爲使用str.format()

+0

非常感謝。對此,我真的非常感激!我使用round(),因爲我的similarity()函數輸出一個浮點數。 – 2012-02-28 14:48:06

+0

@Adam_G:我知道你爲什麼使用'round()',但如上所述,'round()'並不意味着用於輸出格式。有關輸出格式的更多信息,請參閱Python教程中的[Fancier輸出格式化]一節(http://docs.python.org/tutorial/inputoutput.html#fancier-output-formatting),並參見[浮點運算:問題和侷限性](http://docs.python.org/tutorial/floatingpoint.html)爲什麼使用'round()'來達到這個目的是個壞主意。 – 2012-02-28 14:55:46

2

在一個列表中,在每個項目(另一個或同一個)列表進行比較的每個項目的過程在數學上被稱爲Cartesian product。 Python有一個內置函數來做到這一點:itertools.product這相當於嵌套的for循環:

假設A和B是列表:

for x in A: 
    for y in B: 
     print (x,y) 

可以寫成一個generator expression爲:

for pair in ((x,y) for x in A for y in B): 
    print pair 

,或者更簡潔:

from itertools import product 
for pair in product(A, B): 
    print pair 

在你的情況你將列表中的所有項目與自身進行比較,因此您可以編寫product(texts, texts),但產品在此情況下具有可選的關鍵字參數repeatproduct(A, repeat=4)的含義與product(A, A, A, A)相同。

您現在可以重寫代碼是這樣的:

from itertools import product 

caesar = """BOOK I 
I.--All Gaul is divided into three parts, one of which the Belgae 
inhabit, the Aquitani another, those who in their own language are 
called Celts, in ours Gauls, the third. All these differ from each other 
in language, customs and laws.""" 

hamlet = """Who's there?" 
"Nay, answer me. Stand and unfold yourself." 
"Long live the King!" 
"Barnardo!" 
"He." (I.i.1-5)""" 

macbeth = """ACT I SCENE I  A desert place. Thunder and lightning. 
[Thunder and lightning. Enter three Witches] 
First Witch When shall we three meet again 
In thunder, lightning, or in rain? 
Second Witch When the hurlyburly's done, 
When the battle's lost and won.""" 

texts = [caesar, hamlet, macbeth] 

def similarity(x, y): 
    """similarity based on length of the text, 
    substitute with similarity function from Natural Language Toolkit""" 
    return float(len(x))/len(y) 


for pair in product(texts, repeat=2): 
    print "{}".format(similarity(*pair))