如何在字典列表中分组和添加字典键

时间:2020-08-18 19:35:01

标签: python dictionary

下面有一个词典列表:

dict = [{'name': 'Sector',
  'entity': 'ORG(100.0), nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan',
  'synonyms': "Sector:['sector', 'sphere'], , ",
  'definition': 'Sector: a plane figure bounded by two radii and the included arc of a circle',
  'sentiment': ''},
  {'name': 'Community Name',
  'entity': 'PERSON(39.74), GPE(22.88), ORG(20.57), LOC(9.95), FAC(3.6), NORP(2.02), CARDINAL(0.45), LAW(0.39), DATE(0.39), nan, nan, nan, nan, nan',
  'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district'], Name:['advert', 'appoint', 'bring_up', 'call', 'cite', 'constitute', 'describe', 'diagnose', 'discover', 'distinguish', 'epithet', 'figure', 'gens', 'identify', 'key', 'key_out', 'list', 'make', 'mention', 'name', 'nominate', 'public_figure', 'refer'], ",
  'definition': 'Community: a group of people living in a particular local area, Name: a language unit by which a person or thing is known',
  'sentiment': ''}]

如何添加将实体,同义词,定义和情感分组为值的新键?

所需的输出(nlp是新添加的键):

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

  'nlp': {

  'entity': 'ORG(100.0), nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan',
  'synonyms': "Sector:['sector', 'sphere'], , ",
  'definition': 'Sector: a plane figure bounded by two radii and the included arc of a circle',
  'sentiment': ''}},

  {'name': 'Community Name',

  'nlp':{

  'entity': 'PERSON(39.74), GPE(22.88), ORG(20.57), LOC(9.95), FAC(3.6), NORP(2.02), CARDINAL(0.45), LAW(0.39), DATE(0.39), nan, nan, nan, nan, nan',
  'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district'], Name:['advert', 'appoint', 'bring_up', 'call', 'cite', 'constitute', 'describe', 'diagnose', 'discover', 'distinguish', 'epithet', 'figure', 'gens', 'identify', 'key', 'key_out', 'list', 'make', 'mention', 'name', 'nominate', 'public_figure', 'refer'], ",
  'definition': 'Community: a group of people living in a particular local area, Name: a language unit by which a person or thing is known',
  'sentiment': ''}}]

2 个答案:

答案 0 :(得分:1)

您可以在for循环中使用.pop()

lst = [{'name': 'Sector',
        'entity': 'ORG(100.0), nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan',
        'synonyms': "Sector:['sector', 'sphere'], , ",
        'definition': 'Sector: a plane figure bounded by two radii and the included arc of a circle',
        'sentiment': ''},
       {'name': 'Community Name',
        'entity': 'PERSON(39.74), GPE(22.88), ORG(20.57), LOC(9.95), FAC(3.6), NORP(2.02), CARDINAL(0.45), LAW(0.39), DATE(0.39), nan, nan, nan, nan, nan',
        'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district'], Name:['advert', 'appoint', 'bring_up', 'call', 'cite', 'constitute', 'describe', 'diagnose', 'discover', 'distinguish', 'epithet', 'figure', 'gens', 'identify', 'key', 'key_out', 'list', 'make', 'mention', 'name', 'nominate', 'public_figure', 'refer'], ",
        'definition': 'Community: a group of people living in a particular local area, Name: a language unit by which a person or thing is known',
        'sentiment': ''}]

result = []
for dct in lst:
    newdict = {}
    newdict["name"] = dct.pop('name')
    newdict["nlp"] = dct
    result.append(newdict)

print(result)

哪个产量

[{'name': 'Sector', 'nlp': {'entity': 'ORG(100.0), nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan', 'synonyms': "Sector:['sector', 'sphere'], , ", 'definition': 'Sector: a plane figure bounded by two radii and the included arc of a circle', 'sentiment': ''}}, {'name': 'Community Name', 'nlp': {'entity': 'PERSON(39.74), GPE(22.88), ORG(20.57), LOC(9.95), FAC(3.6), NORP(2.02), CARDINAL(0.45), LAW(0.39), DATE(0.39), nan, nan, nan, nan, nan', 'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district'], Name:['advert', 'appoint', 'bring_up', 'call', 'cite', 'constitute', 'describe', 'diagnose', 'discover', 'distinguish', 'epithet', 'figure', 'gens', 'identify', 'key', 'key_out', 'list', 'make', 'mention', 'name', 'nominate', 'public_figure', 'refer'], ", 'definition': 'Community: a group of people living in a particular local area, Name: a language unit by which a person or thing is known', 'sentiment': ''}}]

答案 1 :(得分:1)

pop()删除一个元素并返回该元素

result = [{'name':d.pop('name'), 'nlp':d} for d in dict]

通过使用专有密钥name,我们从d中删除了该密钥。如前所述,d.pop('name')将返回d['name']

中的值

d.pop('name')之后,d中将没有密钥name