replacing any character that is not 'l', 'r', 'j', 's'

前端 未结 1 507
执念已碎
执念已碎 2021-01-29 00:47

I am creating a script that translates certain characters with directions, and any character that is not one of the known characters is replaced with \"Aaaaah!\".



        
相关标签:
1条回答
  • 2021-01-29 01:00

    Use dict and dict.get:

    my_dict = {'r': 'right', 'l': 'left', 'j': 'jump', 's': 'straight'}
    
    # string = input('Terrain: ')
    string = 'rljsZ' # For test purpose
    
    new_string = ''.join(map(lambda x:my_dict.get(x, 'Aaaaah!'), string))
    print(new_string)
    

    Output:

    'rightleftjumpstraightAaaaah!'
    
    0 讨论(0)
提交回复
热议问题