json 我想使用list和python生成复杂的对象列表

q3qa4bjr  于 2023-02-26  发布在  Python
关注(0)|答案(1)|浏览(102)

我想根据一些规则和对生成数据的独特结构不知道如何去做,任何帮助将不胜感激
我有两份清单:

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}

任何帮助都将不胜感激

2lpgd968

2lpgd9681#

这可能比您想象的要复杂一些,无法可靠地处理。
不如这样:

import json # only needed for output formatting

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 = []

for d in only_on2:
    for k, v in d.items():
        data = {}
        _d = {'name_place': k, 'data': data}
        for _v in v:
            if (cn := _v.get('cats_name')) in categories:
                data.setdefault(cn, {}).setdefault('UNIT #', []).append(_v.get('code'))
                data[cn].setdefault('Name', []).append(_v.get('name'))
        result_desired.append(_d)

print(json.dumps(result_desired, indent=2))
    • 输出:**
[
  {
    "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"
        ]
      }
    }
  }
]

相关问题