2009-10-07 116 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