gpt4 book ai didi

python - 在 python 中组合列表中的第一个元素和同一列表的其他元素

转载 作者:太空宇宙 更新时间:2023-11-03 14:01:13 32 4
gpt4 key购买 nike

我对 python 有一个要求,将与列表中第一个元素的组合视为常见组合,并采用其他组合。

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

我有一个上面的列表,我想要如下的组合。

'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')

关于python - 在 python 中组合列表中的第一个元素和同一列表的其他元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49002224/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com