Python 从列表对象为dict生成键
我有一份清单:Python 从列表对象为dict生成键,python,dictionary,Python,Dictionary,我有一份清单: k = ["key1", "subkey2", "subsubkey3"] 我确信d是一个dict,因此d[“key1”][“subkey2”][“subsubkey3”]是有效的 如何将listk转换为dictd的键,以返回d[k[0]][k[1]]… temp_d = d for key in k: temp_d = temp_d[key] 此代码完成后,temp_d将包含您的值 此代码完成后,temp_d将包含您的值这是为数不多的减少可能是个好主意的情况之一-它所做的
k = ["key1", "subkey2", "subsubkey3"]
我确信d
是一个dict,因此d[“key1”][“subkey2”][“subsubkey3”]
是有效的
如何将listk
转换为dictd
的键,以返回d[k[0]][k[1]]…
temp_d = d
for key in k:
temp_d = temp_d[key]
此代码完成后,temp_d将包含您的值
此代码完成后,temp_d将包含您的值这是为数不多的减少可能是个好主意的情况之一-它所做的是对值连续应用相同的操作
items = {'foo': {'bar': {'baz': 123}}}
keys = ['foo', 'bar', 'baz']
reduce(lambda d, k: d[k], keys, items)
这相当于:
items = {'foo': …}
keys = ['foo', …]
result = items
for k in keys:
# The RHS here is the function passed to reduce(), applied to the
# (intermediate) result and the current step in the loop
result = items[k]
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
try:
initializer = next(it)
except StopIteration:
raise TypeError('reduce() of empty sequence with no initial value')
accum_value = initializer
for x in it:
accum_value = function(accum_value, x)
return accum_value
accum_value = d
for x in k:
accum_value = accum_value[x]
这是
reduce
可能是个好主意的少数情况之一-它所做的是对值连续应用相同的操作
items = {'foo': {'bar': {'baz': 123}}}
keys = ['foo', 'bar', 'baz']
reduce(lambda d, k: d[k], keys, items)
这相当于:
items = {'foo': …}
keys = ['foo', …]
result = items
for k in keys:
# The RHS here is the function passed to reduce(), applied to the
# (intermediate) result and the current step in the loop
result = items[k]
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
try:
initializer = next(it)
except StopIteration:
raise TypeError('reduce() of empty sequence with no initial value')
accum_value = initializer
for x in it:
accum_value = function(accum_value, x)
return accum_value
accum_value = d
for x in k:
accum_value = accum_value[x]
您可以尝试使用:
在Python3.x中,应该使用
reduce()
只需获取一个2参数函数,并将其连续应用于列表的元素,从而累积结果。还有一个可选的初始值设定项参数,我们在这里使用了它。正如文档所述,reduce()
大致相当于:
items = {'foo': …}
keys = ['foo', …]
result = items
for k in keys:
# The RHS here is the function passed to reduce(), applied to the
# (intermediate) result and the current step in the loop
result = items[k]
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
try:
initializer = next(it)
except StopIteration:
raise TypeError('reduce() of empty sequence with no initial value')
accum_value = initializer
for x in it:
accum_value = function(accum_value, x)
return accum_value
accum_value = d
for x in k:
accum_value = accum_value[x]
在本例中,我们传递的是一个初始值设定项
,因此它不会是None
。因此,我们有:
def reduce(function, iterable, initializer=None):
it = iter(iterable)
accum_value = initializer
for x in it:
accum_value = function(accum_value, x)
return accum_value
在本例中,我们的函数是getitem(a,b)
(参见上面的链接),它只返回a[b]
。此外,我们的iterable
是k
,我们的初始值设定项是d
。因此,上面的reduce()
调用相当于:
items = {'foo': …}
keys = ['foo', …]
result = items
for k in keys:
# The RHS here is the function passed to reduce(), applied to the
# (intermediate) result and the current step in the loop
result = items[k]
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
try:
initializer = next(it)
except StopIteration:
raise TypeError('reduce() of empty sequence with no initial value')
accum_value = initializer
for x in it:
accum_value = function(accum_value, x)
return accum_value
accum_value = d
for x in k:
accum_value = accum_value[x]
您可以尝试使用:
在Python3.x中,应该使用
reduce()
只需获取一个2参数函数,并将其连续应用于列表的元素,从而累积结果。还有一个可选的初始值设定项参数,我们在这里使用了它。正如文档所述,reduce()
大致相当于:
items = {'foo': …}
keys = ['foo', …]
result = items
for k in keys:
# The RHS here is the function passed to reduce(), applied to the
# (intermediate) result and the current step in the loop
result = items[k]
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
try:
initializer = next(it)
except StopIteration:
raise TypeError('reduce() of empty sequence with no initial value')
accum_value = initializer
for x in it:
accum_value = function(accum_value, x)
return accum_value
accum_value = d
for x in k:
accum_value = accum_value[x]
在本例中,我们传递的是一个初始值设定项
,因此它不会是None
。因此,我们有:
def reduce(function, iterable, initializer=None):
it = iter(iterable)
accum_value = initializer
for x in it:
accum_value = function(accum_value, x)
return accum_value
在本例中,我们的函数是getitem(a,b)
(参见上面的链接),它只返回a[b]
。此外,我们的iterable
是k
,我们的初始值设定项是d
。因此,上面的reduce()
调用相当于:
items = {'foo': …}
keys = ['foo', …]
result = items
for k in keys:
# The RHS here is the function passed to reduce(), applied to the
# (intermediate) result and the current step in the loop
result = items[k]
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
try:
initializer = next(it)
except StopIteration:
raise TypeError('reduce() of empty sequence with no initial value')
accum_value = initializer
for x in it:
accum_value = function(accum_value, x)
return accum_value
accum_value = d
for x in k:
accum_value = accum_value[x]
你的意思是d[k[0]]
是一本包含键k[1]
的字典,其中包含一本带有键k[2]
的字典。此外:你试过什么吗?是的,我的意思是。@SkyFox你的问题很好,重新措辞,并用代码化一些部分使它更可读。d[k[0][k[1][k[2]
你的意思是d[k[0]]
是一个包含键k[1]
的字典,它包含一个带有键k[2]
的字典。此外:你试过什么吗?是的,我的意思是。@SkyFox你的问题很好,重新措辞,并用代码化一些部分使它更可读。d[k[0][k[1][k[2]
这很好。我学到了一些新东西,谢谢。你能再详细解释一下吗?这太棒了。我学到了一些新东西,谢谢。你能再详细解释一下吗?