arevilla009
arevilla009

Reputation: 453

Getting data from a dictionary Python

I'm using python dictionaries from counting the repeated objects from an array. I use a function obtained from this forum from counting the objects, and the obtained result is on the next format: {object: nºelements, ...). My issue is that the function doesn't return the dictionary objects differentiated by keys and I don't know how to get the objects.

count_assistence_issues = {x:list(assistances_issues).count(x) for x in list(assistances_issues)}
count_positive_issues = {x:list(positive_issues).count(x) for x in list(positive_issues)}
count_negative_issues = {x:list(negative_issues).count(x) for x in list(negative_issues)}
print(count_assistence_issues)
print(count_positive_issues)
print(count_negative_issues)

This is the output obtained:

{school.issue(10,): 1, school.issue(13,): 1}
{school.issue(12,): 1}
{school.issue(14,): 2}

And this is the output I need to obtain:

{{issue: school.issue(10,), count: 1},
{issue: school.issue(13,), count: 1}}

{{issue: school.issue(12,), count: 1}}

{{issue: school.issue(14,), count: 2}}

Anyone know how to differenciate by keys the elements of the array using the function? Or any other function for counting the repeated objects for obtaining a dictionary with the format {'issue': issue,'count': count) Thanks for reading!

Upvotes: 1

Views: 68

Answers (1)

Jason Chia
Jason Chia

Reputation: 1145

Given your input and output. I would consider the following.

1) Merge all your counts into a single dictionary

#assuming that what diffrentitaes your issues is a unique ID/key/value etc.
#meaning that no issues are subsets of the other. IF they are this will need some tweaking
issue_count = {}
issue_count.update(count_assistence_issues)
issue_count.update(count_positive_issues)
issue_count.update(count_positive_issues)

Getting the counts is then simply:

issue_count[school.issue(n,)]

The key is your array. If you want an alternative. You could make a list of keys or dict of your keys. You can make this as verbose as you want.

key_issues = {"issue1":school.issue(1,),"issue2":school.issue(2,)....}

This then allows you to call your counts by:

issue_count[key_issues["issue1"]]

If you want to use the "count" field. You would need to fix your counter to give you a dict of your issue with field count but that's another question.

Upvotes: 1

Related Questions