Python中—选择迭代哪个字典(并在其中操作值)的方法
我的问题是: 假设我有两本字典,dict_a和dict_b 它们中的每一个都有相似的键和值,我可以用同样的方式来操作它们,事实上,这就是我在一大块代码中所做的。只是我不想写两遍。但是,我不能这样做:Python中—选择迭代哪个字典(并在其中操作值)的方法,python,dictionary,Python,Dictionary,我的问题是: 假设我有两本字典,dict_a和dict_b 它们中的每一个都有相似的键和值,我可以用同样的方式来操作它们,事实上,这就是我在一大块代码中所做的。只是我不想写两遍。但是,我不能这样做: if choose_a == 1: for x and y in dict_a.iteritems(): # goto line 20 if choose_b == 1: for x and y in dict_b.iteritems(): # goto line
if choose_a == 1:
for x and y in dict_a.iteritems():
# goto line 20
if choose_b == 1:
for x and y in dict_b.iteritems():
# goto line 20
# line 20
# do stuff with x and y.
if choose_a == 1: the_dict=dict_a
elif choose_b == 1: the_dict=dict_b
for x,y in the_dict.iteritems():
# do stuff with x and y.
除了我不知道在这种情况下该怎么办。如果有一个类似的线程,请指向它,并原谅我,如果我违反了任何事情(第一篇文章)。提前谢谢,谢谢你的帮助 只需定义一个函数
def do_stuff(x, y):
# Do stuff
pass
if choose_a == 1:
for x and y in dict_a.iteritems():
do_stuff(x, y)
if choose_b == 1:
for x and y in dict_b.iteritems():
do_stuff(x, y)
只需定义一个函数
def do_stuff(x, y):
# Do stuff
pass
if choose_a == 1:
for x and y in dict_a.iteritems():
do_stuff(x, y)
if choose_b == 1:
for x and y in dict_b.iteritems():
do_stuff(x, y)
或许可以这样做:
if choose_a == 1:
for x and y in dict_a.iteritems():
# goto line 20
if choose_b == 1:
for x and y in dict_b.iteritems():
# goto line 20
# line 20
# do stuff with x and y.
if choose_a == 1: the_dict=dict_a
elif choose_b == 1: the_dict=dict_b
for x,y in the_dict.iteritems():
# do stuff with x and y.
或许可以这样做:
if choose_a == 1:
for x and y in dict_a.iteritems():
# goto line 20
if choose_b == 1:
for x and y in dict_b.iteritems():
# goto line 20
# line 20
# do stuff with x and y.
if choose_a == 1: the_dict=dict_a
elif choose_b == 1: the_dict=dict_b
for x,y in the_dict.iteritems():
# do stuff with x and y.
这个怎么样:
d = dict_a if choose_a else dict_b
for x, y in d.items():
# do stuff with x and y
显然,这是假设你要使用其中一个;如果不是这样的话,添加if语句非常简单。另外,您的x和y
语法无效,但我想您会发现:)这个怎么样:
d = dict_a if choose_a else dict_b
for x, y in d.items():
# do stuff with x and y
显然,这是假设你要使用其中一个;如果不是这样的话,添加if语句非常简单。另外,您的
x和y
语法无效,但我想您会发现:)如果选择a
和选择b
都为真,您希望发生什么?如果两者都不是真的呢?这两种情况中有一种是可能的吗
你能负担得起像几个答案所建议的那样将所有的“东西”转移到一个单独的函数吗,或者由此产生的范围变化会是一个问题吗
正如您所看到的,您留下了许多未指定(或完全未指定)的内容。假设最坏的情况…:
choose\uu…
变量都可能为true,在这种情况下,您需要同时使用这两个dictchoose\u…
变量都可能为false,在这种情况下,您不想执行任何操作thedicts = []
if choose_a == 1: thedicts.append(dict_a)
if choose_b == 1: thedicts.append(dict_b)
for d in thedicts:
for x, y in d.iteritems():
...do stuff _locally_ with x and y...
您可以更简洁地表达图片列表的构建,但是,我认为,如果在for
语句中将其汇总起来,就不那么清楚了,例如,如下所示:
for d in [d for d, c in zip((dict_a, dict_b), (choose_a, choose_b)) if c]:
如果choose_a
和choose_b
都为真,您希望发生什么?如果两者都不是真的呢?这两种情况中有一种是可能的吗
你能负担得起像几个答案所建议的那样将所有的“东西”转移到一个单独的函数吗,或者由此产生的范围变化会是一个问题吗
正如您所看到的,您留下了许多未指定(或完全未指定)的内容。假设最坏的情况…:
两个choose\uu…
变量都可能为true,在这种情况下,您需要同时使用这两个dict
choose\u…
变量都可能为false,在这种情况下,您不想执行任何操作
由于作用域问题,您需要在当前函数中发生“东西”
然后…:
thedicts = []
if choose_a == 1: thedicts.append(dict_a)
if choose_b == 1: thedicts.append(dict_b)
for d in thedicts:
for x, y in d.iteritems():
...do stuff _locally_ with x and y...
您可以更简洁地表达图片列表的构建,但是,我认为,如果在for
语句中将其汇总起来,就不那么清楚了,例如,如下所示:
for d in [d for d, c in zip((dict_a, dict_b), (choose_a, choose_b)) if c]:
您可以将这两个词典放入dict
中,然后用键选择它们
例如:
choice = 'A' # or choice = 'B'
working_dict = {'A': dict_a, 'B': dict_b}[choice]
for x,y in working_dict.iteritems():
# do stuff with x and y
这与使用两个标志的方法略有不同。如果您坚持使用该方案,您可以使用类似的方法设置choice
if choose_a == 1:
choice = 'A'
elif choose_b == 1:
choice = 'B'
else:
pass # maybe this should raise an exception?
也许这样写比较容易:
if choose_a == 1:
working_dict = dict_a
elif choose_b == 1:
working_dict = dict_b
else:
pass # maybe this should raise an exception?
for x,y in working_dict.iteritems():
# do stuff with x and y
您可以将这两个词典放入dict
中,然后用键选择它们
例如:
choice = 'A' # or choice = 'B'
working_dict = {'A': dict_a, 'B': dict_b}[choice]
for x,y in working_dict.iteritems():
# do stuff with x and y
这与使用两个标志的方法略有不同。如果您坚持使用该方案,您可以使用类似的方法设置choice
if choose_a == 1:
choice = 'A'
elif choose_b == 1:
choice = 'B'
else:
pass # maybe this should raise an exception?
也许这样写比较容易:
if choose_a == 1:
working_dict = dict_a
elif choose_b == 1:
working_dict = dict_b
else:
pass # maybe this should raise an exception?
for x,y in working_dict.iteritems():
# do stuff with x and y
+1,这有一个选择的时刻,在此之后,您的代码不可知dict来自何处。稍后添加dict_c或dict_d非常简单,并且代码重复最少。+1,这是唯一的选择,之后您的代码不知道dict来自何处。稍后添加dict_c或dict_d非常简单,并且代码重复最少。选民是否愿意解释这个答案的错误?我也想知道。我对此投了更高的票,因为这不是一个坏答案,尽管我更喜欢~unutbu的解决方案。下层选民是否愿意解释这个答案的错误?我也想知道。虽然我更喜欢~unutbu的解决方案,但我还是对这个答案投了赞成票。你提到这两个dict具有相似的键和值的方式让我觉得dict_a
和dict_b
应该是同一类的两个实例。您是否考虑过朝这个方向重新组织代码?然后,您可以定义使用该类型对象的适当方法,并在任何适当的实例上调用它。您提到这两个dict具有相似的键和值的方式让我觉得dict_a
和dict_b
应该是同一类的两个实例。您是否考虑过朝这个方向重新组织代码?然后可以定义使用该类型对象的适当方法,并在适当的实例上调用它。