简体   繁体   English

在列表理解中实现三元条件运算符

[英]Implementing Ternary Conditional Operator in a List-Comprehension

I am trying to implement a ternary conditional operator in a list-comprehension. 我试图在列表理解中实现三元条件运算符。 I have written it like this: 我写得像这样:

lst.append(dict2obj(item)) if type(item) is not in ['int'] else lst.append(item) for item in v

Where lst is empty list and v is another list with various elements. 其中lst是空列表, v是另一个包含各种元素的列表。 Editor is showing it syntactically incorrect. 编辑器在语法上显示它不正确。 What am I doing wrong? 我究竟做错了什么?

  • If you mean to write list comprehension you missed [ , ] : 如果你想写下列表理解你错过了[]
  • There is no is not in operator. 有没有is not in操作。 Use not in . not in使用。
  • type function does not return string . type函数不返回string Why not use isinstance(item, int) ? 为什么不使用isinstance(item, int)

[lst.append(dict2obj(item)) if not isinstance(item, int) else lst.append(item)
 for item in v]

Use simple for loop if possible. 如果可能,使用简单的for循环。 It's more readable. 它更具可读性。

for item in v:
    if not isinstance(item, int)
        lst.append(dict2obj(item))
    else:
        lst.append(item)

If the lst is empty from the start, you can simply create it like this: 如果lst从一开始就是空的,你可以简单地创建它:

lst = [dict2obj(item) if not isinstance(item, int) else item for item in v]

If you already have the list and want to add items, the proper way to do this in Python is to just extend the list you have with the new list: 如果您已经拥有该列表并想要添加项目,那么在Python中执行此操作的正确方法是使用新列表扩展您的列表:

lst.extend([dict2obj(item) if not isinstance(item, int) else item for item in v])

Or something like this (this uses an generator) to prevent extra overhead: 或类似的东西(这使用生成器)来防止额外的开销:

map(lst.append, (dict2obj(item) if not isinstance(item, int) else item for item in v))

I avoid mixing list comprehensions with ternary operators because it's too hard to understand what the function does at a glance. 我避免将列表推导与三元运算符混合在一起,因为它很难理解函数的一目了然。

I also try to use list comprehensions only for building up a list of return values. 我还尝试仅使用列表推导来构建返回值列表。 If I desire side-effects (such as adding items to a list), I will do this in a regular for-loop. 如果我想要副作用(例如将项添加到列表中),我将在常规的for循环中执行此操作。 This is especially true when I don't care about the list of return values. 当我不关心返回值列表时尤其如此。 If you do go the route of a list comprehension, use the consume recipe for itertools ( http://docs.python.org/2/library/itertools.html#recipes ). 如果您确实使用列表itertools的路径,请使用itertools的消耗配方( http://docs.python.org/2/library/itertools.html#recipes )。 consume((lst.append(dict2obj(item)) if not isinstance(item) else lst.append(item) for item in v), None)

Here's how I'd solve this problem if I didn't do use @falsetru's approach (which is probably the easiest to read) 如果我没有使用@fattru的方法(这可能是最容易阅读的),我就是这样解决这个问题的方法

def convert(item):
    if not isinstance(item, int):
        result = dict2obj(item)
    else:
        result = item
    return result

lst.extend(map(convert, v)) #or itertools.imap

convert could be a lambda function if you're willing to trade compactness for readability. 如果您愿意交换紧凑性以便于阅读,则convert可以是lambda函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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