Python:匹配每个多列表中的多个变量

时间:2017-09-17 04:50:12

标签: python

我可以知道如何将多个字符串与多个列表匹配吗?我运行了代码,但它没有给我'BEC'作为结果,为什么?

a = ["A", "B", "C"]
b = ["AB","BB"]
c = ["ABC","BCD","BEC"]

av, bv, cv = 'B', 'AB', 'BEC'

for _a, _b, _c in zip(a,b,c):
    if _a == av and _b == bv and _c == cv:
        print cv # 'BEC'

2 个答案:

答案 0 :(得分:0)

最简单的方法:

if a in a and bv in b and cv in c: print cv

all方式使用:

all((av in a, bv in b, cv in c))

为了娱乐而改编的all方式:

_lists = [a,b,c]
all(value in _lists[i] for i, value in enumerate([av,bv,cv]))

答案 1 :(得分:0)

from itertools import product
a = ["A", "B", "C"]
b = ["AB","BB"]
c = ["ABC","BCD","BEC"]

av, bv, cv = 'B', 'AB', 'BEC'
for _a, _b, _c in product(a,b,c):
    if _a == av and _b == bv and _c == cv:
        print cv # 'BEC'