从python中的函数返回数字字典

use*_*042 -3 python dictionary hashtable

我想通过将列表的每个值分配给"喜欢"来返回元素字典,但我不断出现错误.这是我的代码.

def f([a,b,c]):
    d={a:'like'}
    for x in [a,b,c]:
        d[x]='like'
    return d
Run Code Online (Sandbox Code Playgroud)

Mic*_*ber 5

dict.fromkeys 是一个方便的构造函数:

>>> dict.fromkeys([1,2,3], 'like')
{1: 'like', 2: 'like', 3: 'like'}
Run Code Online (Sandbox Code Playgroud)