-1

This is my code:

d={(i,j):i+j for i in range(1,7) for j in range(1,7)}

from collections import defaultdict 
dinv = defaultdict(list) 
for i,j in d.values(): 
    dinv[j].append(i)

X={i:len(j)/36. for i,j in dinv.iteritems() } 
print (X)

This is my traceback error.

TypeError                                 Traceback (most recent call last)
<ipython-input-20-d6b279f4a6a8> in <module>
      4 from collections import defaultdict
      5 dinv = defaultdict(list)
----> 6 for i,j in d.values():
      7     dinv[j].append(i)
      8 

TypeError: cannot unpack non-iterable int object
azro
  • 53,056
  • 7
  • 34
  • 70
  • The values are a list of int so you cannot unpack to i and j, What do you want to do ? The key is a 2-element tuple by the way – azro Jun 04 '20 at 11:38

2 Answers2

1

To iterate on key and values, you need to use .items() also you'd better use better names for your variables

dinv = defaultdict(list) 
for pair,v_sum in d.items(): 
    dinv[v_sum].append(pair)

Next you'll that defaultdict has no attribute iteritems, use items too

X = {key:len(values)/36. for key,values in dinv.items()} 
azro
  • 53,056
  • 7
  • 34
  • 70
0

You probably meant d.items() or d.keys() (or just d) instead of d.values() in for i,j in d.values():.

norok2
  • 25,683
  • 4
  • 73
  • 99