从变量列表创建字典 [英] create dictionary from list of variables

查看:92
本文介绍了从变量列表创建字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种无需显式编写键即可创建字典的方法
我想创建获取数字变量的函数,并创建其中变量名称为键且其值为变量值的字典

I'm looking for a way to create dictionary without writing the key explicitly I want to create function that gets number variables, and create dictionary where the variables name are the keys and their value is the variable value

而不是编写以下函数:

def foo():
    first_name = "daniel"
    second_name = "daniel"
    id = 1
    return {'first_name':first_name, 'second_name':second_name}

我想获得与函数相同的结果:

i want to get the same result with function :

create_dict_from_variables(first_name, second_name)

有这样做吗?

推荐答案

您不能这样做。

您的函数可以定义为:

Your function could be defined as:

def create_dict_from_variables(first_name, second_name):
    return something_from(first_name, second_name)

一个d您可以调用此函数:

and you can call this function:

create_dict_from_variables('abcd', 'efgh')

这两个参数'abcd''efgh'不是命名变量。

These two arguments 'abcd' and 'efgh' are not named variables.

这篇关于从变量列表创建字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆