嘗試嵌套列表理解
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)]
數組那麼你可以使用groupby
從itertools
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)])
這將打印出項目和數量。每個陣列呢? –
我不確定你在這裏問什麼。 –
輸出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 –