请问这个哪里错了?
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for x in L:
for y in s:
if x=y:
s.remove(y)
else:
s.add(x)
print s
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for x in L:
for y in s:
if x=y:
s.remove(y)
else:
s.add(x)
print s
2018-06-21
无法一边遍历一边删除。
我是复制了set的内容 达到你程序可以运行的程度,但是答案并不正确:
s = set(['Adam', 'Lisa', 'Paul']) d = ['Adam', 'Lisa', 'Paul'] L = ['Adam', 'Lisa', 'Bart', 'Paul'] for x in L: for y in d: if x == y: s.remove(y) else: s.add(x) print s
x = A,L,B,P ; y = A,L,P
1:x = A : y=A, s = L, P ; y = L , s = A, L, P ; y = P , s = A, L, P
2:x = L : y = A, s = A, L, P; y =L , s = A, P ; y = P , s = A, L, P
3:x = B : y = A, s = A, L, B, P ; y = L , s = A, L, B, P ; y = P , s = A, L, B, P
4:x = P : y = A , s = A, L, B, P; y = L , s = A, L, B, P; y = P , s = A, L, B
举报