Is there a smart way to write the following code in three or four lines?
是否有一种聪明的方法可以在三行或四行中编写以下代码?
a=l["artist"]
if a:
b=a["projects"]
if b:
c=b["project"]
if c:
print c
So I thought for something like pseudocode:
所以我想像伪代码一样:
a = l["artist"] if True:
5 个解决方案
#1
5
I don't necessarily think that this is better but you could do:
我不一定认为这更好但你可以这样做:
try:
c = l["artist"]["projects"]["project"]
except (KeyError, TypeError) as e:
print e
pass
#2
8
How about:
怎么样:
try:
print l["artist"]["projects"]["project"]
except KeyError:
pass
except TypeError:
pass # None["key"] raises TypeError.
This will try
to print
the value, but if a KeyError
is raised, the except
block will be run. pass
means to do nothing. This is known and EAFP: it’s Easier to Ask Forgiveness than Permission.
这将尝试打印该值,但如果引发KeyError,则将运行except块。通过意味着什么也不做。这是众所周知的和EAFP:宽恕比宽恕更容易。
#3
0
p = l.get('artist') and l['artist'].get('projects') and l['artist']['projects'].get('project')
if p:
print p
You can also make a more general function for this purpose:
您还可以为此目的制作更通用的功能:
def get_attr(lst, attr):
current = lst
for a in attr:
if current.get(a) is not None:
current = current.get(a)
else:
break
return current
>>> l = {'artist':{'projects':{'project':1625}}}
>>> get_attr(l,['artist','projects','project'])
1625
#4
0
One-liner (as in the title) without exceptions:
单行(如标题中)无例外:
if "artist" in l and l["artist"] and "projects" in l["artist"] and l["artist"]["projects"] and "project" in l["artist"]["projects"]: print l["artist"]["projects"]["project"]
#5
0
Since you're dealing with nested dictionaries, you might find this generic one-liner useful because it will allow you to access values at any level just by passing it more keys
arguments:
由于您正在处理嵌套字典,您可能会发现这种通用的单行有用,因为它允许您通过传递更多键参数来访问任何级别的值:
nested_dict_get = lambda item, *keys: reduce(lambda d, k: d.get(k), keys, item)
l = {'artist': {'projects': {'project': 'the_value'}}}
print( nested_dict_get(l, 'artist', 'projects', 'project') ) # -> the_value
Note: In Python 3, you'd need to add a from functools import reduce
at the top.
注意:在Python 3中,您需要在顶部添加functools import reduce。
#1
5
I don't necessarily think that this is better but you could do:
我不一定认为这更好但你可以这样做:
try:
c = l["artist"]["projects"]["project"]
except (KeyError, TypeError) as e:
print e
pass
#2
8
How about:
怎么样:
try:
print l["artist"]["projects"]["project"]
except KeyError:
pass
except TypeError:
pass # None["key"] raises TypeError.
This will try
to print
the value, but if a KeyError
is raised, the except
block will be run. pass
means to do nothing. This is known and EAFP: it’s Easier to Ask Forgiveness than Permission.
这将尝试打印该值,但如果引发KeyError,则将运行except块。通过意味着什么也不做。这是众所周知的和EAFP:宽恕比宽恕更容易。
#3
0
p = l.get('artist') and l['artist'].get('projects') and l['artist']['projects'].get('project')
if p:
print p
You can also make a more general function for this purpose:
您还可以为此目的制作更通用的功能:
def get_attr(lst, attr):
current = lst
for a in attr:
if current.get(a) is not None:
current = current.get(a)
else:
break
return current
>>> l = {'artist':{'projects':{'project':1625}}}
>>> get_attr(l,['artist','projects','project'])
1625
#4
0
One-liner (as in the title) without exceptions:
单行(如标题中)无例外:
if "artist" in l and l["artist"] and "projects" in l["artist"] and l["artist"]["projects"] and "project" in l["artist"]["projects"]: print l["artist"]["projects"]["project"]
#5
0
Since you're dealing with nested dictionaries, you might find this generic one-liner useful because it will allow you to access values at any level just by passing it more keys
arguments:
由于您正在处理嵌套字典,您可能会发现这种通用的单行有用,因为它允许您通过传递更多键参数来访问任何级别的值:
nested_dict_get = lambda item, *keys: reduce(lambda d, k: d.get(k), keys, item)
l = {'artist': {'projects': {'project': 'the_value'}}}
print( nested_dict_get(l, 'artist', 'projects', 'project') ) # -> the_value
Note: In Python 3, you'd need to add a from functools import reduce
at the top.
注意:在Python 3中,您需要在顶部添加functools import reduce。