Python 你能用一个函数创建字典吗?
作为一个完全的初学者,我对这个函数感到非常自豪。虽然我相信可能有一种更简单、更像蟒蛇的方法来做同样的事情:Python 你能用一个函数创建字典吗?,python,python-3.x,dictionary,key,python-3.6,Python,Python 3.x,Dictionary,Key,Python 3.6,作为一个完全的初学者,我对这个函数感到非常自豪。虽然我相信可能有一种更简单、更像蟒蛇的方法来做同样的事情: Genes = ['Gen1', 'Gen2', 'Gen3'] Mutations = ['Gen1.A', 'Gen1.B', 'Gen2.A', 'Gen3.A', 'Gen3.B', 'Gen3.C'] def RawDict(keys, values): dictKeys = [] dictValues = [] for key in keys:
Genes = ['Gen1', 'Gen2', 'Gen3']
Mutations = ['Gen1.A', 'Gen1.B', 'Gen2.A', 'Gen3.A', 'Gen3.B', 'Gen3.C']
def RawDict(keys, values):
dictKeys = []
dictValues = []
for key in keys:
keyVal = []
for value in values:
if value.find(key) == -1:
pass
else:
keyVal.append(value)
dictKeys.append(key)
dictValues.append(keyVal)
return zip(dictKeys, dictValues)
GenDict = dict(RawDict(Genes, Mutations))
print(GenDict)
上面的功能是一种相当复杂的(我认为)在键(基因)中放置几个值(突变)的方法。但是我想知道我是否可以调整它,这样我就可以通过这样做得到一本字典:
dict(GenDict, Genes, Mutations)
print(GenDict)
我的难题是,当我在函数中使用dict时,这将不起作用:
Genes = ['Gen1', 'Gen2', 'Gen3']
Mutations = ['Gen1.A', 'Gen1.B', 'Gen2.A', 'Gen3.A', 'Gen3.B', 'Gen3.C']
def fullDict(dictName, keys, values):
dictKeys = []
dictValues = []
for key in keys:
keyVal = []
for value in values:
if value.find(key) == -1:
pass
else:
keyVal.append(value)
dictKeys.append(key)
dictValues.append(keyVal)
dictName = dict(RawDict(Genes, Mutations))
fullDict(GenDict, Genes, Mutations)
print(GenDict)
由于GenDict没有定义,上述方法就行不通。我假设您希望“Gen”通过其包含的数值进行存储
Genes = ['Gen1', 'Gen2', 'Gen3']
Mutations = ['Gen1.A', 'Gen1.B', 'Gen2.A', 'Gen3.A', 'Gen3.B', 'Gen3.C']
the_dict = {i:[] for i in Genes}
for i in Mutations:
new_val = i.split(".")
the_dict[new_val[0]].append(i)
print(the_dict)
输出:
{'Gen2': ['Gen2.A'], 'Gen3': ['Gen3.A', 'Gen3.B', 'Gen3.C'], 'Gen1': ['Gen1.A', 'Gen1.B']}
我假设您希望“Gen”由它包含的数值存储
Genes = ['Gen1', 'Gen2', 'Gen3']
Mutations = ['Gen1.A', 'Gen1.B', 'Gen2.A', 'Gen3.A', 'Gen3.B', 'Gen3.C']
the_dict = {i:[] for i in Genes}
for i in Mutations:
new_val = i.split(".")
the_dict[new_val[0]].append(i)
print(the_dict)
输出:
{'Gen2': ['Gen2.A'], 'Gen3': ['Gen3.A', 'Gen3.B', 'Gen3.C'], 'Gen1': ['Gen1.A', 'Gen1.B']}
据我所知,你想从这个角度出发:
gen_dict = make_dictionary(genes, mutations)
为此:
make_dictionary(gen_dict, genes, mutations)
其中,make_dictionary
函数“创建”变量gen_dict
不幸的是,变量并不是这样工作的。如果要定义名为GenDict
的变量,方法是使用GenDict=…
。你可以这样做:
gen_dict = {}
fill_dictionary(gen_dict, genes, mutations)
这将创建一个名为gen_dict
的变量,并将其分配给一个新的空字典。然后,您的函数将遍历并向该字典添加内容:
def fill_dictionary(d, genes, mutations):
for g in genes:
d[g] = [m for m in mutations if m.startswith(g)]
但是调用函数不能导致新变量出现在调用方的作用域中。(这并不完全正确,因为globals()
,但就大多数意图和目的而言,这是正确的。)
(顺便说一句,有一行代码可以创建字典:
dictionary={g:[m代表突变中的m,如果m.startswith(g+”)]g代表基因中的g}
。在Google或StackOverflow上搜索列表理解和字典理解——它们太神奇了!)据我所知,你想从这一点出发:
gen_dict = make_dictionary(genes, mutations)
为此:
make_dictionary(gen_dict, genes, mutations)
其中,make_dictionary
函数“创建”变量gen_dict
不幸的是,变量并不是这样工作的。如果要定义名为GenDict
的变量,方法是使用GenDict=…
。你可以这样做:
gen_dict = {}
fill_dictionary(gen_dict, genes, mutations)
这将创建一个名为gen_dict
的变量,并将其分配给一个新的空字典。然后,您的函数将遍历并向该字典添加内容:
def fill_dictionary(d, genes, mutations):
for g in genes:
d[g] = [m for m in mutations if m.startswith(g)]
但是调用函数不能导致新变量出现在调用方的作用域中。(这并不完全正确,因为globals()
,但就大多数意图和目的而言,这是正确的。)
(顺便说一句,有一行代码可以创建字典:
dictionary={g:[m代表突变中的m,如果m.startswith(g+”)]g代表基因中的g}
。在Google或StackOverflow上搜索列表理解和字典理解——它们太神奇了!)我假设您有Python以外其他语言编程的背景;一种允许您更改函数参数的语言。好吧,Python没有。问题不在于使用dict,而在于分配给函数参数。这不会在功能之外产生影响。您要做的可能是:
def fullDict(keys, values):
return { key: [ value for value in values if key in value] for key in keys }
print(fullDict(Genes, Mutations))
我假设你有Python以外的其他语言编程的背景;一种允许您更改函数参数的语言。好吧,Python没有。问题不在于使用dict,而在于分配给函数参数。这不会在功能之外产生影响。您要做的可能是:
def fullDict(keys, values):
return { key: [ value for value in values if key in value] for key in keys }
print(fullDict(Genes, Mutations))
你想要的结果是什么?一本字典,其中基因是键,突变是每个键的值。所以Gen1有['Gen1.A'、'Gen1.B']作为值等等。为什么您希望函数看起来像
fullDict(GenDict,Genes,translations)
而不是GenDict=fullDict(Genes,translations)
?这似乎很奇怪。@MiguelAlberolaCano您是否有工作代码需要改进,以改进代码风格?我想这是离题,但你可能想考虑不考虑,但谢谢!你想要的结果是什么?一本字典,其中基因是键,突变是每个键的值。所以Gen1有['Gen1.A'、'Gen1.B']作为值等等。为什么您希望函数看起来像fullDict(GenDict,Genes,translations)
而不是GenDict=fullDict(Genes,translations)
?这似乎很奇怪。@MiguelAlberolaCano您是否有工作代码需要改进,以改进代码风格?我想这是离题,但你可能想考虑不考虑,但谢谢!那个是很好。请问你知道这些东西的来源吗?通过在python文档中查找随机函数来构造函数似乎效率不高。从存储库中获取远高于我技能水平的函数似乎不是一个好主意……您可能想先看看Stackoverflow上的其他问题,这些问题标记为dict-comprehension和list-comprehension,然后尝试自己解决其他用户发布的问题。为了更好地理解列表和听写,您可以查看以下链接:谢谢!我会查的!很高兴我能帮忙!那个是很好。请问你知道这些东西的来源吗?通过在python文档中查找随机函数来构造函数似乎效率不高。从存储库中获取远高于我技能水平的函数似乎不是一个好主意……您可能想先看看Stackoverflow上的其他问题,这些问题标记为dict-comprehension和list-comprehension,然后尝试自己解决其他用户发布的问题。为了更好地理解列表和听写,您可以查看以下链接:谢谢!我会查的!很高兴我能帮忙!这是一个很好的解释!我正在查看:)那是