Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中基于另一个列表的元素迭代列表_Python_Iteration - Fatal编程技术网

在python中基于另一个列表的元素迭代列表

在python中基于另一个列表的元素迭代列表,python,iteration,Python,Iteration,关于迭代有很多答案,但我没有找到我想要的。 因此,情况如下: 我有两个不同长度的列表。但l1只能是,你的初始化中有一些奇怪的括号,可能想先检查一下拼写错误。接下来,您需要=而不是=进行比较。最后,根本不清楚你想做什么。您甚至使用了l3的元素来初始化l3,这毫无意义。l2与l1有什么关系?在你的最后一个l3中,它似乎只是使用了l2中的(0,1)、(2,3)、(4,5)等索引对,但是你说它取决于l1中的值。所以是的,对不起,我编辑了我的帖子。现在可以理解了吗?我希望如此。”我想在下面检查一下我的回答

关于迭代有很多答案,但我没有找到我想要的。 因此,情况如下:
我有两个不同长度的列表。但l1只能是,你的初始化中有一些奇怪的括号,可能想先检查一下拼写错误。接下来,您需要
=
而不是
=
进行比较。最后,根本不清楚你想做什么。您甚至使用了
l3
的元素来初始化
l3
,这毫无意义。
l2
l1
有什么关系?在你的最后一个
l3
中,它似乎只是使用了
l2
中的
(0,1)、(2,3)、(4,5)等索引对,但是你说它取决于
l1
中的值。所以是的,对不起,我编辑了我的帖子。现在可以理解了吗?我希望如此。”我想在下面检查一下我的回答
l1 = [2, 3, 1, 2, 2]
l2 = [12, 4, 5, 2, 9, 33, 5, 8, 4, 5, 1, 7, 3]
l3 = []
l3 = [(l2[0] + l2[1], l2[2] + l2[3] + l2[4], l2[5], l3[6] + l3[7]...)]
if l1[0] == 2:
Add the value of the sum of (the 2 first values of l2) in l3
if l1[0] == 3:
Add the value of the sum of (the next 3 values of l2) in l3
i = 0
for v in l1:
    for k in l2:
        sum(l2[i])
    i += 1
l1 = [2, 3, 1, 2, 2]
l2 = [12, 4, 5, 2, 9, 33, 5, 8, 4, 5, 1, 7, 3]
l3 = []

assert (len(l2) >= sum(l1))

i = 0
for e in l1:
    l3.append(sum(l2[i:i+e]))
    i += e

print (l3)
# [16, 16, 33, 13, 9]