Created
October 13, 2022 06:57
-
-
Save maddrum/3d3d60066fa2aa7cd71ed290a5df34d8 to your computer and use it in GitHub Desktop.
Why know it is Python when....
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
value_list = [ | |
{ | |
'name': 'a', | |
'value': '1', | |
}, | |
{ | |
'name': 'a', | |
'value': '2', | |
}, | |
{ | |
'name': 'b', | |
'value': '10', | |
}, | |
{ | |
'name': 'b', | |
'value': '11', | |
}, | |
{ | |
'name': 'c', | |
'value': '100', | |
}, | |
] | |
DELIMITER_SYMBOL = ',' | |
# you start with | |
result_dict = {} | |
processed_slugs = [] | |
for item in value_list: | |
if item['name'] not in result_dict: | |
result_dict[item['name']] = item['value'] | |
processed_slugs.append(item['value']) | |
continue | |
if item['value'] not in processed_slugs: | |
result_dict[item['name']] += f'{DELIMITER_SYMBOL}{item["value"]}' | |
processed_slugs.append(item['value']) | |
print(result_dict) | |
# then you go | |
result_dict = {} | |
all_names = {item['name'] for item in value_list} | |
for item in sorted(all_names): | |
filtered_list = list(filter(lambda x: x['name'] == item, value_list)) | |
slugs_list = list({single_item['value'] for single_item in filtered_list}) | |
result_dict[item] = DELIMITER_SYMBOL.join(sorted(slugs_list)) | |
print(result_dict) | |
# and you finish with | |
all_names = {item['name'] for item in value_list} | |
result_dict = { | |
item: DELIMITER_SYMBOL.join( | |
sorted(list({single_item['value'] for single_item in value_list if single_item['name'] == item}))) | |
for item in sorted(all_names) | |
} | |
print(result_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment