Python:根据条目的位置删除列表中的单个条目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33443163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:21:52  来源:igfitidea点击:

Python: Remove a single entry in a list based on the position of the entry

pythonlistvariables

提问by

Is there an easy way to delete an entry in a list? I would like to only remove the first entry. In every forum that I have looked at, the only way that I can delete one entry is with the list.remove()function. This would be perfect, but I can only delete the entry if I know it's name.

有没有一种简单的方法可以删除列表中的条目?我只想删除第一个条目。在我看过的每个论坛中,我可以删除一个条目的唯一方法是使用该list.remove()功能。这将是完美的,但如果我知道它的名称,我只能删除该条目。

    list = ['hey', 'hi', 'hello', 'phil', 'zed', 'alpha']
    list.remove(0)

This doesn't work because you can only remove an entry based on it's name. I would have to run list.remove('hey'). I can't do this in this particular instance.

这不起作用,因为您只能根据名称删除条目。我不得不跑list.remove('hey')。在这种特殊情况下,我不能这样做。

If you require any additional information, ask.

如果您需要任何其他信息,请询问。

采纳答案by Hackaholic

These are methods you can try:

这些是您可以尝试的方法:

>>> my_list = ['hey', 'hi', 'hello', 'phil', 'zed', 'alpha']
>>> del my_list[0]
>>> my_list = ['hey', 'hi', 'hello', 'phil', 'zed', 'alpha']
>>> if 'hey' in my_list:     # you're looking for this one I think
...     del my_list[my_list.index('hey')]
... 
>>> my_list
['hi', 'hello', 'phil', 'zed', 'alpha']

You can also use filter:

您还可以使用filter

 my_list = filter(lambda x: x!='hey', my_list)

Using list comprehension:

使用list comprehension

my_list = [ x for x in my_list if x!='hey']

回答by RobertB

First of all, never call something "list" since it clobbers the built-in type 'list'. Second of all, here is your answer:

首先,永远不要调用“列表”,因为它破坏了内置类型“列表”。其次,这是你的答案:

>>> my_list = ['hey', 'hi', 'hello', 'phil', 'zed', 'alpha']
>>> del my_list[1]
>>> my_list
['hey', 'hello', 'phil', 'zed', 'alpha']

回答by Dimitris Fasarakis Hilliard

Lists work with positions, not keys(or names, whatever you want to call them).

列表与位置一起工作,而不是(或名称,无论你想怎么称呼它们)。

If you need named accessto your data structure consider using a dictionary instead which allows access to its value by using keyswhich map to the values.

如果您需要对数据结构进行命名访问,请考虑使用字典,它允许通过使用keyswhich 映射到values.

d = {'hey':0, 'hi':0, 'hello':0, 'phil':0, 'zed':0, 'alpha':0}

del d['hey']

print(d)  # d = {'alpha': 0, 'hello': 0, 'hi': 0, 'phil': 0, 'zed': 0}

Otherwise you will need to resort to index based deletion by getting the index of the element and calling del alist[index].

否则,您将需要通过获取元素的索引并调用del alist[index].

回答by flamenco

To add to the poll of answers..how about:

添加到答案投票中……怎么样:

>>> my_list = ['hey', 'hi', 'hello', 'phil', 'zed', 'alpha']
>>> my_list=my_list[1:]
>>> my_list
['hi', 'hello', 'phil', 'zed', 'alpha']