所以我有一个字典,它是通过读取文件并为该文件中找到的每个单词创建一个键,其值是该单词出现的一组行号。以下是文件中字典的示例。
{'we': {4}, 'created': {4}, 'into': {2}, 'cant': {6}, 'imagination': {3}, 'with': {4}, 'nature': {2}, 'genius': {7}, 'gravity': {6}, 'of': {1, 3, 5}, 'rather': {1}, 'has': {7}, 'difference': {7}, 'try': {1}, 'better': {2}, 'used': {4}, 'value': {1}, 'between': {7}, 'blame': {6}, 'problems': {4}, 'is': {3, 7}, 'everything': {2}, 'not': {1, 3}, 'to': {1}, 'intelligence': {3}, 'thinking': {4}, 'them': {4}, 'deep': {2}, 'become': {1}, 'falling': {6}, 'for': {6}, 'character': {5}, 'when': {4}, 'will': {2}, 'solve': {4}, 'limits': {7}, 'same': {4}, 'weakness': {5}, 'and': {2, 7}, 'but': {1, 3}, 'love': {6}, 'knowledge': {3}, 'understand': {2}, 'then': {2}, 'man': {1}, 'our': {4}, 'attitude': {5}, 'in': {6}, 'the': {3, 4, 7}, 'that': {7}, 'sign': {3}, 'look': {2}, 'stupidity': {7}, 'cannot': {4}, 'its': {7}, 'true': {3}, 'success': {1}, 'becomes': {5}, 'you': {2, 6}}
我需要做的是将用户输入的空格分隔的单词(我制作成一个列表)并在字典中搜索它们全部在的行的交集。例如,如果用户输入“the”,那么它将返回3,4,7,如果他们输入“is is”将返回3,7。
到目前为止,我已经提出了这个问题,试图让它用于1个单词:
inp_lst = inp_str.strip().split()
print("The co-occurance for: " + ", ".join(inp_lst))
for word in inp_lst:
word = word.strip().strip(string.punctuation).lower()\
.replace("'","").replace("-","")
if word in D:
word_set = D[word]
else:
return None
cooccurance_lst = list(word_set)
return cooccurance_lst.sort()
我尝试的一切都不会返回无。
答案 0 :(得分:4)
我们假设uinput
是用户输入的单词列表,D
是您的字典,例如:
uinput = "the is".split()
然后你可以翻看uinput
,将每个单词用作字典键,获取其值,最后选择交集,就像问题标题所示:
set.intersection(*[D[x] for x in uinput if x in D])
#{3, 7}
答案 1 :(得分:0)
这就是问题:
if word not in D:
word_set = D[word]
应该是
if word in D:
word_set = D[word]
答案 2 :(得分:0)
我认为问题是行if word not in D:
。在该行中,您确保D
中 的任何输入都被推迟到else
,从而返回None
(我假设这一切都发生了在函数中,因为这是return
语句有意义的唯一地方。将其更改为if word in D:
应该允许您继续调试。