Python 如何检查一个单词是否出现在另一个单词(键)的ID(元素列表中的位置)中

Python 如何检查一个单词是否出现在另一个单词(键)的ID(元素列表中的位置)中,python,dictionary,Python,Dictionary,我有一份清单: lst = [" ","1- make your choice", "2- put something and make", "3- make something happens", "4- giulio took his choice so make","5- make your choice", "6- put something and make", "7- make something happens", "8- giulio took his choice so mak

我有一份清单:

lst = [" ","1- make your choice", "2- put something and make", "3- make something happens",
"4- giulio took his choice so make","5- make your choice", "6- put something and make",
"7- make something happens", "8- giulio took his choice so make","9- make your choice",
"10- put something and make", "11- make something happens", "12- giulio took his choice so make"]
我创建了两个字典,第一个是在lst中单词位置的ID(数字),在value中是该位置的单词

{1: ['make', 'your', 'choice'], 2: ['put', 'something', 'and', 'make'], 3: ['make', 'something', 'happens'], 4: ['giulio', 'took', 'his', 'choice', 'so', 'make'], 5: ['make', 'your', 'choice'], 6: ['put', 'something', 'and', 'make'], 7: ['make', 'something', 'happens'], 8: ['giulio', 'took', 'his', 'choice', 'so', 'make'], 9: ['make', 'your', 'choice'], 10: ['put', 'something', 'and', 'make'], 11: ['make', 'something', 'happens'], 12: ['giulio', 'took', 'his', 'choice', 'so', 'make']}

在第二个字典中,作为key,我有lst中的所有单词,作为value,我有两个set()

在第二组中,我将所有ID放在钥匙所在的位置,例如:

'choice': (set([]), set([1, 4, 5, 8, 9, 12]))

在第一组中,我想将所有单词同时放在所有关键字选择ID中,例如:

如果我们查看lst,我们可以看到在所有键选择ID中出现的唯一单词是“make”,因此键选择的结果是:

'choice': (set(['make']), set([1, 4, 5, 8, 9, 12]))
当然,除了“选择”这个词


关于如何查看一个单词是否出现在第二本词典关键字的同一ID中,有什么建议吗?然后将其放入第一组?

您可以迭代id,将所有列表展平为一个大列表,并检查每个单词的计数是否等于列表的数量

这是唯一一个可以用更便宜的流量完成的选择,但我不想让事情复杂化

解决方案

my_dict = {1: ['make', 'your', 'choice'], 2: ['put', 'something', 'and', 'make'], 3: ['make', 'something', 'happens'], 4: ['giulio', 'took', 'his', 'choice', 'so', 'make'], 5: ['make', 'your', 'choice'], 6: ['put', 'something', 'and', 'make'], 7: ['make', 'something', 'happens'], 8: ['giulio', 'took', 'his', 'choice', 'so', 'make'], 9: ['make', 'your', 'choice'], 10: ['put', 'something', 'and', 'make'], 11: ['make', 'something', 'happens'], 12: ['giulio', 'took', 'his', 'choice', 'so', 'make']}
words = {'and': (set([]), set([2, 10, 6])), 'happens': (set([]), set([11, 3, 7])), 'his': (set([]), set([8, 12, 4])), 'giulio': (set([]), set([8, 12, 4])), 'make': (set([]), set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])), 'took': (set([]), set([8, 12, 4])), 'choice': (set([]), set([1, 4, 5, 8, 9, 12])), 'so': (set([]), set([8, 12, 4])), 'something': (set([]), set([2, 3, 6, 7, 10, 11])), 'put': (set([]), set([2, 10, 6])), 'your': (set([]), set([1, 5, 9]))}

for k, v in words.items():
    flatten_list = [elem for id_ in v[1] for elem in my_dict[id_]]
    words[k][0].update(set([word for word in flatten_list if word != k if flatten_list.count(word) == len(v[1])]))

print words
{'and': (set(['put', 'make', 'something']), set([2, 10, 6])), 'his': (set(['make', 'so', 'giulio', 'took', 'choice']), set([8, 4, 12])), 'took': (set(['make', 'his', 'so', 'giulio', 'choice']), set([8, 4, 12])), 'choice': (set(['make']), set([1, 4, 5, 8, 9, 12])), 'something': (set(['make']), set([2, 3, 6, 7, 10, 11])), 'put': (set(['and', 'make', 'something']), set([2, 10, 6])), 'your': (set(['make', 'choice']), set([1, 5, 9])), 'happens': (set(['make', 'something']), set([3, 11, 7])), 'giulio': (set(['make', 'his', 'so', 'took', 'choice']), set([8, 4, 12])), 'make': (set([]), set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])), 'so': (set(['make', 'his', 'giulio', 'took', 'choice']), set([8, 4, 12]))}
输出

my_dict = {1: ['make', 'your', 'choice'], 2: ['put', 'something', 'and', 'make'], 3: ['make', 'something', 'happens'], 4: ['giulio', 'took', 'his', 'choice', 'so', 'make'], 5: ['make', 'your', 'choice'], 6: ['put', 'something', 'and', 'make'], 7: ['make', 'something', 'happens'], 8: ['giulio', 'took', 'his', 'choice', 'so', 'make'], 9: ['make', 'your', 'choice'], 10: ['put', 'something', 'and', 'make'], 11: ['make', 'something', 'happens'], 12: ['giulio', 'took', 'his', 'choice', 'so', 'make']}
words = {'and': (set([]), set([2, 10, 6])), 'happens': (set([]), set([11, 3, 7])), 'his': (set([]), set([8, 12, 4])), 'giulio': (set([]), set([8, 12, 4])), 'make': (set([]), set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])), 'took': (set([]), set([8, 12, 4])), 'choice': (set([]), set([1, 4, 5, 8, 9, 12])), 'so': (set([]), set([8, 12, 4])), 'something': (set([]), set([2, 3, 6, 7, 10, 11])), 'put': (set([]), set([2, 10, 6])), 'your': (set([]), set([1, 5, 9]))}

for k, v in words.items():
    flatten_list = [elem for id_ in v[1] for elem in my_dict[id_]]
    words[k][0].update(set([word for word in flatten_list if word != k if flatten_list.count(word) == len(v[1])]))

print words
{'and': (set(['put', 'make', 'something']), set([2, 10, 6])), 'his': (set(['make', 'so', 'giulio', 'took', 'choice']), set([8, 4, 12])), 'took': (set(['make', 'his', 'so', 'giulio', 'choice']), set([8, 4, 12])), 'choice': (set(['make']), set([1, 4, 5, 8, 9, 12])), 'something': (set(['make']), set([2, 3, 6, 7, 10, 11])), 'put': (set(['and', 'make', 'something']), set([2, 10, 6])), 'your': (set(['make', 'choice']), set([1, 5, 9])), 'happens': (set(['make', 'something']), set([3, 11, 7])), 'giulio': (set(['make', 'his', 'so', 'took', 'choice']), set([8, 4, 12])), 'make': (set([]), set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])), 'so': (set(['make', 'his', 'giulio', 'took', 'choice']), set([8, 4, 12]))}

如果在第一个列表中,而不是在列表中,你有集合,那会容易得多。