从for循环中创建多个列表值

时间:2019-11-18 18:43:54

标签: python pandas itertools

我正在尝试使用此伪代码创建具有多个值的列表: 对于范围为(0,3)的i,在字母i,i和i后面附加字母i,并返回包含这些值的列表

所需的输出:

first1,second1,third1,first2,second2,third3

我尝试为此使用itertools

list(itertools.chain.from_iterable(
    ('first'+str(i),'second'+str(i),'third'+str(i))
         for i in range(1,86)))

但是我收到消息:

TypeError: 'list' object is not callable

这是完整的错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-265-593da73e688e> in <module>
----> 5 col_names =list(itertools.chain.from_iterable(('first'+str(i),'second'+str(i),'third'+str(i)) for i in range(1,86)))

TypeError: 'list' object is not callable

1 个答案:

答案 0 :(得分:1)

尝试一下:

import itertools

x=["first", "second", "third"]
y=list(range(5))

z=list(map(lambda x: f"{x[1]}{x[0]}", itertools.product(y,x)))

print(z)

输出:

['first0', 'second0', 'third0', 'first1', 'second1', 'third1', 'first2', 'second2', 'third2', 'first3', 'second3', 'third3', 'first4', 'second4', 'third4']