3 回答

TA贡献2003条经验 获得超2个赞
我假设您的问题是创建temp文件中定义的所有可能的单词对。这称为置换,您已经在使用该itertools.permutations函数
如果需要将输出实际写入文件,则代码应为以下内容:
代码:
import itertools
f = open("temp","r")
lines = [line.split(' ')[-1].strip() for line in f] #1
pairs = list(itertools.permutations(lines, 2)) #2
r = open('result', 'w') #3
r.write("\n".join([" ".join(p) for p in pairs])) #4
r.close() #5
该
[line.split(' ')[-1].strip() for line in f]
会读取整个文件,并为每个readed线,它会分裂它周围的空格字符,选择该行的最后一个项目(负指标就像-1
在列表中向后行走),删除任何尾随空格(像\n
),并把所有的一个列表中的行像您已经做过的那样生成对,但是现在它们没有拖尾了
\n
打开
result
文件进行写入将用空格(
" "
)分隔的对连接起来,用a将每个结果(一行)连接起来\n
,然后写入文件关闭文件(因此刷新它)

TA贡献1830条经验 获得超9个赞
import itertools
with 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')]

TA贡献1826条经验 获得超6个赞
一些改进的解释
import itertools
with 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。首先创建一个列表,避免了再次遍历所有元素的工作。
添加回答
举报