can you write a str.replace() using dictionary values in Python?

后端 未结 10 868
小鲜肉
小鲜肉 2020-12-03 00:20

I have to replace the north, south, etc with N S in address fields.

If I have

list = {\'NORTH\':\'N\',\'SOUTH\':\'S\',\'EAST\':\'E\',\'WEST\':\'W\'}         


        
10条回答
  •  时光说笑
    2020-12-03 01:23

    you are close, actually:

    dictionary = {"NORTH":"N", "SOUTH":"S" } 
    for key in dictionary.iterkeys():
        address.upper().replace(key, dictionary[key])
    

    Note: for Python 3 users, you should use .keys() instead of .iterkeys():

    dictionary = {"NORTH":"N", "SOUTH":"S" } 
    for key in dictionary.keys():
        address.upper().replace(key, dictionary[key])
    

提交回复
热议问题