简体   繁体   English

集字典的交集

[英]Intersection of dictionary of sets

So I have a dictionary that was made from reading a file and making a key for each word found in that file with the value being a set of line numbers that the word occurs on. 因此,我有一个字典,该字典是通过读取文件并为该文件中找到的每个单词制作一个关键字而制成的,其值是该单词所在的一组行号。 Here is an example of the dictionary from a file. 这是文件中字典的示例。

{'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}}

What I need to do is take user entered space separated words (that I made into a list) and search the dictionary for the intersection of lines that they are all on. 我需要做的是将用户输入的用空格分隔的单词(我做成一个列表),然后在字典中搜索它们全部位于的线的交点。 For example if the user enters "the" then it would return 3, 4, 7 and if they entered "the is" would return 3, 7. 例如,如果用户输入“ the”,则它将返回3、4、7;如果用户输入“ the”,则将返回3、7。

Here is what I have come up with so far just trying to get it to work for 1 word: 到目前为止,我想出的只是想让它工作一个字而已:

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() 

And everything I try keeps returning None. 而且,我尝试的所有操作始终返回None。

Let's assume uinput is the list of user-entered words and D is your dictionary, eg: 假设uinput是用户输入的单词的列表, D是您的字典,例如:

uinput = "the is".split()

Then you can go over the uinput , use each word as a dictionary key, fetch its value, and finally take the intersection, exactly as your question's title suggests: 然后,您可以遍历uinput ,将每个单词用作字典键,获取其值,最后取交集, uinput您的问题标题所建议的那样:

set.intersection(*[D[x] for x in uinput if x in D])
#{3, 7}

this is the problem : 这就是问题 :

 if word not in D: 
    word_set = D[word]

should be 应该

if word in D: 
    word_set = D[word]

I think the problem is the line if word not in D: . 我认为问题在于if word not in D:则该行。 In that line, you make sure any input that is in D is deferred to the else , thereby returning None (I'm assuming this all takes place within a function, as that's the only place where return statements make sense). 在那行,你要确保任何输入的情况下在D被推迟到else ,从而返回None (我假设这一切都需要一个函数内的地方,因为这是其中唯一的地方return语句意义)。 Changing it to if word in D: should allow you to continue debugging. 将其更改为if word in D:应该可以继续进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM