3

I am very new to Python and programming, and am struggling to understand a problem. Any help is greatly appreciated.

I have a deque of values and categories:

deque([(3000.0, category1), (6000.0, category1), (8000.0, category2), (3000.0, category3), (7000.0, category3), (4500.0, category3)])

I am trying to use a dictionary to print the average of each value by category. For example, category1 = (3000 + 6000) / 2; category2 = 8000 / 1; category3 = (3000 + 7000 + 4500) / 3.

Is there a straightforward way to do this? So far, my only thought is to create a list of unique categories:

catetory_list = []
for i in deque:
    if i[1] not in category_list:
    category_list.append(i[1])

I think I am missing something important here, but can't seem to find the next step.

I found some similar issues here already, namely here and here, but I am struggling on how to make this fit for my question.

Community
  • 1
  • 1
N__
  • 43
  • 4
  • Welcome to Stack Overflow! You can [take the tour](http://stackoverflow.com/tour) first and learn [How to Ask a good question](http://stackoverflow.com/help/how-to-ask) and create a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example. That makes it easier for us to help you. – Stephen Rauch Feb 20 '17 at 21:49
  • @StephenRauch Ok, it sounds like my question is not very clear. I was really trying. :( I'll try to edit my post for clarity. – N__ Feb 20 '17 at 22:17
  • 1
    see: http://stackoverflow.com/q/42309909/7484636 – KernelPanic Feb 22 '17 at 21:24

1 Answers1

3

You were close, I think you need to construct a dict, with the values. And then average the values afterwards:

Sample Data:

my_dq = deque([
    (3000.0, 'category1'), (6000.0, 'category1'), (8000.0, 'category2'),
    (3000.0, 'category3'), (7000.0, 'category3'), (4500.0, 'category3')])

Code:

categories = {}
for i in my_dq:
    categories.setdefault(i[1], []).append(i[0])
categories = {k: sum(v) / len(v) for k, v in categories.items()}

Results:

{'category1': 4500.0, 'category3': 4833.3333, 'category2': 8000.0}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • ... with the obvious optimization to sum the values as you go, and keep a total number of items (which may/may not be worth it). – csl Feb 20 '17 at 22:55