[英]Dictionary with a query of sets in 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”,然后..知道我如何达到这一点吗?
固定两行:
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]}
您也可以使用defaultdict
并enumerate
:
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
的集合中。
而不是在字典中使用整数,而应使用集合:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.