英文:
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('tags', []):
tags.add(value)
if item.get('child'):
tags |= get_tags(item['child'])
return tags
print(get_tags(data))
Output :
{'tag1', 'tag2', 'tag3', 'tag4', 'tag5'}
答案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('tags', []))
if obj.get('child'):
child_tags = get_tags(obj["child"])
exp_tags.extend(child_tags)
return list(set(exp_tags))
Output:
['tag4', 'tag5', 'tag2', 'tag1', 'tag3']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论