如何在Python中执行以下操作?
row = [unicode(x.strip()) for x in row if x is not None else '']
Run Code Online (Sandbox Code Playgroud)
实质上:
可能重复:
`==`运算符何时不等于`is`运算符?(蟒蛇)
我使用的是Python 2.x.
我比较时编辑给了我一个"警告"下划线my_var == None,但是当我使用时没有警告my_var is None.
我在Python shell中做了一个测试,并确定两者都是有效的语法,但我的编辑器似乎说这my_var is None是首选.
是这种情况,如果是这样,为什么?
它更多的是关于python列表理解语法.我有一个列表理解,可以生成给定范围的奇数列表:
[x for x in range(1, 10) if x % 2]
Run Code Online (Sandbox Code Playgroud)
这是一个过滤器 - 我有一个源列表,我删除偶数(if x % 2).我想在这里使用if-then-else之类的东西.以下代码失败:
>>> [x for x in range(1, 10) if x % 2 else x * 100]
File "<stdin>", line 1
[x for x in range(1, 10) if x % 2 else x * 100]
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
有一个像if-else的python表达式:
1 if 0 is 0 else 3
Run Code Online (Sandbox Code Playgroud)
如何在列表理解中使用它?
python list-comprehension ternary-operator conditional-operator