Browse lists within a python dictionary

0

If I have this dictionary with python :

{'143534':[23,12,45],'123765':[33,89,90],'148987':[98,65,80]}

I need to place a matrix like this:

23,33,98
12,89,65
45,90,80

Any suggestions?

    
asked by Jorge Gonzalez 05.07.2018 в 00:09
source

1 answer

2

You can use zip :

>>> d = {'143534': [23, 12, 45], '123765': [33, 89, 90], '148987':[98, 65, 80]}
>>> d.values()
[[23, 12, 45], [33, 89, 90], [98, 65, 80]]
>>> zip(*d.values())
[(23, 33, 98), (12, 89, 65), (45, 90, 80)]

With the values method you get the values for each dictionary element. Now, as zip expects a certain amount of iterables (lists or tuples), we are passing each one using * . That is, what is happening to zip is:

>>> zip([23, 12, 45], [33, 89, 90], [98, 65, 80])
[(23, 33, 98), (12, 89, 65), (45, 90, 80)]
    
answered by 05.07.2018 в 00:26