python- 搜索字典子列表;将字典键转换为值
python- searching dictionary sublists; converting dictionary keys to values
假设我有以下词典(我正在使用的词典大得多):
dict1={1:["item", "word", "thing"], 2:["word", "item"], 3:["thing", "item", "item"]}
并将词典中使用的每个单词都存储在列表中:
all_words=["item", "word", "thing"]
我想 运行 列表中的每个单词通过字典子列表,return 找到它们的所有子列表的键,将它们存储在元组中。所以我想得到:
dict2={"item":(1, 2, 3), "word":(1, 2), "thing":(1, 3)}
这是我的:
dict2={}
for word in all_words:
for key, sublist in dict2.items():
for word in sublist:
if word not in sublist:
dict2[word]=dict2[word]+key
else:
dict2[word]=key
您的代码逻辑不正确,因为您正在遍历 3 个对象,而您只需要遍历字典并反转键和值的位置,但由于您可能有重复的值,因此您可以使用 set
容器,用于保留每个名称的相应键。 dict.setdefault
是解决这种情况的好工具:
>>> d={}
>>> for i,j in dict1.items():
... for k in j:
... d.setdefault(k,set()).add(i)
...
>>> d
{'item': set([1, 2, 3]), 'word': set([1, 2]), 'thing': set([1, 3])}
因此,根据评论,您的固定程序如下所示
>>> dict2 = {}
>>> for word in all_words:
... # Iterate over the dict1's items
... for key, sublist in dict1.items():
... # If the word is found in the sublist
... if word in sublist:
... # If the current word is found in dict2's keys
... if word in dict2:
... # Append the current key as a one element tuple
... dict2[word] += (key,)
... else:
... # Create a one element tuple and assign it to the word
... dict2[word] = (key,)
...
>>> dict2
{'item': (1, 2, 3), 'word': (1, 2), 'thing': (1, 3)}
如果你了解字典理解,那么同样可以写成
>>> {word: tuple(k for k, v in dict1.items() if word in v) for word in all_words}
{'item': (1, 2, 3), 'word': (1, 2), 'thing': (1, 3)}
整个元组创建逻辑,基于每个对应的 word
的 dict1
,已压缩为单个生成器表达式并转换为具有 tuple(k for k, v in dict1.items() if word in v)
[=15 的元组=]
问题是您正在循环 dict2.items
而它应该是 dict1.items
。此外,如果找到 将键附加 到 dict2
值中,您只需将值重新分配给在 dict1
值中找到的最后一个键。因此 dict2
值不是您所期望的。
您也可以使用 collections.defaultdict
(或使用来自@Kasra、@thefourtheye 的解决方案):
from collections import defaultdict
dict2 = defaultdict(tuple)
for word in all_words:
for key, sublist in dict1.iteritems(): # this
if word in sublist:
dict2[word] += (k,)
else:
dict2[word] = (k,)
dict2
Out[3]: defaultdict(<type 'tuple'>, {'item': (1, 2, 3), 'word': (1, 2), 'thing': (1, 3)})
假设我有以下词典(我正在使用的词典大得多):
dict1={1:["item", "word", "thing"], 2:["word", "item"], 3:["thing", "item", "item"]}
并将词典中使用的每个单词都存储在列表中:
all_words=["item", "word", "thing"]
我想 运行 列表中的每个单词通过字典子列表,return 找到它们的所有子列表的键,将它们存储在元组中。所以我想得到:
dict2={"item":(1, 2, 3), "word":(1, 2), "thing":(1, 3)}
这是我的:
dict2={}
for word in all_words:
for key, sublist in dict2.items():
for word in sublist:
if word not in sublist:
dict2[word]=dict2[word]+key
else:
dict2[word]=key
您的代码逻辑不正确,因为您正在遍历 3 个对象,而您只需要遍历字典并反转键和值的位置,但由于您可能有重复的值,因此您可以使用 set
容器,用于保留每个名称的相应键。 dict.setdefault
是解决这种情况的好工具:
>>> d={}
>>> for i,j in dict1.items():
... for k in j:
... d.setdefault(k,set()).add(i)
...
>>> d
{'item': set([1, 2, 3]), 'word': set([1, 2]), 'thing': set([1, 3])}
因此,根据评论,您的固定程序如下所示
>>> dict2 = {}
>>> for word in all_words:
... # Iterate over the dict1's items
... for key, sublist in dict1.items():
... # If the word is found in the sublist
... if word in sublist:
... # If the current word is found in dict2's keys
... if word in dict2:
... # Append the current key as a one element tuple
... dict2[word] += (key,)
... else:
... # Create a one element tuple and assign it to the word
... dict2[word] = (key,)
...
>>> dict2
{'item': (1, 2, 3), 'word': (1, 2), 'thing': (1, 3)}
如果你了解字典理解,那么同样可以写成
>>> {word: tuple(k for k, v in dict1.items() if word in v) for word in all_words}
{'item': (1, 2, 3), 'word': (1, 2), 'thing': (1, 3)}
整个元组创建逻辑,基于每个对应的 word
的 dict1
,已压缩为单个生成器表达式并转换为具有 tuple(k for k, v in dict1.items() if word in v)
[=15 的元组=]
问题是您正在循环 dict2.items
而它应该是 dict1.items
。此外,如果找到 将键附加 到 dict2
值中,您只需将值重新分配给在 dict1
值中找到的最后一个键。因此 dict2
值不是您所期望的。
您也可以使用 collections.defaultdict
(或使用来自@Kasra、@thefourtheye 的解决方案):
from collections import defaultdict
dict2 = defaultdict(tuple)
for word in all_words:
for key, sublist in dict1.iteritems(): # this
if word in sublist:
dict2[word] += (k,)
else:
dict2[word] = (k,)
dict2
Out[3]: defaultdict(<type 'tuple'>, {'item': (1, 2, 3), 'word': (1, 2), 'thing': (1, 3)})