简体   繁体   English

从列表中删除一个数字

[英]removing a number from a list

I'm stuck on a part of a looping question.我被困在一个循环问题的一部分上。 I have to remove from list1 all instances of "number".我必须从 list1 中删除“number”的所有实例。 So lets say list1 is (1,2,3) and num is 2. The list I would have to return would be (1,3)所以让我们说 list1 是 (1,2,3) 和 num 是 2。我必须返回的列表是 (1,3)

def remove(list1,num)

    list1=list1
    num = num

This is what is given.这就是所给的。 So far, I have this:到目前为止,我有这个:

def remove(list1,num)

list1=list1

num=num

if num in list1:

This is where I'm stuck because I don't know how to say in coding "remove num from list" I am not allowed to use .remove either.这就是我被卡住的地方,因为我不知道如何在编码中说“从列表中删除 num”,我也不允许使用 .remove。

Would appreciate the help.将不胜感激的帮助。

It sounds like this is a homework problem, especially since you can't use .remove .听起来这是一个家庭作业问题,尤其是因为您不能使用.remove

Given that, your teacher probably wants you to take a manual approach that looks something like this:鉴于此,您的老师可能希望您采用如下所示的手动方法:

  1. Create a new list创建一个新列表
  2. For each item in the previous list...对于上一个列表中的每个项目...
    1. If it's not the value you want to filter out, .append it to your new list如果它不是您要过滤掉的值,请将其.append到您的新列表中
  3. Return your new list返回您的新列表

(mouseover if you don't want to write the code yourself) (如果你不想自己写代码,鼠标悬停)

\ndef remove(list1, num): def 删除(list1, num):\n    new_list = []新列表 = []\n    for item in list1:对于列表 1 中的项目:\n        if item != num:如果项目 != num:\n            new_list.append(item) new_list.append(item)\n    return new_list返回新列表

Use list comprehension:使用列表理解:

list1 = [1,2,3]
num = 2
new_list = [i for i in list1 if i != num]
print(new_list)
>> [1,3]
def remove(item,given_list):
    new_list = []
    for each in given_list:
        if item != each:
            new_list.append(each)
    return new_list

print(remove(2,[1,2,3,1,2,1,2,2,4]))
#outputs [1, 3, 1, 1, 4]

While my answer as strong as others, I feel this is the fundamental way of thinking on how to remove an item from a list.虽然我的回答和其他人一样强烈,但我觉得这是思考如何从列表中删除项目的基本方式。 It allows people to understand at a basic level of what's happening.它使人们能够从基本层面了解正在发生的事情。

Basically we are taking 2 inputs, the item to remove and the list to remove it from.基本上我们采用 2 个输入,要删除的项目和要从中删除的列表。 It loops through the input_list and checks if the item is equal to the item we want to remove, if it's not the same we append it to the new list, and return the new list.它循环遍历input_list并检查该item是否与我们要删除的item相同,如果不相同,我们appendappend到新列表中,并返回新列表。

We don't want to remove items in place in the list while looping because it could cause undesirable looping.我们不想在循环时删除列表中的项目,因为它可能会导致不受欢迎的循环。 For example if we have the example_list=[1,2,3] and we are on the second iteration of the for loop and we remove 2 in place, it will try to go somewhere we don't want it to go.例如,如果我们有example_list=[1,2,3]并且我们在for loop的第二次迭代中并且我们删除了 2 ,它将尝试去我们不希望它去的地方。

Taking into account:考虑在内:

list=[1,2,3,2]

You can check if the element is in the list with:您可以使用以下命令检查元素是否在列表中:

if num in list

And then remove with:然后删除:

list.remove(num)

And iterate through it并遍历它

Example:例子:

>>> list=[1,2,3]
>>> list.remove(2)
>>> print list
[1, 3]

To go with pure loops and delete with list index:要使用纯循环并使用列表索引删除:

#!/usr/bin/env python
from __future__ import print_function

def remove(item, old_list):
    while True:
        try:
            # find the index of the item
            index = old_list.index(item)
            # remove the item found
            del old_list[index]
        except ValueError as e:
            # no more items, return
            return old_list

a_list = [1, 2, 3, 2, 1, 3, 2, 4, 2]
print(remove(2, a_list))

If possible, of course, you should use list comprehension, which is pythonic and much easier!如果可能的话,当然,您应该使用列表推导式,这是 Pythonic 并且更容易!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM