我需要从列表中所有嵌套字典的”tags”字段中获取唯一的数值。

huangapple go评论62阅读模式
英文:

I need to get the unique values from the tags field in all the nested dictionaries from the list

问题

以下是翻译好的代码部分:

这是我的示例数据

```python
data = [{
        "name": "name1",
        "status": "one",
        "tags": ["tag11","tag4"],
        "child": [{
                "name": "name2",
                "status": "two",
                "child": [{
                        "name": "name3",
                        "status": "three",
                        "tags": ["tag1","tag512"],
                        "child": [{
                                "name": "name3",
                                "status": "four",
                                "tags": ["tag22","tag4"],
                                "child": []
                                }]
                         }],
                }]
        },
        {
        "name": "name2",
        "status": "one",
        "tags": ["tag1","tag33"],
        "child": [{
                "name": "name3",
                "status": "four",
                "child": [[{
                        "name": "name3",
                        "status": "four",
                        "tags": ["tag221","tag4"],
                        "child": []
                          }]]
                }]
        }
]

我尝试以以下方式获取

def tags(data):
    tags = []
    for item in data:
        for values in item.get('tags', []):
            if values not in tags:
                tags.append(values)
    print(tags)
tags(data)

期望的输出:"tag11","tag4","tag1","tag512","tag22","tag4","tag1","tag33","tag221"(来自所有嵌套字典中的键的所有唯一值)在一个列表中。

英文:

This is my sample data

data = [{
        "name": "name1",
        "status": "one",
        "tags": ["tag11","tag4"],
        "child": [{
                "name": "name2",
                "status": "two",
                "child": [{
                        "name": "name3",
                        "status": "three",
                        "tags": ["tag1","tag512"],
                        "child": [{
                                "name": "name3",
                                "status": "four",
                                "tags": ["tag22","tag4"],
                                "child": []
                                }]
                         }],
                }]
        },
        {
        "name": "name2",
        "status": "one",
        "tags": ["tag1","tag33"],
        "child": [{
                "name": "name3",
                "status": "four",
                "child": [[{
                        "name": "name3",
                        "status": "four",
                        "tags": ["tag221","tag4"],
                        "child": []
                          }]]
                }]
        }
]

i tried in this way to get

def tags(data):
    tags = []
    for item in data:
        for values in item.get('tags', []):
            if values not in tags:
                tags.append(values)
    print(tags)
tags(data)

Expected output: `"tag11","tag4","tag1","tag512","tag22","tag4","tag1","tag33","tag221" (all unique values from keys)in all the nested dictionaries in a list.

答案1

得分: 2

你可以使用递归函数来遍历嵌套的字典并提取所有唯一的标签

```py
def get_tags(data):
    tags = set()
    for item in data:
        for value in item.get('tags', []):
            tags.add(value)
        if item.get('child'):
            tags |= get_tags(item['child'])
    return tags

print(get_tags(data))

输出:

{'tag1', 'tag2', 'tag3', 'tag4', 'tag5'}

<details>
<summary>英文:</summary>

You can use a recursive function to traverse through the nested dictionaries and extract all the unique tags :

```py
def get_tags(data):
    tags = set()
    for item in data:
        for value in item.get(&#39;tags&#39;, []):
            tags.add(value)
        if item.get(&#39;child&#39;):
            tags |= get_tags(item[&#39;child&#39;])
    return tags

print(get_tags(data))

Output :

{&#39;tag1&#39;, &#39;tag2&#39;, &#39;tag3&#39;, &#39;tag4&#39;, &#39;tag5&#39;}

答案2

得分: 1

你可以使用此方法获取唯一的 '标签',该函数如下:

def get_tags(data):
    exp_tags = []
    for obj in data:
        exp_tags.extend(obj.get('tags', []))
        if obj.get('child'):
            child_tags = get_tags(obj["child"])
            exp_tags.extend(child_tags)
    return list(set(exp_tags))

输出:

['tag4', 'tag5', 'tag2', 'tag1', 'tag3']
英文:

You can use this method to get the unique 'tags', the function will be as follows:

def get_tags(data):
    exp_tags = []
    for obj in data:
        exp_tags.extend(obj.get(&#39;tags&#39;, []))
        if obj.get(&#39;child&#39;):
            child_tags = get_tags(obj[&quot;child&quot;])
            exp_tags.extend(child_tags)
    return list(set(exp_tags))

Output:

[&#39;tag4&#39;, &#39;tag5&#39;, &#39;tag2&#39;, &#39;tag1&#39;, &#39;tag3&#39;]

huangapple
  • 本文由 发表于 2023年2月23日 21:13:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定