Condense successive 'try' blocks

user3501855

I'm trying to take an existing dict with, say, five possible entries and move its entries into a new dict, 'translating' the keys along the way. The keys of the first dict are structured in a way that the move cannot be done procedurally. There is no guarantee that all of the entries will be present in the first dict.

In order to avoid KeyError, each entry can be assigned within its own try block

first_dict = {
   'key_1': foo1,
   'key_two': foo2,
   'key_three': foo3,
   'key_number_4': foo4,
   'key_5': foo5
}

second_dict = {}

try: 
    second_dict['translated_key_1'] = first_dict['key_1']
except KeyError:
    pass

try:
    second_dict['translated_key_2'] = first_dict['key_two']
except KeyError:
    pass

...

try:
    second_dict['translated_key_5'] = first_dict['key_5']
except KeyError:
    pass


   'translated_key_2': first_dict['key_two'],
   'translated_key_3': first_dict['key_three'],
   'translated_key_4': first_dict['key_number_4'],
   'translated_key_5': first_dict['key_5'],
}

Perhaps a better way is to check if the entry exists in the first dict and then assign it.

if 'key_1' in first_dict:
    second_dict['translated_key_1'] = first_dict['key_1']
if 'key_two' in first_dict:
    second_dict['translated_key_2'] = first_dict['key_two']
...

In either case, is there a way to condense this? When the number of elements is large it seems like this would become unnecessarily bulky. Is there a way to iterate through the relationships without creating a try block for each?

cwallenpoole

You could do this with a loop:

# This is a simple mapping which associates the old keys with the new ones
translations = {'translated_key_1': 'key_one',
                'translated_key_2': 'key_two'}

# iterate through the map
for k,v in translations.iteritems():
    # you could also replace with try except
    if v in first_dict:
        second_dict[k] = first_dict[v]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Condense event list with LINQ

来自分类Dev

Try and catch blocks: whether in the class itself or when I call the method outside the class

来自分类Dev

How does the STDIN buffer and getchar() pointer change during successive calls?

来自分类Dev

Number of blocks in a Simulink model

来自分类Dev

Swift blocks not working

来自分类Dev

带boost :: successive_shortest_path_nonnegative_weights的最小成本最大流量

来自分类Dev

Destructure React component into "building blocks"

来自分类Dev

用Qt设置Code :: Blocks

来自分类Dev

Inline function blocks in C#

来自分类Dev

Incorrect compiler implementation of iterator blocks?

来自分类Dev

使用Code :: Blocks调试C

来自分类Dev

Ruby Blocks(收益率)

来自分类Dev

Insert missing data within blocks

来自分类Dev

如何安装Code :: Blocks IDE?

来自分类Dev

Xenial Beta上的code :: blocks

来自分类Dev

Ruby Blocks有速记吗?

来自分类Dev

设置Blocks Bukkit性能友好

来自分类Dev

如何安装Code :: Blocks IDE?

来自分类Dev

怎么把Option [Try [_]]转换为Try [Option [_]]?

来自分类Dev

try catch语句的位置

来自分类Dev

JavaScript嵌套的try异常

来自分类Dev

从try {} catch {}访问变量

来自分类Dev

声明try catch块

来自分类Dev

遍历try / catch块?

来自分类Dev

php - try, catch, and retry

来自分类Dev

压缩连续的“ try”块

来自分类Dev

Try语句(Python)

来自分类Dev

Java Try Catch块

来自分类Dev

Try语句语法