1、简介
最近有多个判断条件,脑子有点拎不清关系后便跑了几个栗子康康
2、例子2个数组对应1个位置处不计算
- 如 1和1
- 其余两两计算
test1 = [1,2,3,4]
test2 = [1,2,3,4]
for a in test1:
for b in test2:
if a != 1 or b != 1:
print(a,'--',b)
#等价
test1 = [1,2,3,4]
test2 = [1,2,3,4]
for a in test1:
for b in test2:
if (a == 1 and b == 1) or (a == 2 and b == 2):
continue
else:
print(a,'--',b)
结果
1 -- 2
1 -- 3
1 -- 4
2 -- 1
2 -- 2
2 -- 3
2 -- 4
3 -- 1
3 -- 2
3 -- 3
3 -- 4
4 -- 1
4 -- 2
4 -- 3
4 -- 4
2个数组对应2个位置处不计算
- 如 1和1+2和2
- 其余两两计算
test1 = [1,2,3,4]
test2 = [1,2,3,4]
for a in test1:
for b in test2:
if (a != 1 or b != 1) and (a != 2 or b != 2):
print(a,'--',b)
结果
1 -- 2
1 -- 3
1 -- 4
2 -- 1
2 -- 3
2 -- 4
3 -- 1
3 -- 2
3 -- 3
3 -- 4
4 -- 1
4 -- 2
4 -- 3
4 -- 4