亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對不按順序排列的字典鍵和值進行分組

對不按順序排列的字典鍵和值進行分組

尚方寶劍之說 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 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號