我想知道如何将未删除的所有项目附加到新列表中。
challenge = [1, 0, 9, 8, 5, 4, 1, 9, 3, 2, 3, 5, 6, 9]
def remove_values(thelist, value):
newlist = []
while value in thelist:
thelist.remove(value)
newlist.append()
bye = remove_values(challenge, max(challenge))
例如,如果我删除所有9个(最大值),我如何将其余的附加到新列表中?
答案 0 :(得分:0)
challenge = [1, 0, 9, 8, 5, 4, 1, 9, 3, 2, 3, 5, 6, 9]
# This will append every item to a new List where the value not is max
# You won't need 2 lists to achieve what you want, it can be done with a simple list comprehension
removed_list = [x for x in challenge if x != max(challenge)]
print(removed_list)
# will print [1, 0, 8, 5, 4, 1, 3, 2, 3, 5, 6]