在List Comprehensions中使用if,elif,else,Python [英] Using if, elif, else in List Comprehensions, Python

查看:445
本文介绍了在List Comprehensions中使用if,elif,else,Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created the following list comprehension in 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]

我在尝试构建最终的else时遇到问题,如果条件:

I'm having issues trying to construct the final else, if condition:

else int(a[0].internal_value) if type(a[0].internal_value) in (float,int)

如果我在该行中使用if条件,则语法无效。

I get an invalid syntax if I use the if conditional in that line.

 if type(a[0].internal_value) in (float,int)

如果我删除if语句

else int(a[0].internal_value)

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

then it seems to run fine. I need to have that if statement in there.

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

To me the else, if conditions are list comprehensions way of doing the more simple if, else conditions:

if i == x:
  do something
elif i == y:
  do something
elif i == z:
  do something

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

By rule, you do not always have to have an 'else' to close a series of conditional sentences. It seems to me, that my code wants a final 'else' in the comprehension. Am I correct in stating that and if so, is there a way to construct a final else, if in a python list comprehension instead of a final else?

推荐答案

你是(ab)使用条件表达式,如果测试其他false_expression ,则必须的格式为 true_expression。这些表达式总是产生一个值,不像 if 复合语句。

You are (ab)using conditional expressions, and they must be of the form true_expression if test else false_expression. These expressions always produce a value, unlike an if compound statement.

注意您应该测试 == True ;没有该测试,布尔表达式已经为true或false。不要使用<> ,该运算符已被弃用并已从Python 3中删除。当测试,一个单身时,你使用不是无然而。

Note that you should not test for == True; boolean expressions are already true or false without that test. Don't use <> either, that operator has been deprecated and has removed from Python 3 altogether. When testing for None, a singleton, you'd use is not None however.

您正在测试 type()结果;看起来你想使用 isinstance()测试

You are testing against type() results; that looks like you want to use isinstance() tests instead.

您还在值上使用 int(),然后在结果上调用 .lower()。没有 int.lower()方法,因此这些调用将失败并带有 AttributeError

You are also using int() on values, then calling .lower() on the result. There is no int.lower() method, so those calls will fail with an AttributeError.

以下更接近工作正常,除非有比 int float str unicode

The following is closer to working just fine, unless there are more types than int, float, str or unicode:

[int(a[0].internal_value) if isinstance(a[0].internal_value, (float, int)) or a[0].internal_value.isdigit() 
 else str(a[0].internal_value).lower()
 for a in ws.iter_rows() if a[0].internal_value is not None]

但是,我会将转换转换为过滤功能:

However, I'd farm out the conversion to filter function instead:

def conversion(value):
    if isinstance(value, (float, int)):
        return int(value)
    return str(value).lower()

然后在列表理解中使用

then use that in a list comprehension:

[conversion(a[0].internal_value) for a in ws.iter_rows() if a[0].internal_value is not None]

这篇关于在List Comprehensions中使用if,elif,else,Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆