Currently, I am trying to create a new dictionary from an unusable dictionary but am having difficulty updating/creating new keys based on the number of tuples in the 'Acceptable' key.
dirty_dictionary = {'DOG': {'Acceptable': ([[35, 38]], 'DOG_GROUP'),
'Unacceptable': ([[2], [29], [44], [50], [54], [60]], 'DOG_GROUP')},
'CAT': {'Acceptable': ([[3, 6], [100, 101]], 'CAT_GROUP'), 'Unacceptable': ([[12], [18], [45], [51], [61]], 'CAT_GROUP')},
'FISH': {'Acceptable': ([], 'FISH_GROUP'), 'Unacceptable': ([[13], [19], [22], [28], [34]], 'FISH_GROUP')},
'COW': {'Acceptable': ([[87, 69]], 'COW_GROUP'), 'Unacceptable': ([], 'COW_GROUP')}}
new_dict = {}
for key, values in dirty_dictionary.items():
if len(values['Acceptable'][0]) == 0:
new_dict[dirty_dictionary[key]['Acceptable'][-1]] = {'Acceptable': []}
for oids in values['Acceptable'][0]:
if len(values['Acceptable'][0]) == 1:
new_dict[dirty_dictionary[key]['Acceptable'][-1]] = {'Acceptable': oids}
if len(values['Acceptable'][0]) > 1:
for i in range(len(values['Acceptable'][0])):
new_dict[dirty_dictionary[key]['Acceptable'][-1] + F'_{i}'] = {'Acceptable': values['Acceptable'][0][i]}
# for oids in values['Unacceptable'][0]:
# if len(values['Unacceptable'][0]) == 1:
# new_dict[dirty_dictionary[key]['Unacceptable'][-1]].update({'Unacceptable': oids})
# if len(values['Unacceptable'][0]) > 1:
# for i in range(len(values['Unacceptable'][0])):
# new_dict[dirty_dictionary[key]['Unacceptable'][-1] + F'_{i}'].update({'Unacceptable': values['Unacceptable'][0][i]})
print(new_dict)
I can create a new dictionary with all 'Acceptable' Keys/Values, but I am stuck on updating the dictionary with the 'Unacceptable' since new groups need to be created if the len of values['Acceptable'][0] > 1.
The goal is to get the final dictionary to look like:
final = {'DOG_GROUP': {'Acceptable': [35, 38], 'Unacceptable': [2, 29, 44, 50, 54, 60]},
'CAT_GROUP_0': {'Acceptable': [3, 6], 'Unacceptable': []},
'CAT_GROUP_1': {'Acceptable': [100, 101], 'Unacceptable': [12, 18, 45, 51, 61]},
'FISH_GROUP': {'Acceptable': [], 'Unacceptable': [13, 19, 22, 28, 34]},
'COW_GROUP': {'Acceptable': [87, 69], 'Unacceptable': []}}
Try this one.
dirty_dictionary = {'DOG': {'Acceptable': ([[35, 38]], 'DOG_GROUP'), 'Unacceptable': ([[2], [29], [44], [50], [54], [60]], 'DOG_GROUP')}, 'CAT': {'Acceptable': ([[3, 6], [100, 101]], 'CAT_GROUP'), 'Unacceptable': ([[12], [18], [45], [51], [61]], 'CAT_GROUP')}, 'FISH': {'Acceptable': ([], 'FISH_GROUP'), 'Unacceptable': ([[13], [19], [22], [28], [34]], 'FISH_GROUP')}}
new_dict = {}
# assign text strings in variable to get suggestion in IDE
acceptable = 'Acceptable'
unacceptable = 'Unacceptable'
for key, values in dirty_dictionary.items():
group = values[acceptable][1]
if len(values[acceptable][0]) <= 1:
new_dict[group] = {}
new_dict[group][acceptable] = [y for x in values[acceptable][0] for y in x]
new_dict[group][unacceptable] = [y for x in values[unacceptable][0] for y in x]
else:
for idx, item in enumerate(values[acceptable][0]):
group_temp = group + '_' + str(idx+1)
new_dict[group_temp] = {}
new_dict[group_temp][acceptable] = item
# if last item then give all unacceptable as a single array
if idx == len(values[acceptable][0]) - 1:
new_dict[group_temp][unacceptable] = [y for x in values[unacceptable][0] for y in x]
else: # else empty array
new_dict[group_temp][unacceptable] = []
print(new_dict)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments