[英]how to create a list of word pairs from a list
我在文件“ 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)
我迷路了,請幫忙。
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')]
我假設您的問題是創建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]
的[line.split(' ')[-1].strip() for line in f]
將讀取整個文件,並且對於讀取的每一行,它將在空格字符周圍分割它,選擇該行的最后一項(負索引(如-1
)在列表中向后移動),刪除所有尾隨空格(如\\n
),並將所有行放在一個列表中 \\n
result
文件進行寫入 " "
)分隔的行對,將每個結果(一行)與\\n
,然后寫入文件 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
。 首先創建列表可以避免再次遍歷所有元素的工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.