如何从列表中创建单词对列表

我在文件“ temp”中有一个单词列表:


 1. the 

 2. of

 3. to

 4. and

 5. bank

等等


我如何提高其可读性?


import itertools

f = open("temp.txt","r")

lines = f.readlines()

pairs = list(itertools.permutations(lines, 2))

print(pairs)

我迷路了,请帮忙。


慕容708150
浏览 172回答 3
3回答

湖上湖

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

慕标琳琳

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。首先创建一个列表,避免了再次遍历所有元素的工作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python