我里面有一个dict
包含另一个dict
d1 = {'a':{'p':1, 'q':2, 'r':'abc'},
'b':{'p':5, 'q':6, 'r':["google", "pypi.org"]}
}
url1 = "https://google.com"
url2 = "https://abc.com"
现在我想做的是检查r
两者的值,dict values
但我不想要任何代码冗余。这怎么可能?
我现在正在做的是:-
for k, v in d1.iteritems():
if isinstance(v['r'], list):
for l in v['r']:
if url1.find(l):
..Do something..
else:
continue
else:
if url1.find(v['r'):
..Do Something same as above..
else:
continue
现在问题出现了相同的Do something
重复 2 次,有没有办法通过理解或任何其他方法来解决冗余,除了函数制作和调用。
编辑——代码已经在一个大型函数定义中,所以除了创建另一个函数并调用它之外,请提供其他解决方案。