我有一个复杂的字典结构,我想通过一个键列表访问,以解决正确的项目.
dataDict = {
"a":{
"r": 1,
"s": 2,
"t": 3
},
"b":{
"u": 1,
"v": {
"x": 1,
"y": 2,
"z": 3
},
"w": 3
}
}
maplist = ["a", "r"]
Run Code Online (Sandbox Code Playgroud)
要么
maplist = ["b", "v", "y"]
Run Code Online (Sandbox Code Playgroud)
我已经制作了以下代码,但是我确信如果有人有想法,有更好更有效的方法.
# Get a given data from a dictionary with position provided as a list
def getFromDict(dataDict, mapList):
for k in mapList: dataDict = dataDict[k]
return dataDict
# Set a given data in a dictionary with position provided as a list
def …Run Code Online (Sandbox Code Playgroud) 我希望能够做到这样的事情:
from dotDict import dotdictify
life = {'bigBang':
{'stars':
{'planets': []}
}
}
dotdictify(life)
# This would be the regular way:
life['bigBang']['stars']['planets'] = {'earth': {'singleCellLife': {}}}
# But how can we make this work?
life.bigBang.stars.planets.earth = {'singleCellLife': {}}
#Also creating new child objects if none exist, using the following syntax:
life.bigBang.stars.planets.earth.multiCellLife = {'reptiles':{},'mammals':{}}
Run Code Online (Sandbox Code Playgroud)
我的动机是改进代码的简洁性,如果可能的话,使用与Javascript类似的语法来访问JSON对象,以实现高效的跨平台开发.(我也使用Py2JS和类似的.)
说我有在Python几个变量或对象a,b,c,...
如何轻松地将这些变量转储到Python中的命名空间中并在以后恢复它们?(例如,以相同的方式argparse将各种变量包装到命名空间中).
以下是我希望如何在命名空间之间转储内容的两个示例:
function (bar):
# We start with a, b and c
a = 10
b = 20
c = "hello world"
# We can dump anything we want into e, by just passing things as arguments:
e = dump_into_namespace(a, b, c)
del a, b, c
print (e.a + e.b) # Prints 30
return e # We can return e if we want. This is just …Run Code Online (Sandbox Code Playgroud) 全新的python,让我说我有一个字典:
kidshair = {'allkids':{'child1':{'hair':'blonde'},
'child2':{'hair':'black'},
'child3':{'hair':'red'},
'child4':{'hair':'brown'}}}
Run Code Online (Sandbox Code Playgroud)
如果child3定期更改头发颜色,我可能想编写一个应用程序来加速数据维护.在这个例子中,我使用:
kidshair['allkids']['child3']['hair'] = ...
Run Code Online (Sandbox Code Playgroud)
有没有办法将此路径存储为变量,以便我可以在我的乐趣中访问它?明显
mypath = kidshair['allkids']['child3']['hair']
Run Code Online (Sandbox Code Playgroud)
导致mypath ='red'.是否有任何可能的方法来硬编码路径本身,所以我可以使用:
mypath = 'blue'
Run Code Online (Sandbox Code Playgroud)
拒绝
kidshair['allkids']['child3']['hair'] = 'blue'
Run Code Online (Sandbox Code Playgroud)
非常感谢,ATfPT
有没有一种很好的方法来处理不存在的字典键?
我有一个基于字典的数据库,其中的键看起来像这样
ID
ver
sub_ver
A
B
C
Run Code Online (Sandbox Code Playgroud)
我想比较每个ID/ver/sub_ver的A,B,C键的值,以确保:
(A==B and C==None) or (A==C and B==None) or (A==B==C)
Run Code Online (Sandbox Code Playgroud)
然而,并非所有"ID"都具有A和B以及C键/值
我不是很好的代码:
**loops outside of this for ID/ver/sub_ver**
try:
A = data_structure[ID][ver][sub_ver]['A']
B = data_structure[ID][ver][sub_ver]['B']
C = data_structure[ID][ver][sub_ver]['C']
except KeyError:
try:
A = data_structure[ID][ver][sub_ver]['A']
B = data_structure[ID][ver][sub_ver]['B']
C = None
except KeyError:
try:
A = data_structure[ID][ver][sub_ver]['A']
B = None
C = data_structure[ID][ver][sub_ver]['C']
Run Code Online (Sandbox Code Playgroud)
接下来我检查所有值是否匹配
我使用set()以防万一A/B/C列表不合规
if not any((set(A)==set(B) and C==None, \
set(A)==set(C) and B==None, \
set(A)==set(B)==set(C))):
set_of_problems.append([ID, ver, sub_ver, [A, B, C])
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来执行嵌套try …