[英]Error handling for list comprehension in Python
Is there a way to handle errors in a python list comprehension.有没有办法处理 python 列表理解中的错误。 Preferably I would have something like this where the last two values are represented by
None
:最好我有这样的东西,其中最后两个值由
None
表示:
values = [try i ["row"] except KeyError None for i in [{"row" : 1}, {"Row" : 0}, {}]]
This throws a syntax error and the only way I can do it is:这会引发语法错误,我能做到的唯一方法是:
values = []
for i in [{"row" : 1}, {"Row" : 0}, {}]:
try: values.append (i ["row"])
except KeyError: values.append (None)
I hope there is a more 'neat' way of doing this because the current solution is not preferable due to having to append to a blank list when a list comprehension does this in such a nice way!我希望有一种更“整洁”的方法来做到这一点,因为当列表理解以如此好的方式执行此操作时,由于必须附加到空白列表,因此当前的解决方案并不可取!
you cannot catch an exception from a list comprehension ( How can I handle exceptions in a list comprehension in Python? ).您无法从列表理解中捕获异常( How can I handle exceptions in a list comprehension in Python? )。 But given what you want to do you could use
get
:但是考虑到您想做什么,您可以使用
get
:
values = [i.get("row") for i in [{"row" : 1}, {"Row" : 0}, {}]]
if the key isn't found in the dictionary get
returns None
, exactly what you're looking for (it can return anything you want, just by passing the default value as second argument, see Why dict.get(key) instead of dict[key]? )如果在字典中找不到键
get
返回None
,正是你要找的东西(它可以返回你想要的任何东西,只需将默认值作为第二个参数传递,请参阅为什么 dict.get(key) 而不是 dict [关键]? )
You can also check if key is dictionary
in list comprehension and return None
if it is not present:您还可以检查 key 是否是列表理解中的
dictionary
,如果不存在则返回None
:
key = 'row'
values = [i[key] if key in i else None for i in [{"row" : 1}, {"Row" : 0}, {}]]
You can't handle exceptions directly in a list comprehension, because try
is a statement, not an expression.您不能直接在列表理解中处理异常,因为
try
是一个语句,而不是一个表达式。
However, you can abstract the try
out into a separate function:但是,您可以将
try
抽象为一个单独的函数:
def tryirow(i):
try: return i["row"]
except KeyError: return None
values = [tryirow(i) for i in [{"row" : 1}, {"Row" : 0}, {}]]
Of course in this case, as Jean-François Fabre's answer implies, you've just reimplemented the built-in dict.get
method:当然,在这种情况下,正如Jean-François Fabre 的回答所暗示的那样,您刚刚重新实现了内置的
dict.get
方法:
values = [i.get("row") for i in [{"row" : 1}, {"Row" : 0}, {}]]
But this shows how you can solve similar problems more generally: if there's a function that does what you want, call it;但这显示了如何更普遍地解决类似问题:如果有一个函数可以满足您的需求,请调用它; if not, write it.
如果没有,写下来。
And of course sometimes, "write out a for
statement" is actually the right answer.当然有时候,“写出一个
for
语句”实际上是正确的答案。 Not everything should be written as a list comprehension, even many things that can .不是所有的东西都应该写成列表理解,即使是很多可以的东西。 I don't think that's relevant here, but it's worth keeping in mind.
我认为这在这里无关紧要,但值得牢记。
There was actually a proposal to add a try
expression just like the one you're trying to write, PEP 463 .实际上有人提议添加一个
try
表达式,就像您尝试编写的PEP 463一样。 Why was it rejected?为什么被拒绝了? Because almost all of the use-cases were "get-with-default-fallback" cases where the function you want already exists (like
dict.get
) or should exist.因为几乎所有用例都是“get-with-default-fallback”情况,其中您想要的功能已经存在(如
dict.get
)或应该存在。 Nobody could come up with a common use case that wasn't better written with a separate function or an expanded-out for
statement.没有人能想出一个用单独的函数或扩展的
for
语句编写得更好的常见用例。
In a brand new language, I think it would make more sense to have a try
expression and not have methods like dict.get
, but in a language that already had dict.get
for over a decade before anyone suggested a try
expression (and almost two decades before anyone put together a concrete proposal), that would be a bad change to make.在一种全新的语言中,我认为有一个
try
表达式而不是像dict.get
这样的方法更有意义,但是在一种已经有dict.get
十多年的语言中,在任何人建议一个try
表达式之前(几乎在任何人提出具体建议之前二十年),这将是一个糟糕的改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.