英文:
Need suggestions for a better approach to handle missing keys in a function operating on dictionaries
问题
问题是我正在寻找解决任务的最佳实践。
我有下面描述的函数(用于解释问题的简单示例)
def create_additional_keys(data: dict):
data['l_t_j'] = 1 if data['a'] in [27, 11, 33] else 0
data['b_n_j'] = 1 if data['b'] in [29, 1, 27] else 0
data['p_t_q'] = 'ck' if data['c'] == '' else data['c']
data['m_k_z'] = 'd12' if data['d'] in ['d1', 'd2'] else 'other'
data['y_s_n'] = data['e1'] * data['e2'] * data['e3']
data['h_g_p'] = np.log(data['f'])
...
data['s_t_x'] = 1 if data['g'] < 0 else data['g']
data['c_e_m'] = 1 if data['i'] in [97, 26, 57] else 2 if data['i'] in [98, 27, 58] else 3
data['s_o_j'] = 1 if data['j'] in [82, 38, 60] else 0
data['k_s_a'] = data['h'] // 4
问题是,当我使用这个函数时,我总是需要确保我的字典包含所有的键,但这并不总是方便的。
我经常有我需要的大部分键,但有时候没有。
有什么最佳实践可以使函数独立于是否有这些键?
目前,我有几种实现方式,但我不太喜欢它们,希望找到最好的方式。
-
将每段代码包装在try-except中(正如我所说,通常情况下,字典中有大多数的键)
例如:try: data['l_t_j'] = 1 if data['a'] in [27, 11, 33] else 0 except KeyError: pass
-
在格式化新键之前,首先检查字典中是否存在所需的键。
例如:if 'a' in data: data['l_t_j'] = 1 if data['a'] in [27, 11, 33] else 0
-
将负责创建新键的代码行移到单独的函数中,并使用带有try-except结构的循环来遍历它们
例如:formation_l_t_j = lambda data: {"l_t_j": 1 if data["a"] in [27, 11, 33] else 0} ... formation_k_s_a = lambda data: {"k_s_a": data["h"] // 4} for function in [formation_l_t_j, ..., formation_k_s_a]: try: data.update(function(data)) except KeyError: pass
英文:
The question is that I am seeking the best practice to solve my task.
I have the function that I described below (simple example for the explanation of the problem)
def create_additional_keys(data: dict):
data['l_t_j'] = 1 if data['a'] in [27, 11, 33] else 0
data['b_n_j'] = 1 if data['b'] in [29, 1, 27] else 0
data['p_t_q'] = 'ck' if data['c'] == '' else data['c']
data['m_k_z'] = 'd12' if data['d'] in ['d1', 'd2'] else 'other'
data['y_s_n'] = data['e1'] * data['e2'] * data['e3']
data['h_g_p'] = np.log(data['f'])
...
data['s_t_x'] = 1 if data['g'] < 0 else data['g']
data['c_e_m'] = 1 if data['i'] in [97, 26, 57] else 2 if data['i'] in [98, 27, 58] else 3
data['s_o_j'] = 1 if data['j'] in [82, 38, 60] else 0
data['k_s_a'] = data['h'] // 4
The problem is that when I use this function, I always need to ensure that my dictionary has all the keys, but it is not always comfortable.
I often have the majority of keys that I need, but sometimes I do not.
What is the best practice to make a function independent of whether I have these keys or not?
At this point, I have several variants of realization, but I don't like them much and want to realize the best variant.
- To wrap each code in try-except (as I said, most often, the dictionary has a majority of keys)
ex.:
try:
data['l_t_j'] = 1 if data['a'] in [27, 11, 33] else 0
except KeyError:
pass
- Before formatting a new key, check first if the key that is needed exists in the dictionary.
ex.:
if 'a' in data:
data['l_t_j'] = 1 if data['a'] in [27, 11, 33] else 0
- To move the lines of code responsible for creating a new key to separate functions and use a loop with try-except structure to iterate through them
ex.:
formation_l_t_j = lambda data: {"l_t_j": 1 if data["a"] in [27, 11, 33] else 0}
...
formation_k_s_a = lambda data: {"k_s_a": data["h"] // 4}
for function in [formation_l_t_j, ..., formation_k_s_a]:
try:
data.update(function(data))
except KeyError:
pass
答案1
得分: 1
我审查了您的最后提议,并以我认为更清晰的方式实现它:
data = {'a': 27, 'b': 11}
l = (
('l_t_j', lambda: data['a'] in [27, 11, 33]),
('b_n_j', lambda: data['b'] in [29, 1, 27]),
('p_t_q', lambda: 'ck' if data['c'] == '' else data['c']),
)
for key, compute_value in l:
try:
value = compute_value()
except KeyError:
continue
data[key] = value
print(data)
# {'a': 27, 'b': 11, 'l_t_j': True, 'b_n_j': False}
通常不建议“命名”一个lambda函数,我们通常会使用标准的函数定义代替。
https://peps.python.org/pep-0008/#programming-recommendations
始终使用def语句而不是将lambda表达式直接绑定到标识符的赋值语句:
# 正确的方式: def f(x): return 2*x
# 错误的方式: f = lambda x: 2*x
第一种形式意味着结果函数对象的名称是特定的'f',而不是通用的'
'。这在回溯和一般字符串表示方面更有用。赋值语句的使用消除了lambda表达式在明确的def语句之上所提供的唯一好处(即它可以嵌入在较大的表达式中)。
英文:
I reviewed your last proposition and implement it in a way that I found cleaner:
data = {'a': 27, 'b': 11}
l = (
('l_t_j', lambda: data['a'] in [27, 11, 33]),
('b_n_j', lambda: data['b'] in [29, 1, 27]),
('p_t_q', lambda: 'ck' if data['c'] == '' else data['c']),
)
for key, compute_value in l:
try:
value = compute_value()
except KeyError:
continue
data[key] = value
print(data)
# {'a': 27, 'b': 11, 'l_t_j': True, 'b_n_j': False}
It's usually not recommended to "name" a lambda, we usually do a standard function definition instead.
https://peps.python.org/pep-0008/#programming-recommendations
> Always use a def statement instead of an assignment statement that
> binds a lambda expression directly to an identifier:
> python
> # Correct:
> def f(x): return 2*x
>
> python
> # Wrong:
> f = lambda x: 2*x
>
> The first form means that the name of the resulting function object is
> specifically ‘f’ instead of the generic ‘<lambda>’. This is more
> useful for tracebacks and string representations in general. The use
> of the assignment statement eliminates the sole benefit a lambda
> expression can offer over an explicit def statement (i.e. that it can
> be embedded inside a larger expression)
答案2
得分: 0
get()
方法是处理缺失键的典型方式。如果键不存在,它将返回值或 None。还有一个可选参数,用于在键不存在时使用。
要获取 1(如果存在)或 0(如果不存在或没有键):
data['l_t_j'] = 1 if data['a'] in [27, 11, 33] else 0
# 变成
data['l_t_j'] = int(data.get('a') in [27, 11, 33])
使用 or
来替换空/None字符串:
data['p_t_q'] = 'ck' if data['c'] == '' else data['c']
# 变成
data['p_t_q'] = data.get('c') or 'ck'
选择两个值之间的值:
data['m_k_z'] = 'd12' if data['d'] in ['d1', 'd2'] else 'other'
# 变成:
data['m_k_z'] = 'd12' if data.get('d') in ['d1', 'd2'] else 'other'
# 或者这样:
data['m_k_z'] = 'd12' * (data.get('d') in ['d1', 'd2']) or 'other'
对于数值计算使用默认参数:
data['y_s_n'] = data['e1'] * data['e2'] * data['e3']
# 变成
data['y_s_n'] = data.get('e1', 0) * data.get('e2', 0) * data.get('e3', 0)
函数调用:
data['h_g_p'] = np.log(data['f'])
# 变成
data['h_g_p'] = np.log(data.get('f', 1)) # 假设你希望键不存在时返回零,否则...
data['h_g_p'] = data.get('f') and np.log(data['f']) # 如果不存在返回 None
在键缺失时选择条件结果的默认值:
data['s_t_x'] = 1 if data['g'] < 0 else data['g']
# 变成
data['s_t_x'] = 1 if data.get('g', -1) < 0 else data['g']
多个条件:
data['c_e_m'] = 1 if data['i'] in [97, 26, 57] else 2 if data['i'] in [98, 27, 58] else 3
# 变成
data['c_e_m'] = {97: 1, 26: 1, 57: 1, 98: 2, 27: 2, 58: 2}.get(data.get('i'), 3)
英文:
The get()
method is the typical way to handle missing keys. If will return the value or None if the key is absent. There is also an optional parameter to use when the key is not there.
To get 1 if present 0 if not or no key
data['l_t_j'] = 1 if data['a'] in [27, 11, 33] else 0
# becomes
data['l_t_j'] = int( data.get('a') in [27, 11, 33] )
Using or
to replace empty/None strings:
data['p_t_q'] = 'ck' if data['c'] == '' else data['c']
# becomes
data['p_t_q'] = data.get('c') or 'ck'
To select between two values:
data['m_k_z'] = 'd12' if data['d'] in ['d1', 'd2'] else 'other'
# becomes:
data['m_k_z'] = 'd12' if data.get('d') in ['d1', 'd2'] else 'other'
# or this way:
data['m_k_z'] = 'd12' * (data.get('d') in ['d1', 'd2']) or 'other'
Using default parameter for numerical calculations:
data['y_s_n'] = data['e1'] * data['e2'] * data['e3']
# becomes
data['y_s_n'] = data,get('e1',0) * data.get('e2',0) * data.get('e3',0)
Function calls:
data['h_g_p'] = np.log(data['f'])
# becomes
data['h_g_p'] = np.log(data.get('f',1))
# assuming you want zero when key is absent, otherwise ...
data['h_g_p'] = data.get(f) and np.log(data['f']) # None if ansent
default value to chose condition result when key is missing:
data['s_t_x'] = 1 if data['g'] < 0 else data['g']
# becomes
data['s_t_x'] = 1 if data.get('g',-1) < 0 else data['g']
Multiple conditions:
data['c_e_m'] = 1 if data['i'] in [97, 26, 57] else 2 if data['i'] in [98, 27, 58] else 3
#becomes
data['c_e_m'] = {97:1, 26:1, 57:1, 98:2, 27:2, 58:2}.get(data.get('i'),3)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论