【发布时间】:2017-07-24 22:40:56
【问题描述】:
我无法弄清楚这背后的逻辑。
1.) 首先,我从该文本文件中读取了一个去掉标点符号和空格的文本文件:
The sun is bright & the moon glows.
The dog barks while the cat meows.
My dog is dark, dark as crows.
2.) 读完这个文本文件后,我假设有一个字典,其中一个单词作为键,NEXT 单词作为值,如下所示:
{'the':['sun','moon','dog','cat'], 'sun':['is'], 'is':['bright','dark'], 'moon':['glows'],'glows':['the'],
'dog':['barks','is'], 'barks':['while'],'while':['the'], 'cat':['meows'],'meows':['my'], 'my':['dog'],
'dark':['dark','as'], 'as':['crows'],
'bright':['the'], 'crows':[]}
最后两项是特殊情况。 "crows" 有一个空列表,因为它是文本文件中的最后一个单词。
我不确定这背后的逻辑,但我似乎无法理解这一点。
我的第一种方法是创建一个包含所有单词的巨大列表,然后从列表中挑选和拉取以形成几个较小的列表。
【问题讨论】:
标签: python list file dictionary