Reputation: 53
I have a nested dictionary. This dictionary keeps the text ids and the repetitions of the words in those texts. I want to group these repetition numbers with certain intervals and keep the number of elements in the group in a dictionary or dataframe. I've included an example below. I tried many ways but couldn't do exactly what I wanted. Thank you in advance for your help.
Initial dictionary:
{938742158: {'car': 3, 'yes': 5, 'none': 6, 'bi': 5, 'new': 4, 'service': 2, 'color': 8, 'have': 6, 'back': 5},
938735955: {'car': 5, 'my': 11, 'note': 5, 'dir': 3, 'led': 6, 'r': 3, 'line': 10}}
Target output:
{938742158: {'1-3': 2, '3-5': 4, '5-7': 2, '7-10': 1, '10+': 0},
938735955: {'1-3': 2, '3-5': 2, '5-7': 1, '7-10': 1, '10+': 1}}
Upvotes: 0
Views: 167
Reputation: 23815
Try the below
data = {938742158: {'car': 3, 'yes': 5, 'none': 6, 'bi': 5, 'new': 4, 'service': 2, 'color': 8, 'have': 6, 'back': 5},
938735955: {'car': 5, 'my': 11, 'note': 5, 'dir': 3, 'led': 6, 'r': 3, 'line': 10}}
post_data = {k: {'1-3': 0, '3-5': 0, '5-7': 0, '7-10': 0, '10+': 0} for k, v in data.items()}
for k, v in data.items():
for value in v.values():
if 1 < value <= 3:
post_data[k]['1-3'] += 1
elif 3 < value <= 5:
post_data[k]['3-5'] += 1
#TODO add more conditions
print(post_data)
output
{938742158: {'1-3': 2, '3-5': 4, '5-7': 0, '7-10': 0, '10+': 0}, 938735955: {'1-3': 2, '3-5': 2, '5-7': 0, '7-10': 0, '10+': 0}}
Upvotes: 1