在python中查询集合的字典

时间:2018-10-19 21:34:35

标签: python

所以我试图获取列表中每个单词的位置,并将其打印在字典中,该字典中包含key关键字和一组整数,该关键字属于列表。

list_x = ["this is the first", "this is the second"]
my_dict = {}
for i in range(len(list_x)):
    for x in list_x[i].split():
        if x in my_dict:
            my_dict[x] += 1
        else:
            my_dict[x] = 1
print(my_dict)

这是我尝试的代码,但这给了我每个单词出现在列表中的总次数。 我想得到的是这种格式:

{'this': {0, 1}, 'is': {0, 1}, 'the': {0, 1}, 'first': {0}, 'second': {1}}

正如您所看到的,这是关键,它一次出现在“ 0”位置,一次出现在“ 1”和..您知道我如何到达这一点吗?

3 个答案:

答案 0 :(得分:3)

修正了两行:

list_x = ["this is the first", "this is the second"]
my_dict = {}
for i in range(len(list_x)):
    for x in list_x[i].split():
        if x in my_dict:
            my_dict[x].append(i)
        else:
            my_dict[x] = [i]
print(my_dict)

返回:

{'this': [0, 1], 'is': [0, 1], 'the': [0, 1], 'first': [0], 'second': [1]}

答案 1 :(得分:1)

您也可以使用defaultdictenumerate来做到这一点:

from collections import defaultdict
list_x = ["this is the first",
          "this is the second",
          "third is this"]
pos = defaultdict(set)
for i, sublist in enumerate(list_x):
    for word in sublist.split():
        pos[word].add(i)

输出:

>>> from pprint import pprint
>>> pprint(dict(pos))
{'first': {0},
 'is': {0, 1, 2},
 'second': {1},
 'the': {0, 1},
 'third': {2},
 'this': {0, 1, 2}}

枚举的目的是提供list_x中每个字符串的索引(位置)。对于遇到的每个单词,其句子在list_x中的位置将被添加到结果pos中其对应关键字的集合中。

答案 2 :(得分:1)

而不是在字典中使用整数,而应使用set:

for i in range(len(list_x)):
    for x in list_x[i].split():
        if x in my_dict:
            my_dict[x].add(i)
        else:
            my_dict[x] = set([i])

或更简单地说,

for i in range(len(list_x)):
    for x in list_x[i].split():
        my_dict.setdefault(x, set()).add(i)