gpt4 book ai didi

python - 从列表创建元组列表

转载 作者:行者123 更新时间:2023-12-01 06:01:30 25 4
gpt4 key购买 nike

我有一个列表,例如:

data=['January', 'February', 'March',
2007, 2008, 2009,
'value1', 'value2', 'value3']

现在我需要创建以下格式的元组列表

[('January', 2007,'value1'),('February', '2007','value2'),('March', 2007,'value3'),
('January', 2008,'value1'),('February', 2008,'value2'),('March', 2008,'value3'),
('January', 2009,'value1'),('February', 2009,'value2'),('March', 2009,'value3')]

请告诉我这个概念

最佳答案

类似于:

>>> l1 = ['January', 'February', 'March']
>>> l2 = [2007,2008,2009]
>>> l3 = ['value1','value2','value3']
>>> for year in l2:
... print zip(l1, [year] * len(l1), l3)
...
[('January', 2007, 'value1'), ('February', 2007, 'value2'), ('March', 2007, 'value3')]
[('January', 2008, 'value1'), ('February', 2008, 'value2'), ('March', 2008, 'value3')]
[('January', 2009, 'value1'), ('February', 2009, 'value2'), ('March', 2009, 'value3')]

将给定列表拆分为树 block 作为 OP 的练习。

关于python - 从列表创建元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10211243/

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