第一种情况是要对三个表同时进行迭代,第二种情况是依次对三个表进行迭代
#!/usr/bin/env python #coding:utf-8 #@Author:Andy # Date: 2017/6/11 from random import randint # 3 lists l1 = [randint(1,100) for _ in range(1,10)] l2 = [randint(1,100) for _ in range(1, 10)] l3 = [randint(1,100) for _ in range(1, 10)] # 将三个表中的对应顺序的项相加,合并成一张表 res = [] # for a, b, c in zip(l1, l2, l3): # total = a+b+c # res.append(total) [res.append(a+b+c) for a,b,c in zip(l1, l2, l3)] print(res) # 统计三个表中,大于60的数, # 此时就需要对三个表依次迭代 # 虽然列表可以直接进行相加、拼接但不建议这样做 res1 = [] from itertools import chain # for i in chain(l1, l2, l3): # if i > 60: # res1.append(i) [res1.append(i) for i in chain(l1, l2, l3) if i > 60] print(res1)
时间: 2024-10-14 16:11:11