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

时间:2015-10-28 13:11:41

标签: 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 make","9- make your choice",
"10- put something and make", "11- make something happens", "12- giulio took his choice so make"]

我创建了两个词典,首先我在lst中将词的位置ID(数字)作为关键词,并且在值中我将该词放在该位置。

{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中有所有单词,而作为值我有两个set()

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

在第二组中,我将所有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中的任何建议?把它放在第一组?

1 个答案:

答案 0 :(得分:1)

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

只有一个选项可以用较便宜的流程来完成,但我并不想让事情复杂化。

<强>解决方案

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]))}
相关问题