-4
比如我有如何刪除列表中的特定重複號碼?
[3, 4, 5, 5, 12, 13, 0, 0, 0, 8, 15, 17, 0, 0, 0]
我想刪除剛纔的所有零。 注意:假設我們在一個未知長度的列表中有一個未知數目的零。
比如我有如何刪除列表中的特定重複號碼?
[3, 4, 5, 5, 12, 13, 0, 0, 0, 8, 15, 17, 0, 0, 0]
我想刪除剛纔的所有零。 注意:假設我們在一個未知長度的列表中有一個未知數目的零。
列表理解使這個容易:
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
很好的使用'filter'! – Li357
'[榆樹的榆樹在list_var如果榆樹!= 0]' – Li357