您当前的位置: 首页 >  Python

静静喜欢大白

暂无认证

  • 3浏览

    0关注

    521博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Python】for和if使用

静静喜欢大白 发布时间:2021-07-29 21:08:09 ,浏览量:3

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

关注
打赏
1510642601
查看更多评论
立即登录/注册

微信扫码登录

0.0384s