#3、题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。#已抽签决定比赛名单。有人向队员打听比赛的名单。#a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。A = ['c','a','b']B = ['x','y','z']for i in range(3): n = A[i] for j in range(len(B)): m = B[j] if((n=='a' and m=='x') or(n=='c' and m=='x') or(n=='c' and m=='z')): continue print(n,m) B.remove(m)
1 回答
已采纳
产品经理不是经理
TA贡献481条经验 获得超143个赞
# 循环中删除了元素,list长度变化 # 方法1:拷贝一份B,循环拷贝的,操作原始的 A = ['c','a','b'] B = ['x','y','z'] for i in range(3): n = A[i] for j in B[:]: m = j if((n=='a' and m=='x') or(n=='c' and m=='x') or(n=='c' and m=='z')): continue print(n,m) B.remove(m) # 方法2:倒序遍历 A = ['c','a','b'] B = ['x','y','z'] for i in range(3): n = A[i] for j in range(len(B)-1,-1,-1): m = B[j] if((n=='a' and m=='x') or(n=='c' and m=='x') or(n=='c' and m=='z')): continue print(n,m) B.remove(m)
添加回答
举报
0/150
提交
取消