python product避免多重循环

2019-05-14 16:46:31 浏览数 (1)

通过 from itertools import product 避免多重循环,提高可读性

代码语言:javascript复制
#!/usr/bin/env python
# coding=utf-8
# author: wz
# mail: 277215243@qq.com
# datetime:2017/5/3 6:32 PM
# web: https://www.bthlt.com
from itertools import product
a_list=["a1","a2","a3"]
b_list=["b1","b2","b3"]
c_list=["c1","c2","c3"]

for a in a_list:
    for b in b_list:
        for c in c_list:
            if "2" in a and "2" in b and "2" in c:
                print a,b,c

#用product实现,避免多重循环
for a,b,c in product(a_list,b_list,c_list):
    if "2" in a and "2" in b and "2" in c:
        print a, b, c

0 人点赞