从生成器输出中删除None。

huangapple go评论68阅读模式
英文:

Remove None from generator output

问题

我如何从这个生成器中删除所有的None值:

(i.get("x") for i in l)

我认为可以使用赋值表达式来完成。我尝试过:

(a:=i.get("x") for i in l if a)

但它什么都没有返回。

英文:

How do I remove all the None values from this generator:

(i.get("x") for i in l) 

I believe to can be done using assignment expressions. I tried:

(a:=i.get("x") for i in l if a) 

But it returned nothing.

答案1

得分: 4

如果您使用支持海象运算符(Python 3.8+)的Python版本,您可以在您的推导式中使用它:

l = [
    {"x": 1},
    {"x": 0},
    {"y": 2},
]

print([
    clean
    for i in l
    if (clean := i.get("x")) is not None
])

应该会得到:

[1, 0]
英文:

If you have a version of python that supports the walrus operator (3.8+) then you can use it in your comprehension:

l = [
    {"x": 1},
    {"x": 0},
    {"y": 2},
]

print([
    clean
    for i in l
    if (clean := i.get("x")) is not None
])

Should give you:

[1, 0]

答案2

得分: 1

没有河马操作符,我们可以只是迭代一个从 i.get('x') 创建的单例元组。这会创建一个名为 x 的变量,其绑定到 i.get('x') 返回的值。

>>> lst = [{'d': 42}, {'x': 27}]
>>> [i.get('x') for i in lst]
[None, 27]
>>> [x 
...  for i in lst
...  for x in (i.get('x'),)
...  if x is not None]
[27]

这不考虑 x 是其中一个字典中的键且其关联值为 None 的特殊情况。

英文:

Without the walrus operator, we can just iterate over a singleton tuple created from i.get('x'). This has the effect of creating a variable x bound to the value returned by i.get('x').

>>> lst = [{'d': 42}, {'x': 27}]
>>> [i.get('x') for i in lst]
[None, 27]
>>> [x 
...  for i in lst
...  for x in (i.get('x'),)
...  if x is not None]
[27]

This does not account for the corner case of 'x' being a key in one of the dictionaries with the associated value None.

答案3

得分: 0

你可以使用海象操作符,但对于列表推导或生成器,赋值必须在if部分完成:

(a for i in l if (a:=i.get('x')) is not None)
英文:

Yes, you can use the walrus operator, but for a comprehension or generator the assignment has to be done in the if part:

(a for i in l if (a:=i.get('x')) is not None)

答案4

得分: -1

可以过滤生成器结果,仅返回不为None的结果。

filter(lambda x: x is not None, (i.get("x") for i in l))

或者

(x.get("x") for x in l if x.get("x") is not None)
英文:

You can filter the generator result to return only the results that are not None.

filter(lambda x: x is not None, (i.get("x") for i in l))

or

(x.get("x") for x in l if x.get("x") is not None)

答案5

得分: -1

你可以首先检查字典中是否包含 "x"

l = [{'x': 1}, {'x': 0}, {'y': 2}]

result = [d["x"] for d in l if "x" in d]

结果:

[1, 0]

如果你想要过滤掉值为 None 的情况,你可以使用沃尔鲁斯方法:

result = [x for d in l if (x := d.get("x")) is not None]
英文:

You could just check if "x" is in the dict first:

l = [{'x': 1}, {'x': 0}, {'y': 2}]

result = [d["x"] for d in l if "x" in d]

Result:

[1, 0]

In the event you want to filter out values that are None as well then you could use the walrus approch:

result = [x for d in l if (x := d.get("x")) is not None]

huangapple
  • 本文由 发表于 2023年6月15日 03:50:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定