简体   繁体   中英

Can you use ifinstance in a list comprehension in python?

I am trying to run this line of code,

 revert = [chr(intchange[i]).lower() if isinstance(intchange[i], int) for i in range(len(data))]

but it is giving me this error

    revert = [chr(intchange[i]).lower() if isinstance(intchange[i], int) for i in range(len(data))]
                                                                         ^
SyntaxError: invalid syntax

anyone know if this is a glitch or something, I really don't understand why this wouldn't work?! Thank you so much! Also, I am really trying to keep this in one line...

我认为你需要添加else ,试试这个。

revert = [chr(intchange[i]).lower() if isinstance(intchange[i], int) else intchange[i].lower() for i in range(len(data))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM