I have the following data:
data = [['AB', 'BS, BT'], ['AH', 'AH'], ['AS', 'AS, GS']]
I would like to iterate through the lists of list to produce a list of tuples.
new_data = [('AB', 'BS'), ('AB', 'BT'), ('AH', 'AH'), ('AS', 'AS') ('AS', 'GS')]
I was thinking about using the zip() function, but wasn't sure if I was using the right logic.
zip
wouldn't be my first choice. My first choice would be itertools.product
and itertools.chain
In [103]: data = [['AB', 'BS, BT'], ['AH', 'AH'], ['AS', 'AS, GS']]
In [104]: [list(itertools.product([d[0]], d[1].split(','))) for d in data]
Out[104]: [[('AB', 'BS'), ('AB', ' BT')], [('AH', 'AH')], [('AS', 'AS'), ('AS', ' GS')]]
In [105]: list(itertools.chain.from_iterable([list(itertools.product([d[0]], d[1].split(','))) for d in data]))
Out[105]: [('AB', 'BS'), ('AB', ' BT'), ('AH', 'AH'), ('AS', 'AS'), ('AS', ' GS')]
Hope this helps
This can be done easily enough with itertools.repeat()
. We use this to repeat the first item for each of the other items, which we get by splitting on ","
, then zipping up to generate our tuples. We then use itertools.chain.from_iterable()
to generate a single list.
>>> import itertools
>>> data = [['AB', 'BS, BT'], ['AH', 'AH'], ['AS', 'AS, GS']]
>>> for item in itertools.chain.from_iterable(zip(itertools.repeat(first), second.split(",")) for first, second in data):
... print(item)
...
('AB', 'BS')
('AB', ' BT')
('AH', 'AH')
('AS', 'AS')
('AS', ' GS')
using zip()
:
In [32]: data
Out[32]: [['AB', 'BS, BT'], ['AH', 'AH'], ['AS', 'AS, GS']]
In [33]: [zip([x[0]]*len(x[1].split(",")),x[1].split(",")) for x in data]
Out[33]: [[('AB', 'BS'), ('AB', ' BT')], [('AH', 'AH')], [('AS', 'AS'), ('AS', ' GS')]]
use chain()
to get the expected output:
In [34]: lis=[zip([x[0]]*len(x[1].split(",")),x[1].split(",")) for x in data]
In [35]: list(chain(*lis))
Out[35]: [('AB', 'BS'), ('AB', ' BT'), ('AH', 'AH'), ('AS', 'AS'), ('AS', ' GS')]
using izip_longest
, with fillvalue
equal to first element of each sublist:
In [47]: from itertools import chain,izip_longest
In [48]: lis=[tuple(izip_longest([x[0]],x[1].split(","),fillvalue=x[0])) for x in data]
In [49]: lis
Out[49]: [(('AB', 'BS'), ('AB', ' BT')), (('AH', 'AH'),), (('AS', 'AS'), ('AS', ' GS'))]
In [50]: list(chain(*lis))
Out[50]: [('AB', 'BS'), ('AB', ' BT'), ('AH', 'AH'), ('AS', 'AS'), ('AS', ' GS')]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.