python中的循环函数,为每组数据生成单独的列表
因此,我有以下几点:python中的循环函数,为每组数据生成单独的列表,python,list,loops,Python,List,Loops,因此,我有以下几点: import PROGRAMS as prg testlist = [] y=[1,2,3,4,5] functions = [prg.test1, prg.test2, prg.test3] for func in functions: for j in y: x = j*2 z = func(x) testlist.append(z) print testlist #####PROGRAMS def tes
import PROGRAMS as prg
testlist = []
y=[1,2,3,4,5]
functions = [prg.test1, prg.test2, prg.test3]
for func in functions:
for j in y:
x = j*2
z = func(x)
testlist.append(z)
print testlist
#####PROGRAMS
def test1(x):
x=x**2
return x
def test2(x):
x=x**3
return x
def test3(x):
x=x+10
return x
现在我想为每个函数生成一个单独的测试列表。也就是说,有一个列表,其中包含使用test1运行循环的所有数据,然后有一个单独的列表,其中包含test2等的数据
理想情况下,我不想定义3个单独的列表testlist1、testlist2等,而是有一个系统,其中列表根据列表“函数”的长度生成,并以某种方式实现到我已经拥有的列表中
提前谢谢大家,,
Sven.要创建每个函数的单独值列表:
outlists = []
for func in functions:
thislist = []
for j in y:
thislist.append(func(j*2))
outlists.append(thislist)
大纲视图
将包含每个函数的一个列表
您还可以使用嵌套列表理解来执行此操作:
outlists = [[func(j*2) for j in y] for func in functions]
但这超出了一些人的舒适度。对不起,英语不是我的第一语言,我是说len(函数)。谢谢,这太完美了——对不起,我的英语不好!