慕标琳琳
import itertoolswith open("temp.txt", "r") as f: words = [item.split(' ')[-1].strip() for item in f]pairs = list(itertools.permutations(words, 2))print(pairs)印刷品(pprint用于提高可读性):[('the', 'of'), ('the', 'to'), ('the', 'and'), ('the', 'bank'), ('of', 'the'), ('of', 'to'), ('of', 'and'), ('of', 'bank'), ('to', 'the'), ('to', 'of'), ('to', 'and'), ('to', 'bank'), ('and', 'the'), ('and', 'of'), ('and', 'to'), ('and', 'bank'), ('bank', 'the'), ('bank', 'of'), ('bank', 'to'), ('bank', 'and')]
跃然一笑
一些改进的解释import itertoolswith open('temp.txt', 'r') as fobj_in, open('out.txt', 'w') as fobj_out: words = (item.split()[-1] for item in fobj_in if item.strip()) for pair in itertools.permutations(words, 2): fobj_out.write('{} {}\n'.format(*pair))解释with open('temp.txt', 'r') as fobj_in, open('out.txt', 'w') as fobj_out:我们打开两个文件,一个用于读取,一个在的帮助下编写with。这保证了with即使我们在该块的某个位置出现异常,只要我们离开该块的缩进,这两个文件都将被关闭。我们使用列表理解来获取所有单词:words = [item.split()[-1] for item in fobj_in if item.strip()]item.split()[-1]删除任何空格,并为我们提供行中的最后一个条目。请注意,它也在\n每行的结尾处取下。不需要.strip()这里。item.split()通常比item.split(' ')它更好,因为它也可以在多个空间和制表符中使用。我们仍然需要确保该行不是空的if item.strip()。如果删除所有空格后什么也没留下,那么我们item.split()[-1]就没有话语了,并且会给出和索引错误。只需转到下一行并丢弃该行即可。现在我们可以遍历所有对,并将它们写入输出文件:for pair in itertools.permutations(words, 2): fobj_out.write('{} {}\n'.format(*pair))我们要求迭代器一次给我们下一个单词对一对,然后将此对写入输出文件。无需将其转换为列表。将其中.format(*pair)的两个元素解包pair,.format(pair[0], pair[1])对于与我们两个元素对应的那对等价。业绩说明第一种直觉可能是也使用生成器表达式从文件中读取单词:words = (item.split()[-1] for item in fobj_in if item.strip())但是时间测量表明,列表理解比生成器表达式要快。这是由于无论如何都要itertools.permutations(words)消耗迭代器words。首先创建一个列表,避免了再次遍历所有元素的工作。