Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何将 k,v 转换为 {k:v},尽可能短且 Python 化?
你有一个变量v和一个变量k,你想制作一个dict?{k: v}完全是pythonic
v
k
dict
{k: v}
您可以将元组转换为字典。尝试这个:
>>> dict([('k', 'v')]) {'k': 'v'}
我假设k, v这里是您在某个变量中拥有的元组,我将其称为x. 这里有几个选项:
k, v
x
x = ('k', 'v') d = dict([x])
或者...
d = {x[0]: x[1]}