Is it possible to assign the same value to multiple keys in a dict object at once?

前端 未结 6 1892
时光取名叫无心
时光取名叫无心 2020-11-27 03:44

In Python, I need a dictionary object which looks like:

{\'a\': 10, \'b\': 20, \'c\': 10, \'d\': 10, \'e\': 20}

I\'ve been able to get th

6条回答
  •  暖寄归人
    2020-11-27 04:09

    There is one way that comes to mind. Still has the limitation of only having the same contents.

    The value of one key can't change another.

    key_map = {   
        'KEY_UP':move_up,
        'w':'KEY_UP',
    }
    

    for key in key_map:

    # loop over the `dict`
    
    if key_map[key] in key_map:
        # one key is the value of the other
    
        target_key = key_map[key]
        key_map[key] = key_map[target_key]
        # overwrite key white the ontents of the other
    

提交回复
热议问题