为了账号安全,请及时绑定邮箱和手机立即绑定

对不按顺序排列的字典键和值进行分组

对不按顺序排列的字典键和值进行分组

尚方宝剑之说 2023-06-06 17:37:25
下面我有一个字典列表:dict = [{'name': 'Sector',  'entity': 'ORG(100.0)',  'synonyms': "Sector:['sector', 'sphere'], , ",  'definition': 'Sector'},  {'name': 'Community Name',  'entity': 'PERSON(39.74)',  'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district']",  'definition': 'Community'}]如何添加将实体分组的新键,并将定义定义为值?所需的输出(类别是新添加的键):dict = [{'name': 'Sector',  'category': {  'entity': 'ORG(100.0)',  'definition': 'Sector'},  'synonyms': "Sector:['sector', 'sphere'], , "},  {'name': 'Community Name',  'category':{  'entity': 'PERSON(39.74)',  'definition': 'Community'},   'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district']"}]我已经尝试过[{'name': i.pop('name'), 'category': i}  for I in dict],但它只适用于按顺序排列的键,我如何修改它以便我可以选择某些键,因为entity它们definition彼此不相邻?
查看完整描述

2 回答

?
绝地无双

TA贡献1946条经验 获得超4个赞

看来你需要


data = [{'name': 'Sector',

  'entity': 'ORG(100.0)',

  'synonyms': "Sector:['sector', 'sphere'], , ",

  'definition': 'Sector'},


  {'name': 'Community Name',

  'entity': 'PERSON(39.74)',

  'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district']",

  'definition': 'Community'}]


subkeys = ['entity', 'definition']

result = [{'category': {k: i.pop(k) for k in subkeys},  **i}  for i in data]

print(result)

输出:


[{'category': {'definition': 'Sector', 'entity': 'ORG(100.0)'},

  'name': 'Sector',

  'synonyms': "Sector:['sector', 'sphere'], , "},

 {'category': {'definition': 'Community', 'entity': 'PERSON(39.74)'},

  'name': 'Community Name',

  'synonyms': "Community:['biotic_community', 'community', "

              "'community_of_interests', 'residential_area', "

              "'residential_district']"}]


查看完整回答
反对 回复 2023-06-06
?
四季花海

TA贡献1811条经验 获得超5个赞

看起来您想转换每个对象,在这种情况下,我会选择具有自定义功能的地图。


import json


dicts = [

    {

        'name': 'Sector', 

        'entity': 'ORG(100.0)', 

        'synonyms': "Sector:['sector', 'sphere'], , ", 

        'definition': 'Sector'

    },

    {

        'name': 'Community Name', 

        'entity': 'PERSON(39.74)', 

        'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district']", 

        'definition': 'Community'

    }

]


def map_func(item):

    item['category'] = {'entity': item['entity'], 'definition': item['definition']}

    item.pop('entity')

    item.pop('definition')

    return item


mapped_dicts = map(lambda x: map_func(x), dicts)

print(json.dumps(list(mapped_dicts), indent=2))


[

  {

    "name": "Sector",

    "synonyms": "Sector:['sector', 'sphere'], , ",

    "category": {

      "entity": "ORG(100.0)",

      "definition": "Sector"

    }

  },

  {

    "name": "Community Name",

    "synonyms": "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district']",

    "category": {

      "entity": "PERSON(39.74)",

      "definition": "Community"

    }

  }

]


查看完整回答
反对 回复 2023-06-06
  • 2 回答
  • 0 关注
  • 113 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信