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

后端 未结 10 871
小鲜肉
小鲜肉 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:02

    address = "123 north anywhere street"
    
    for word, initial in {"NORTH":"N", "SOUTH":"S" }.items():
        address = address.replace(word.lower(), initial)
    print address
    

    nice and concise and readable too.

提交回复
热议问题