Reputation: 331
When I run the following code, it doesn't modify the list generated using 'deepcopy', i.e. I get 'mt1' unchanged. If I applied the same code on 'mt', I get the desired result!
def subDic(f):
w = random.randint(2, int(0.7*len(f)))
s = random.randint(0, len(f)-w)
idSub = {}
for i in range(s, s+w):
idSub[i] = f[i]
return idSub
ft = [(2,3), (4,8), (1,0), (7,1)]
mt = copy.deepcopy(ft)
random.shuffle(mt)
mt1 = copy.deepcopy(mt)
ftDic = subDic(ft)
for e in mt1:
if e in ftDic.values():
mt1.remove(e)
Upvotes: 0
Views: 74
Reputation: 3071
You shouldn't iterate over mt1
while removing its values.
Try something like this:
def subDic(f):
w = random.randint(2, int(0.7*len(f)))
s = random.randint(0, len(f)-w)
idSub = {}
for i in range(s, s+w):
idSub[i] = f[i]
return idSub
ft = [(2,3), (4,8), (1,0), (7,1)]
mt = copy.deepcopy(ft)
random.shuffle(mt)
mt1 = copy.deepcopy(mt)
ftDic = subDic(ft)
for e in ftDic.values():
mt1.remove(e)
Upvotes: 2