Summing While Projecting a Dictionary Onto a Sub-Tuple Key

CopyPasteIt

Update: I realize now that errors can occur when you scale down your code and then post a question. This is the same question but with the sample code matching the true situation. I also learned that you don't need the line continuation "\" when your python expression is ending in a comma ",".


I can certainly get my projection/sum dictionary by using for loops, but is there a more Pythonic method? If for loops must be used is there a 'best python practice'.

Also, if any of my wording is off please let me know the proper way to frame this using python terminology.

Here is the code:

import sys
from collections import defaultdict
D = defaultdict(int)
while True:
    D = { ('a', 'mike'):  1, 
          ('b', 'mike'):  2,
          ('c', 'mike'):  3, 
          ('b', 'tom'):   1, 
          ('e', 'mary'): 11,
          ('k', 'mike'):  2, 
          ('f', 'mike'):  1, 
          ('z', 'tom'):   1,  
        }
    #print (D)
    # how to create prD:
    prD_A = defaultdict(int)    
    prD_A = { 'mike': 9, 'tom': 2, 'mary': 11 }

#Answer supplied by Delirious Lettuce:

    prD_B = defaultdict(int)
    for k, v in D.items():
        _, name = k
        prD_B[name] += v

    #print(prD_B) 

    if prD_A == prD_B:
        print("Same result")

    sys.exit() # END PROGRAM        
G_M

You were asked in the comments whether or not your keys were supposed to be strings (of tuples) or tuples themselves. Since you didn't really answer, here are two versions which account for both possibilities.

# Keys as strings of tuples
In[2]: correct = {'mike': 9, 'tom': 2, 'mary': 11}
In[3]: my_dict = {
  ...:     "('a', 'mike')": 1,
  ...:     "('b', 'mike')": 2,
  ...:     "('c', 'mike')": 3,
  ...:     "('b', 'tom')": 1,
  ...:     "('e', 'mary')": 11,
  ...:     "('k', 'mike')": 2,
  ...:     "('f', 'mike')": 1,
  ...:     "('z', 'tom')": 1
  ...: }
In[4]: from ast import literal_eval
  ...: from collections import defaultdict
  ...: 
  ...: result = defaultdict(int)
  ...: for k, v in my_dict.items():
  ...:     _, name = literal_eval(k)
  ...:     result[name] += v
  ...: 
In[5]: result == correct
Out[5]: True

# Keys as actual tuples
In[6]: my_dict_2 = {
  ...:     ('a', 'mike'): 1,
  ...:     ('b', 'mike'): 2,
  ...:     ('c', 'mike'): 3,
  ...:     ('b', 'tom'): 1,
  ...:     ('e', 'mary'): 11,
  ...:     ('k', 'mike'): 2,
  ...:     ('f', 'mike'): 1,
  ...:     ('z', 'tom'): 1
  ...: }
In[7]: from collections import defaultdict
  ...: 
  ...: result_2 = defaultdict(int)
  ...: for k, v in my_dict_2.items():
  ...:     _, name = k
  ...:     result_2[name] += v
  ...: 
In[8]: result_2 == correct
Out[8]: True

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Return key corresponding to the tuple with smallest third value from dictionary

From Dev

When the key is a tuple in dictionary in Python

From Dev

Summing items within a Tuple

From Dev

In Swift can I use a tuple as the key in a dictionary?

From Dev

Sorting dictionary keys based on a value of the tuple associated with the key in swift

From Dev

summing nested dictionary entries

From Dev

Automapper query for projecting an anonymous type onto a viewmodel

From Dev

tuple as key to a dictionary says: 'tuple' object does not support item assignment

From Dev

Error summing values in a dictionary

From Dev

How to sort dictionary on first element of the key (tuple)

From Dev

Check whether a tuple key exists in dictionary in java

From Dev

Converting Dictionary to Dataframe with tuple as key

From Dev

projecting points onto a plane given by a normal and a point

From Dev

accessing dictionary key and value with one key in a tuple

From Dev

extracting a tuple from a dictionary with a key/tuple pair

From Dev

Projecting a point onto the intersection of n-dimensional spaces in Python

From Dev

Creating a Nested Dictionary While Converting to a Tuple

From Dev

Summing dictionary of lists values by key

From Dev

Projecting a line segment onto a polygon mesh

From Dev

get top items in a sorted dictionary for every sub-key with key as tuple

From Dev

Dictionary key not working as a type of Tuple<int[], int>

From Dev

Tuple or multi-key dictionary?

From Dev

Error summing values in a dictionary

From Dev

Python: update dictionary key with tuple values

From Dev

Linq GroupBy/Aggregation of Dictionary with Key that is a Tuple

From Dev

Tuple list as key of dictionary in Python

From Dev

Pandas: create a dictionary with a tuple as key

From Dev

Projecting 3D Model onto 2d plane

From Dev

Matching dictionary key tuple items in a comprehension

Related Related

  1. 1

    Return key corresponding to the tuple with smallest third value from dictionary

  2. 2

    When the key is a tuple in dictionary in Python

  3. 3

    Summing items within a Tuple

  4. 4

    In Swift can I use a tuple as the key in a dictionary?

  5. 5

    Sorting dictionary keys based on a value of the tuple associated with the key in swift

  6. 6

    summing nested dictionary entries

  7. 7

    Automapper query for projecting an anonymous type onto a viewmodel

  8. 8

    tuple as key to a dictionary says: 'tuple' object does not support item assignment

  9. 9

    Error summing values in a dictionary

  10. 10

    How to sort dictionary on first element of the key (tuple)

  11. 11

    Check whether a tuple key exists in dictionary in java

  12. 12

    Converting Dictionary to Dataframe with tuple as key

  13. 13

    projecting points onto a plane given by a normal and a point

  14. 14

    accessing dictionary key and value with one key in a tuple

  15. 15

    extracting a tuple from a dictionary with a key/tuple pair

  16. 16

    Projecting a point onto the intersection of n-dimensional spaces in Python

  17. 17

    Creating a Nested Dictionary While Converting to a Tuple

  18. 18

    Summing dictionary of lists values by key

  19. 19

    Projecting a line segment onto a polygon mesh

  20. 20

    get top items in a sorted dictionary for every sub-key with key as tuple

  21. 21

    Dictionary key not working as a type of Tuple<int[], int>

  22. 22

    Tuple or multi-key dictionary?

  23. 23

    Error summing values in a dictionary

  24. 24

    Python: update dictionary key with tuple values

  25. 25

    Linq GroupBy/Aggregation of Dictionary with Key that is a Tuple

  26. 26

    Tuple list as key of dictionary in Python

  27. 27

    Pandas: create a dictionary with a tuple as key

  28. 28

    Projecting 3D Model onto 2d plane

  29. 29

    Matching dictionary key tuple items in a comprehension

HotTag

Archive