我正在制作一个字典,其中键是一个txt文件中两个连续单词的元组,每个键的值是直接在键后面找到的单词列表。例如
>>> with open('alice.txt') as f:
... d = associated_words(f)
>>> d[('among', 'the')]
>>> ['people', 'party.', 'trees,', 'distant', 'leaves,', 'trees', 'branches,', 'bright']
到目前为止,我的代码如下,但它还没有完成。有人能帮忙吗?你知道吗
def associated_words(f):
from collections import defaultdict
d = defaultdict(list)
with open('alice.txt', 'r') as f:
lines = f.read().replace('\n', '')
a, b, c = [], [], []
lines.replace(",", "").replace(".", "")
lines = line.split(" ")
for (i, word) in enumerate(lines):
d['something to replace'].append(lines[i+2])
假设你的文件看起来像这样
代码:
像这样的?(应该很容易适应……)
输出(摘录…;不在全文上运行)
相关问题 更多 >
编程相关推荐