Skip to main content
  1. About
  2. For Teams
Asked
Viewed 70 times
1

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!

5
  • Just to note this is an inefficient way of getting counts... what with the list conversions and counting each time... So you should look to change those to count_assitence_issues = collections.Counter(assistances_issues)
    Jon Clements
    –  Jon Clements
    2020-02-25 09:12:33 +00:00
    Commented Feb 25, 2020 at 9:12
  • 2
    I'm not sure I understand your output example... Did you intend to show you wanted a list of dicts as your current output shows you want a set of dicts and since dicts aren't hashable - you can't do that...
    Jon Clements
    –  Jon Clements
    2020-02-25 09:15:27 +00:00
    Commented Feb 25, 2020 at 9:15
  • I need to get the objects with the format {object, counter} for then obtaining the data like this: dict[object], dict[counter] @JonClements
    arevilla009
    –  arevilla009
    2020-02-25 09:18:19 +00:00
    Commented Feb 25, 2020 at 9:18
  • The Counter() method you posted returns the same format of the dictionary. I need to obtain the data of it like dict[key].
    arevilla009
    –  arevilla009
    2020-02-25 09:19:03 +00:00
    Commented Feb 25, 2020 at 9:19
  • What's stopping you doing that... I don't see how the output you've provided reflects that... you can just use whatever[key] on the object you have?
    Jon Clements
    –  Jon Clements
    2020-02-25 09:20:37 +00:00
    Commented Feb 25, 2020 at 9:20

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.