相关疑难解决方法(0)

在List Comprehensions中使用if,elif,else,Python

我在python中创建了以下列表解析:

[int(a[0].internal_value).lower() if type(a[0].internal_value) in (str,unicode) and a[0].internal_value.isdigit() == True
 else str(a[0].internal_value).lower() if type(a[0].internal_value) in (str,unicode)
 else int(a[0].internal_value) if type(a[0].internal_value) in (float,int)
 for a in ws.iter_rows() if a[0].internal_value <> None]
Run Code Online (Sandbox Code Playgroud)

我有问题试图构建最终的else,如果条件:

else int(a[0].internal_value) if type(a[0].internal_value) in (float,int)
Run Code Online (Sandbox Code Playgroud)

如果我在该行中使用if条件,则会得到无效的语法.

 if type(a[0].internal_value) in (float,int)
Run Code Online (Sandbox Code Playgroud)

如果我删除if语句

else int(a[0].internal_value)
Run Code Online (Sandbox Code Playgroud)

然后似乎运行正常.我需要在那里有if语句.

给我其他的,如果条件是列表理解方式做更简单的if,else条件:

if i == x:
  do something
elif i == y:
  do something
elif i == z:
  do something
Run Code Online (Sandbox Code Playgroud)

按照规则,你并不总是需要一个'else'来关闭一系列条件句.在我看来,我的代码想要理解中的最终"其他".我是否正确陈述,如果是这样,有没有办法构建最终的其他,如果在python列表理解而不是最终的其他?

python if-statement list-comprehension

2
推荐指数
1
解决办法
5570
查看次数

标签 统计

if-statement ×1

list-comprehension ×1

python ×1