简体   繁体   中英

Making combinations of 1st element in list and other elements of the same list in python

I have a requirement with python to take combinations with 1st element in a list as common and take other combinations.

list1=['EXCHANGE_US_M','1021,2360','1256,14536,5429','AED589L','DOL895']

I have a list as above and I want to have combinations as below.

'EXCHANGE_US_M/0','1021','1256','AED589L','DOL895',
'EXCHANGE_US_M/1','1021','14536','AED589L','DOL895',
'EXCHANGE_US_M/2','1021','15429','AED589L','DOL895',
'EXCHANGE_US_M/3','2360','1256','AED589L','DOL895',
'EXCHANGE_US_M/4','2360','14536','AED589L','DOL895',
'EXCHANGE_US_M/5','2360','5429','AED589L','DOL895',
>>> from itertools import product
... 
... list1 = ['EXCHANGE_US_M', '1021,2360', '1256,14536,5429', 'AED589L', 'DOL895']
... 
... for i, p in enumerate(product(*[elem.split(',') for elem in list1])):
...     print(('{}/{}'.format(p[0], i), *p[1:]))
... 
('EXCHANGE_US_M/0', '1021', '1256', 'AED589L', 'DOL895')
('EXCHANGE_US_M/1', '1021', '14536', 'AED589L', 'DOL895')
('EXCHANGE_US_M/2', '1021', '5429', 'AED589L', 'DOL895')
('EXCHANGE_US_M/3', '2360', '1256', 'AED589L', 'DOL895')
('EXCHANGE_US_M/4', '2360', '14536', 'AED589L', 'DOL895')
('EXCHANGE_US_M/5', '2360', '5429', 'AED589L', 'DOL895')

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM