2013-08-19 150 views
2

這是我的代碼如何將多個循環合併爲一個循環?

word = ["General William Shelton, said the system", 
     "which will provide more precise positional data", 
     "and that newer technology will provide more", 
     "Commander of the Air Force Space Command", 
     "objects and would become the most accurate metadata"] 

matched_word = ["will", "and", "in", "the", "a", "A"] 

我曾嘗試這樣的代碼:

print word[0] 
for item in matched_word: 
     print item,":",word[0].count(item) 

print word[1] 
for item in matched_word: 
     print item,":",word[1].count(item) 

print word[2] 
for item in matched_word: 
     print item,":",word[2].count(item) 

print word[3] 
for item in matched_word: 
     print item,":",word[3].count(item) 

我得到我想要的輸出,但我不如何創建一個循環這一切的循環。 謝謝

輸出應該是這樣的:

General William Shelton, said the system 
will : 0 
and : 0 
in : 0 
the :1 
a : 3 
A : 0 
which will provide more precise positional data 
will : ! 
and : 0 
in : 0 
the : 0 
a : 3 
A : 0 

...等..

回答

1

添加循環:

for w in word: 
    print w 
    for item in matched_word: 
     print item, ":" ,w.count(item) 
+0

這將打印出項目和數量。每個陣列呢? –

+0

我不確定你在這裏問什麼。 –

+0

輸出shuold是這樣的: 威廉謝爾頓表示,該系統 將:0 和:0 的:1 一個:3 一個:在0 這將提供更精確的位置數據 將:1 和:在0 :0 的:0 一個:3 A:0 和較新的技術將提供更多 將:1 和:0 的::0 一個:2合1 A:0空軍太空司令部 的 指揮官將:0 和:0 的:1 一:3 答:1 將:0 和:0 中:0 的:0 一個在2 :3 答:0 –

1

另一種做法be

import itertools 
for w, item in itertools.product(word, matched_word): 
    print item, ":", w.count(item) 

itertools.product()給出了給定迭代的所有可能組合。

如果您需要新討論的print w,此解決方案無法正常工作。在這種情況下,這種解決方案是不合適的。

1

嘗試嵌套列表理解

counts = [(w, i, w.count(i)) for w in word for i in matched_word] 

你會採取這樣的

[('General William Shelton, said the system', 'will', 0), 
('General William Shelton, said the system', 'and', 0), 
('General William Shelton, said the system', 'in', 0), 
('General William Shelton, said the system', 'the', 1), 
('General William Shelton, said the system', 'a', 3), 
('General William Shelton, said the system', 'A', 0), 
('which will provide more precise positional data', 'will', 1), 
('which will provide more precise positional data', 'and', 0), 
('which will provide more precise positional data', 'in', 0), 
('which will provide more precise positional data', 'the', 0), 
('which will provide more precise positional data', 'a', 3), 
('which will provide more precise positional data', 'A', 0), 
('and that newer technology will provide more', 'will', 1), 
('and that newer technology will provide more', 'and', 1), 
('and that newer technology will provide more', 'in', 0), 
('and that newer technology will provide more', 'the', 0), 
('and that newer technology will provide more', 'a', 2), 
('and that newer technology will provide more', 'A', 0), 
('Commander of the Air Force Space Command', 'will', 0), 
('Commander of the Air Force Space Command', 'and', 2), 
('Commander of the Air Force Space Command', 'in', 0), 
('Commander of the Air Force Space Command', 'the', 1), 
('Commander of the Air Force Space Command', 'a', 3), 
('Commander of the Air Force Space Command', 'A', 1), 
('objects and would become the most accurate metadata', 'will', 0), 
('objects and would become the most accurate metadata', 'and', 1), 
('objects and would become the most accurate metadata', 'in', 0), 
('objects and would become the most accurate metadata', 'the', 1), 
('objects and would become the most accurate metadata', 'a', 6), 
('objects and would become the most accurate metadata', 'A', 0)] 

數組那麼你可以使用groupbyitertools

groupped = groupby(counts, lambda i: i[0]) 

最後

for category, items in groupped: 
    print category, '\n', "\n".join([":".join(map(str, j[1:])) for j in list(items)])