Simple syntax error in Python if else dict comprehension

后端 未结 4 459
野性不改
野性不改 2020-12-17 07:00

I have a set and dictionary and a value = 5

v = s = {\'a\', \'b\', \'c\'}
d = {\'b\':5 //<--new value}

If the key \'b\' in dictionary d

4条回答
  •  臣服心动
    2020-12-17 07:46

    This should solve your problem:

    >>> dict((k, d.get(k, 0)) for k in s)
    {'a': 0, 'c': 0, 'b': 5}
    

提交回复
热议问题