문제

I am new to python and I am trying to make a dictionary using tuples as keys and a nested list as multiple values.

The list is nested in triplets; [[[Isolation source],[host],[country]]...etc]

example below:

value_list = [[['NaN'], ['sponge'], ['Palau']], [['skin'], ['fish'], ['Cuba']], [['claw'], ['crab'], ['Japan: Aomori, Natsudomari peninsula']]....]

And the tuple of keys;

key_tuple = ('AB479448', 'AB479449', 'AB602436',...)

Hence, I would like the output to look like this;

dict = {'AB479448': [NaN, sponge, Palau], 'AB479449': [skin, fish, Cuba], 'AB602436': [claw, crab, Japan: Aomori, Natsudomari peninsula]

I have tried a few different solution but non that I could make work... e.g. dictionary comprehension;

dict = { i: value_list for i in key_tuple }

The above gives me this (uses the different keys but associates the same value to each of them);

{'AB479448': [[[NaN, sponge, Palau]]], 'AB479449': [[[NaN, sponge, Palau]]], 'AB602436': [[[NaN, sponge, Palau]]]...etc..}

Would appreciate any pointers... thanks!

도움이 되었습니까?

해결책

You can use itertools.chain.from_iterable, itertools.izip (or zip) and a dict comprehension:

>>> from itertools import chain, izip
>>> value_list = [[['NaN'], ['sponge'], ['Palau']], [['skin'], ['fish'], ['Cuba']], [['claw'], ['crab'], ['Japan: Aomori, Natsudomari peninsula']]]
>>> key_tuple = ('AB479448', 'AB479449', 'AB602436')
>>> {k: list(chain.from_iterable(v)) for k, v in izip(key_tuple, value_list)}
{'AB479449': ['skin', 'fish', 'Cuba'],
 'AB479448': ['NaN', 'sponge', 'Palau'],
 'AB602436': ['claw', 'crab', 'Japan: Aomori, Natsudomari peninsula']}

다른 팁

Using zip and iter you can create your desired output dictionary as follows

value_list = [[['NaN'], ['sponge'], ['Palau']], [['skin'], ['fish'], ['Cuba']], [['claw'], ['crab'], ['Japan: Aomori, Natsudomari peninsula']]]
key_tuple = ('AB479448', 'AB479449', 'AB602436')

dict( (key,[[list(value)]]) for key,value in zip(key_tuple, zip(*(iter(t[0] for v in value_list for t in v),)*3)))

Out[16]: {'AB479448': [[['NaN', 'sponge', 'Palau']]], 'AB479449': [[['skin', 'fish', 'Cuba']]],'AB602436': [[['claw', 'crab', 'Japan: Aomori, Natsudomari peninsula']]]}

if the desired number of element in the list key changes you can substitute the 3 for the new length value.


It was really fun making this.

Here is a solution using itertools.chain.from_iterable and dictionary comprehension:

from itertools import chain
{keys[i]:list(chain.from_iterable(contents)) for i, contents in enumerate(my_list)}

This, is equal to:

from itertools import chain
for i, contents in enumerate(my_list): #get [['skin'], ['fish'], ['Cuba']]
    result[keys[i]] = list(chain.from_iterable(contents))

Demo:

>>> from itertools import chain
>>> my_list = [[['NaN'], ['sponge'], ['Palau']], [['skin'], ['fish'], ['Cuba']], [['claw'], ['crab'], ['Japan: Aomori, Natsudomari peninsula']]]
>>> keys = ('AB479448', 'AB479449', 'AB602436')
>>> {keys[i]:list(chain.from_iterable(contents)) for i, contents in enumerate(my_list)}
{'AB479449': ['skin', 'fish', 'Cuba'], 'AB479448': ['NaN', 'sponge', 'Palau'], 'AB602436': ['claw', 'crab', 'Japan: Aomori, Natsudomari peninsula']}
>>> 

Hope this helps!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top