s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for skey in s:
for lkey in L:
if lkey == skey:
s.remove(skey)
print s
马春杰杰
浏览 2830回答 1
1回答
孤独的小猪
报错是因为在用for迭代集合s的时候,对集合s做了remove操作,更改了s的结构,导致报错,可以先深拷贝一个s,用拷贝出来的s做remove操作,代码如下:import copy
s = set(['Adam', 'Lisa', 'Paul'])
s_2 = copy.deepcopy(s)
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for skey in s:
for lkey in L:
if lkey == skey:
s_2.remove(skey)
print s_2