我有这段自称的代码:
def get_input():
my_var = input('Enter "a" or "b": ')
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
get_input()
else:
return my_var
print('got input:', get_input())
Run Code Online (Sandbox Code Playgroud)
现在,如果我输入"a"或"b",一切都很好.输出是:
Type "a" or "b": a
got input: a
Run Code Online (Sandbox Code Playgroud)
但是,如果我输入其他内容然后输入"a"或"b",我会得到:
Type "a" or "b": purple
You didn't type "a" or "b". Try again.
Type "a" or "b": a
got input: None
Run Code Online (Sandbox Code Playgroud)
我不知道为什么get_input()要回来None,因为它应该只返回my_var.print语句显示None正确的值,但函数由于某种原因不返回该值.
有很多很好的getattr()函数用于解析嵌套的字典结构,例如:
我想做一个并行的setattr().基本上,给定:
cmd = 'f[0].a'
val = 'whatever'
x = {"a":"stuff"}
Run Code Online (Sandbox Code Playgroud)
我想生成一个我可以分配的功能:
x['f'][0]['a'] = val
Run Code Online (Sandbox Code Playgroud)
或多或少,这将以与以下相同的方式工作:
setattr(x,'f[0].a',val)
Run Code Online (Sandbox Code Playgroud)
产量:
>>> x
{"a":"stuff","f":[{"a":"whatever"}]}
Run Code Online (Sandbox Code Playgroud)
我现在叫它setByDot():
setByDot(x,'f[0].a',val)
Run Code Online (Sandbox Code Playgroud)
这样做的一个问题是,如果中间的密钥不存在,则需要检查并生成中间密钥(如果不存在) - 即,对于上述情况:
>>> x = {"a":"stuff"}
>>> x['f'][0]['a'] = val
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'f'
Run Code Online (Sandbox Code Playgroud)
所以,你首先必须做:
>>> x['f']=[{}]
>>> x
{'a': 'stuff', 'f': [{}]}
>>> x['f'][0]['a']=val
>>> x
{'a': 'stuff', 'f': [{'a': 'whatever'}]}
Run Code Online (Sandbox Code Playgroud)
另一个是当下一个项目是一个字符串时,键入当下一个项目是一个字符串时,键入不同,即:
>>> x = {"a":"stuff"}
>>> x['f']=['']
>>> x['f'][0]['a']=val
Traceback …Run Code Online (Sandbox Code Playgroud) 假设我有一个任意嵌套的字典:
d = {
11: {
21: {31: 'a', 32: 'b'},
22: {31: 'a', 34: 'c'},
},
12: {
1: {2: 3}
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个键列表,其位置告诉我哪个嵌套字典可以查找每个键:
keys = [11, 21, 31]
# keys = [11, 23, 44]
Run Code Online (Sandbox Code Playgroud)
有一个简单的衬垫来做这个吗?我看了下面列出的问题,它们是相似的,但不是我真正想要的.我自己也试过了,想出了这个:
from functools import reduce
def lookup(d, key):
return d.get(key, {}) if d and isinstance(d, dict) else None
def fn(keys, d):
return reduce(lookup, keys, d)
print(fn(keys, d)) # prints 'a'
Run Code Online (Sandbox Code Playgroud)
这样做的问题是,如果是第二个键列表(参见注释掉的键),它会继续查找嵌套键,即使没有找到更高级别的键,继续也没有意义.我怎么能reduce在找到最终匹配或失败后立即停止(下面列出的一个问题解决了它,但我不能在我的用例中真正应用它......或者我可以吗?)?还有其他想法吗?哦,我想用官方的python库来完成这个.所以不numpy,pandas等等,但是functools,itertools没关系
Python:将列表转换为带有异常处理的多维dict的dict键
寻找一个通用的解决方案,我可以从字典中删除特定的键及其值。例如,如果dict包含以下嵌套的键值对:
data={
"set": {
"type": "object", #<-- should remove this key:value pair
"properties": {
"action": {
"type": "string", #<-- should NOT remove this key:value pair
"description": "My settings"
},
"settings": {
"type": "object", #<-- should remove this key:value pair
"description": "for settings",
"properties": {
"temperature": {
"type": "object", #<-- should remove this key:value pair
"description": "temperature in degree C",
"properties": {
"heater": {
"type": "object", #<-- should remove this key:value pair
"properties": {
"setpoint": {
"type": …Run Code Online (Sandbox Code Playgroud)