如何将多个循环组合成一个循环?

时间:2013-08-19 08:18:28

标签: python python-2.7

这是我的代码

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

......等等..

3 个答案:

答案 0 :(得分:1)

添加外部循环:

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

答案 1 :(得分:1)

另一种选择是

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

itertools.product()为您提供给定迭代的所有可能组合。

如果您需要新讨论的print w,此解决方案无效。在这种情况下,这种解决方案是不合适的。

答案 2 :(得分: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)]

然后您可以使用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)])