I want to merge the third index (number list) in test_list
if the first two, index('2021-03-18'
, 'Night'
), are the same.
for example:
test_list
:
[['2021-03-18', 'Night', [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27]],\
['2021-03-18', 'Night',[46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 57, 58, 59]],
['2021-03-19','Other', [33, 34, 35, 36, 37, 38, 57,58,59]]]
How do I merge this into a list like this?
Desired result:
['2021-03-18', 'Night', [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 57, 58, 59]],
['2021-03-19', 'Other', [33, 34, 35, 36, 37, 38, 57,58,59]]
]
CodePudding user response:
test_list = [['2021-03-18', 'Night', [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27]], \
['2021-03-18', 'Night', [46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 57, 58, 59]], \
['2021-03-19','Other', [33, 34, 35, 36, 37, 38, 57,58,59]]]
d = {}
for date, key, lst in test_list:
if (date, key) in d:
d[(date, key)] = lst
else:
d[(date, key)] = lst
result = [list(key) item for key, item in d.items()]