【发布时间】:2019-12-26 01:15:38
【问题描述】:
对于一个需要在没有 pandas 或 numpy 的情况下解决的问题,我需要帮助。我有两个字典列表,即 list1 和 list2。我需要按“post_code”对 list2 进行排序,然后将其分组 e 在加入 list1 和 list2 之前按“代码”对 list2 进行排序,然后按两个具有相同值的不同键。在 list1 中,键“practice”等价于排序后的 list2 中的键“code”。我需要通过“实践”和“代码”的等效键加入list1和list2。
list1=
[{'bnf_code': '0101010G0AAABAB',
'items': 2,
'practice': 'N81013',
'bnf_name': 'Co-Magaldrox_Susp 195mg/220mg/5ml S/F',
'nic': 5.98,
'act_cost': 5.56,
'quantity': 1000},
{'bnf_code': '0101021B0AAAHAH',
'items': 1,
'practice': 'A81001',
'bnf_name': 'Alginate_Raft-Forming Oral Susp S/F',
'nic': 1.95,
'act_cost': 1.82,
'quantity': 500},
{'bnf_code': '0101021B0AAALAL',
'items': 12,
'practice': 'A81002',
'bnf_name': 'Sod Algin/Pot Bicarb_Susp S/F',
'nic': 64.51,
'act_cost': 59.95,
'quantity': 6300},
{'bnf_code': '0101021B0AAAPAP',
'items': 3,
'practice': 'A81004',
'bnf_name': 'Sod Alginate/Pot Bicarb_Tab Chble 500mg',
'nic': 9.21,
'act_cost': 8.55,
'quantity': 180},
{'bnf_code': '0101021B0BEADAJ',
'items': 6,
'practice': 'A81003',
'bnf_name': 'Gaviscon Infant_Sach 2g (Dual Pack) S/F',
'nic': 28.92,
'act_cost': 26.84,
'quantity': 90}]
list2=
[{'code': 'A81001',
'name': 'THE DENSHAM SURGERY',
'addr_1': 'THE HEALTH CENTRE',
'addr_2': 'LAWSON STREET',
'borough': 'STOCKTON ON TEES',
'village': 'CLEVELAND',
'post_code': 'TS18 1HU'},
{'code': 'A81002',
'name': 'QUEENS PARK MEDICAL CENTRE',
'addr_1': 'QUEENS PARK MEDICAL CTR',
'addr_2': 'FARRER STREET',
'borough': 'STOCKTON ON TEES',
'village': 'CLEVELAND',
'post_code': 'TS18 2AW'},
{'code': 'A81003',
'name': 'VICTORIA MEDICAL PRACTICE',
'addr_1': 'THE HEALTH CENTRE',
'addr_2': 'VICTORIA ROAD',
'borough': 'HARTLEPOOL',
'village': 'CLEVELAND',
'post_code': 'TS26 8DB'},
{'code': 'A81004',
'name': 'WOODLANDS ROAD SURGERY',
'addr_1': '6 WOODLANDS ROAD',
'addr_2': None,
'borough': 'MIDDLESBROUGH',
'village': 'CLEVELAND',
'post_code': 'TS1 3BE'},
{'code': 'N81013',
'name': 'SPRINGWOOD SURGERY',
'addr_1': 'SPRINGWOOD SURGERY',
'addr_2': 'RECTORY LANE',
'borough': 'GUISBOROUGH',
'village': None,
'post_code': 'TS14 7DJ'}]
我已经能够按 post_code 对 list2 进行排序并按 code 分组,但我不知道如何加入 list1 和 list2。这是我目前用于排序和分组的代码。
import itertools
from operator import itemgetter
sorted_post_code = sorted(list2, key=itemgetter('post_code'))
for key, group in itertools.groupby(sorted_post_code, key=lambda x:x['code']):
#print (key),
print (list(group))
预期输出是
joined_list=
list1=
[{'bnf_code': '0101010G0AAABAB',
'items': 2,
'practice': 'N81013',
'bnf_name': 'Co-Magaldrox_Susp 195mg/220mg/5ml S/F',
'nic': 5.98,
'act_cost': 5.56,
'quantity': 1000,
'code': 'N81013',
'name': 'SPRINGWOOD SURGERY',
'addr_1': 'SPRINGWOOD SURGERY',
'addr_2': 'RECTORY LANE',
'borough': 'GUISBOROUGH',
'village': None,
'post_code': 'TS14 7DJ'},
{'bnf_code': '0101021B0AAAHAH',
'items': 1,
'practice': 'A81001',
'bnf_name': 'Alginate_Raft-Forming Oral Susp S/F',
'nic': 1.95,
'act_cost': 1.82,
'quantity': 500,
'code': 'A81001',
'name': 'THE DENSHAM SURGERY',
'addr_1': 'THE HEALTH CENTRE',
'addr_2': 'LAWSON STREET',
'borough': 'STOCKTON ON TEES',
'village': 'CLEVELAND',
'post_code': 'TS18 1HU'},
{'bnf_code': '0101021B0AAALAL',
'items': 12,
'practice': 'A81002',
'bnf_name': 'Sod Algin/Pot Bicarb_Susp S/F',
'nic': 64.51,
'act_cost': 59.95,
'quantity': 6300,
'code': 'A81002',
'name': 'QUEENS PARK MEDICAL CENTRE',
'addr_1': 'QUEENS PARK MEDICAL CTR',
'addr_2': 'FARRER STREET',
'borough': 'STOCKTON ON TEES',
'village': 'CLEVELAND',
'post_code': 'TS18 2AW'},
{'bnf_code': '0101021B0AAAPAP',
'items': 3,
'practice': 'A81004',
'bnf_name': 'Sod Alginate/Pot Bicarb_Tab Chble 500mg',
'nic': 9.21,
'act_cost': 8.55,
'quantity': 180,
'code': 'A81004',
'name': 'WOODLANDS ROAD SURGERY',
'addr_1': '6 WOODLANDS ROAD',
'addr_2': None,
'borough': 'MIDDLESBROUGH',
'village': 'CLEVELAND',
'post_code': 'TS1 3BE'},
{'bnf_code': '0101021B0BEADAJ',
'items': 6,
'practice': 'A81003',
'bnf_name': 'Gaviscon Infant_Sach 2g (Dual Pack) S/F',
'nic': 28.92,
'act_cost': 26.84,
'quantity': 90,
'code': 'A81003',
'name': 'VICTORIA MEDICAL PRACTICE',
'addr_1': 'THE HEALTH CENTRE',
'addr_2': 'VICTORIA ROAD',
'borough': 'HARTLEPOOL',
'village': 'CLEVELAND',
'post_code': 'TS26 8DB'}]
【问题讨论】:
-
“加入”list1 和 2 是什么意思?请显示想要的输出。您的代码根本不做任何加入。
-
什么是
practices? -
如果加入意味着分组,那么您可能根本不需要排序。可能是使用
dict之类的东西按想要的键分组 -
我已经更正了。对于那个很抱歉。它应该是 list2 而不是实践。
标签: python list dictionary join merge