gpt4 book ai didi

python - 通过嵌套字典键获取唯一列表项

转载 作者:太空宇宙 更新时间:2023-11-04 10:19:41 24 4
gpt4 key购买 nike

我将列表作为字典值嵌套在另一个名为 data 的字典中。我一直在尝试找到一种从特定嵌套键(如 key1key2)获取所有唯一列表项的快速方法。

我想出了以下功能,这似乎不是很有效。我有什么想法可以加快速度并变得更加 pythonic 吗?

Python 函数

def get_uniq_by_value(data, val_name):
results = []
for key, value in data.iteritems():
for item in value[val_name]:
if item not in results:
results.append(item)
return results

示例数据

data = {
"top1": {
"key1": [
"there is no spoon", "but dictionaries are hard",
],
"key2": [
"mad max fury road was so good",
]
},
"top2": {
"key1": [
"my item", "foo bar"
],
"key2": [
"blah", "more junk"
]
},

最佳答案

如果顺序无关紧要,您可以使用 set/set comprehension 来获得所需的结果 -

def get_uniq_by_value(data, val_name):
return {val for value in data.values() for val in value.get(val_name,[])}

如果您想要一个列表作为结果,您可以在集合理解上使用 list() 以在返回之前将结果集转换为列表。

演示 -

>>> def get_uniq_by_value(data, val_name):
... return {val for value in data.values() for val in value.get(val_name,[])}
...
>>> data = {
... "top1": {
... "key1": [
... "there is no spoon", "but dictionaries are hard",
... ],
... "key2": [
... "mad max fury road was so good",
... ]
... },
... "top2": {
... "key1": [
... "my item", "foo bar"
... ],
... "key2": [
... "blah", "more junk"
... ]
... }}
>>> get_uniq_by_value(data,"key1")
{'but dictionaries are hard', 'my item', 'foo bar', 'there is no spoon'}

如以下评论所示,如果顺序很重要并且 data 已经是 OrderedDictcollections.OrderedDict,您可以使用一个新的 OrderedDict ,并将列表中的元素添加为键,OrderedDict 将避免任何重复并保留添加键的顺序。

您也可以使用 OrderedDict.fomkeys 在一行中完成此操作,如注释中所示。示例 -

from collections import OrderedDict
def get_uniq_by_value(data, val_name):
return list(OrderedDict.fromkeys(val for value in data.values() for val in value.get(val_name,[])))

请注意,这只适用于 data 是嵌套的 OrderedDict,否则 data 的元素根本不会以任何特定顺序开始。

演示 -

>>> from collections import OrderedDict
>>> data = OrderedDict([
... ("top1", OrderedDict([
... ("key1", [
... "there is no spoon", "but dictionaries are hard",
... ]),
... ("key2", [
... "mad max fury road was so good",
... ])
... ])),
... ("top2", OrderedDict([
... ("key1", [
... "my item", "foo bar"
... ]),
... ("key2", [
... "blah", "more junk"
... ])
... ]))])
>>>
>>> def get_uniq_by_value(data, val_name):
... return list(OrderedDict.fromkeys(val for value in data.values() for val in value.get(val_name,[])))
...
>>> get_uniq_by_value(data,"key1")
['there is no spoon', 'but dictionaries are hard', 'my item', 'foo bar']

关于python - 通过嵌套字典键获取唯一列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33042979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com