2009-10-07 119 views
20

列表除非是空的。我已经写了很多结构的这样过去几天的:对于蟒蛇

list = get_list() 
if list: 
    for i in list: 
     pass # do something with the list 
else: 
    pass # do something if the list was empty 

垃圾很多,我的列表分配到一个真正的变量(保持在内存中比需要更长)。 Python已经简化了很多我的代码直到现在...有没有一种简单的方法来做到这一点?

(我的理解是,在for: else:结构的else总是触发后,它已经循环,为空或不 - 所以不是我想要的)

+2

我不确定任何事情**可能会更容易。这有什么问题?您想要消除哪一行代码? – 2009-10-07 12:54:37

+0

呃...理想情况下,我想停止将列表分配给变量,并将if/else压缩为for的一部分(我知道这不太可能)。我可以使用'get_list()作为列表:',但是这会将事情推得更远 – Oli 2009-10-07 12:57:13

+1

@Oli:请不要评论你自己的问题。请*更新*您的问题与其他细节。 – 2009-10-07 12:59:44

回答

8

使用列表理解:

def do_something(x): 
    return x**2 

list = [] 
result = [do_something(x) for x in list if list] 
print result  # [] 

list = [1, 2, 3] 
result = [do_something(x) for x in list if list] 
print result  # [1, 4, 9] 
+9

最后的“if list”是不必要的,并且对每个项目进行评估,而不仅仅是一次。 – FogleBird 2009-10-07 13:35:31

+0

好点,感谢您的更正! – 2009-10-07 13:40:49

+2

你的理解不处理list = None – 2009-10-07 21:21:57

2
def do_something_with_maybe_list(maybe_list): 
    if maybe_list: 
     for x in list: 
      do_something(x) 
    else: 
     do_something_else() 

do_something_with_maybe_list(get_list()) 

你甚至可以提取的行动来完成:

从奥利
def do_something_with_maybe_list(maybe_list, process_item, none_action): 
    if maybe_list: 
     for x in list: 
      process_item(x) 
    else: 
     none_action() 

do_something_with_maybe_list(get_list(), do_something, do_something_else) 
do_something_with_maybe_list(get_otherlist(), do_other, do_still_other) 

编辑:或者去一个进一步:

def do_something_with_maybe_list(maybe_list, process_item, none_action): 
    if maybe_list: 
     return process_list(maybe_list) 
    return none_action() 

do_something_with_maybe_list(get_list(), do_something, do_something_else) 
do_something_with_maybe_list(get_otherlist(), do_other, do_still_other) 
+0

也许你真的想要ML。 :) – 2009-10-07 12:56:52

+0

增加了另一层下剥离。处理可以(也可以不对)在外部完成。我需要返回,所以我可以删除'else',guard-style – Oli 2009-10-07 13:02:18

4

Slighty更简洁:

for i in my_list: 
    # got a list 
if not my_list: 
    # not a list 

假设你没有改变循环中列表的长度。从奥利

编辑:为了弥补我的记忆中使用的后顾之忧,它希望with ING:

with get_list() as my_list: 
    for i in my_list: 
     # got a list 
    if not my_list: 
     # not a list 

但是,是的,这是相当解决这个问题的简单方法。

2

如果你的行为是不同,我会做:

list_ = get_list() # underscore to keep built-in list 
if not list_: 
    # do something 
for i in list_: # 
    # do something for each item 

如果你的行为是类似,这是更美观:

for i in list_ or [None]: 
    # do something for list item or None 

或者,如果你可能有None作为列表元素,

for i in list_ or [...]: 
    # do something for list item or built-in constant Ellipsis 
27

基于其他的答案,我认为最干净的解决方案是

#Handles None return from get_list 
for item in get_list() or []: 
    pass #do something 

或理解当量

result = [item*item for item in get_list() or []] 
1

我认为你的方式在一般情况下是可以的,但你可以考虑这种方法:

def do_something(item): 
    pass # do something with the list 

def action_when_empty(): 
    pass # do something if the list was empty 

# and here goes your example 
yourlist = get_list() or [] 
another_list = [do_something(x) for x in yourlist] or action_when_empty() 
-1
i = None 
for i in get_list(): 
    pass # do something with the list 
else: 
    if i is None: 
     pass # do something if the list was empty 

这有帮助吗?是的,我知道我们距离需求两年:-)

+1

如果get_list()中的最后一个元素实际上是None,这将不起作用。 – Danosaure 2012-11-16 17:46:49