已知有两支兵乓球队进行比赛,每队各出3人:
甲队有a,b,c三人,乙队有x,y,z三人,已抽签决定比赛名单
问题:有人向队员打听比赛名单。a说他不和X比,c说他不和x,z比,程序找出比赛对手
方法一:
1 list1 = ['a','b','c'] 2 list2 = [0,0,0] 3 for i in list1: 4 if i == "c": 5 list2[list1.index(i)] = 'y' 6 elif i == "a": 7 list2[list1.index(i)] = 'z' 8 else: 9 list2[list1.index(i)] = 'x' 10 11 for i in range(len(list1)): 12 print(list1[i],'VS', list2[i],end =' ')
a VS z b VS x c VS y
方法二:
1 list1 = ['a','b','c'] 2 for x in list1: 3 for y in list1: 4 for z in list1: 5 if (x!=y) and (y!=z) and (x!=z) and (x!='a') and (x!='c') and (z!='c'): 6 print('x VS %s ,y VS %s, z VS %s'%(x,y,z))
执行结果:
x VS b ,y VS c, z VS a