我想根据一些规则和对生成数据的独特结构不知道如何去做,任何帮助将不胜感激
我有两份清单:
categories = [
'fruits' 'meats'
]
另一个列表是:
only_on2 = [
{
"earth": [
{
"code": 1,
"cats_name": 'fruits',
"name": "apple"
},
{
"code": 2,
"cats_name": 'meats',
"name": "beef"
},
{
"code": 7,
"cats_name": 'meats',
"name": "chicken"
}
],
"sky": [
{
"code": 3,
"cats_name": 'fruits',
"name": "apple"
},
{
"code": 4,
"cats_name": 'meats',
"name": "chicken"
}
]
}
]
并且想要基于先前的列表生成这个新列表
result_desired = [
{
"name_place": "earth",
"data": {
"fruits": {"UNIT #": [1],
"Name": ["apple"]
},
"meats": {"UNIT #": [2, 7],
"Name": ["beef", "chicken"]
},
}
},
{
"name_place": "sky",
"data": {
"fruits": {"UNIT #": [3],
"Name": ["apple"]
},
"meats": {"UNIT #": [4],
"Name": ['chicken']
},
}
}
]
我试过的,那些方式不起作用,因为需要,但两个列表内的列表比较
results = {place: {cat: {
'UNIT #': [item1 for key, value in only_on2 if item1['cats_name'] == cat for item1 in value],
} for cat in categories} for place in only_on2}
任何帮助都将不胜感激
1条答案
按热度按时间2lpgd9681#
这可能比您想象的要复杂一些,无法可靠地处理。
不如这样: