简体   繁体   中英

Python - Loop Through List and Remove from Dictionary

My goal is to simply loop through a string, look up each character in the keys of a dictionary, and remove that key. This is what I have now:

def m(z,word):
    for char in word:
        if char in z:
            z = z.pop(char)      
        else:
            return False
    print True
    return z

print m({'m':1,'y':2,'z':3},'my')

I understand the error "argument of type 'int' is not iterable," but I'm not clear on the most straightforward way of correcting this.

z.pop(char) removes the key char and returns the corresponing value. Replace

z = z.pop(char)  

with

z.pop(char)  

or use del z[char] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM