0

Here is my dictionary of n items.

{
"proceed": [[6,46] , [7,67], [12,217], [67,562], [67,89]],
"concluded":  [[6,46] , [783,123], [121,521], [67,12351], [67,12351]],
...

}

imagine a dictionary s.t. like that with n keys and items which are two dimensional arrays.

I want to intercept all of them and take the result as [6,46]

I tried s.t. like that :

result=set.intersection(*map(set,output.values()))

however it got error because of items are two dimensinal array.

Can someone please help me how to do that ?

Thanks.

2 Answers2

3

So... sets don't work for lists because lists are not hashable. Instead you'll have to make them sets of tuples like so:

result = set.intersection(*({tuple(p) for p in v} for v in output.values()))

Edit: works in py version >= 2.7

FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
0

Completely agree with answer of @FHTMitchell but here's a bit of more explanation with example of why you can't get unique set with list and get TypeError: unhashable type

Consider below values:

x = {'concluded': [[6, 46], [783, 123], [121, 521], [67, 12351], [67, 12351]],
 'proceed': [[6, 46], [7, 1], [12, 217], [67, 562], [67, 89]]}
y = {'concluded': ((6, 46), (67, 12351), (121, 521), (783, 123)),
 'proceed': ((6, 46), (7, 1), (12, 217), (67, 89), (67, 562))}

x is the dictionary containing list of list as values; the main thing to note is that value of keys are stored as list which is mutable; but in y it's tuple of tuples or you may keep it as set which is not mutable

Now consider some how you managed to get your desire output [6,46] but if you notice it's a list contains some elements stored in a list so if you change the values as below:

x['proceed'][0][0] = 9  

it will change your value [6, 46] to [9,46] in concluded key and now your output may or may not change which depends on how you iterated and stored it.

Gahan
  • 4,075
  • 4
  • 24
  • 44