2016-10-01 34 views

回答

4

列表理解使這個容易:

new_list = [x for x in orig_list if x != 0] 

你可以用filter推動工作,以C層:

# If they're all numbers, you can avoid work by using filter with None: 
new_list = list(filter(None, orig_list)) # List wrapper not needed on Py2 

# If falsy values that aren't numeric zero might be found, and should be kept, you'd do: 
new_list = list(filter((0).__ne__, orig_list)) # List wrapper not needed on Py2 
+0

很好的使用'filter'! – Li357