如何在Python中通过索引从列表中删除元素?
我找到了list.remove方法,但是说我要删除最后一个元素,我该怎么做?似乎默认删除搜索列表,但我不希望执行任何搜索.
我有这个程序计算回答特定问题所花费的时间,当答案不正确时退出while循环,但我想删除最后一次计算,所以我可以打电话min(),这不是错误的时间,对不起,如果这令人困惑.
from time import time
q = input('What do you want to type? ')
a = ' '
record = []
while a != '':
start = time()
a = input('Type: ')
end = time()
v = end-start
record.append(v)
if a == q:
print('Time taken to type name: {:.2f}'.format(v))
else:
break
for i in record:
print('{:.2f} seconds.'.format(i))
Run Code Online (Sandbox Code Playgroud) 这些方法的官方Python 2.7文档几乎完全相同,唯一的区别似乎是remove()引发KeyError而discard没有.
我想知道这两种方法之间的执行速度是否存在差异.如果做不到,它们之间是否存在任何有意义的差异(除非出现KeyError)?
为了解决这个问题,我试图遍历 python 中的坐标对列表,并删除其中一个坐标为负的所有情况。例如:
在数组中:
map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
Run Code Online (Sandbox Code Playgroud)
我想删除其中任一坐标 < 0 的所有对,留下:
map = [[2, 3], [7, 1]]
Run Code Online (Sandbox Code Playgroud)
我的问题是 python 列表不能有任何间隙,所以如果我这样循环:
i = 0
for pair in map:
for coord in pair:
if coord < 0:
del map[i]
i += 1
Run Code Online (Sandbox Code Playgroud)
当元素被删除时,所有的索引都会发生变化,扰乱迭代并导致各种问题。我尝试将坏元素的索引存储在另一个列表中,然后循环遍历并删除这些元素,但我遇到了同样的问题:一旦一个元素消失,整个列表就会发生变化,索引不再准确。
有什么我想念的吗?
谢谢。
使用 list: [-1, 0, 43, 128, 32],有几种方法可以删除最后一个元素。
list.pop()list = list[:-1] (不建议?)del list[-1]它们都会返回[-1, 0, 43, 128],但是计算强度最低的是什么,它有什么不同吗?我知道timeit我可以用来为自己测试的模块。但我对不受控制的变量持谨慎态度,我的非专业知识肯定会稀释结果。同样,对于字符串、浮点数或布尔值,最佳选择是否有所不同?多维列表呢?
我不太确定如何控制和测试这些变量,所以我想我会在这里询问是否有一般的层次结构。
该问题解释了删除方法之间的差异,但不涉及切片。它也根本没有解决速度问题。接受的答案含糊地提到了效率,我可以将其视为解决方案的一部分,但我看不出它如何适合切片。
我目前使用 github3.py 版本 0.9.6,并且在调用github3.organization(login)函数时收到错误:
Traceback (most recent call last):
File "Main.py", line 23, in <module>
__main__()
File "Main.py", line 19, in __main__
Stats.git_auth(username, password, access_token)
File "/Users/edigiovine/Repositories/GitMetrics/Stats.py", line 36, in git_auth
git_orgs(gh)
File "/Users/edigiovine/Repositories/GitMetrics/Stats.py", line 49, in git_orgs
org = gh.organization(rel_org)
File "/Library/Python/2.7/site-packages/github3/github.py", line 971, in organization
return Organization(json, self) if json else None
File "/Library/Python/2.7/site-packages/github3/orgs.py", line 236, in __init__
super(Organization, self).__init__(org, session)
File "/Library/Python/2.7/site-packages/github3/models.py", line 311, in __init__
super(BaseAccount, self).__init__(acct, session)
File "/Library/Python/2.7/site-packages/github3/models.py", line 77, in __init__ …Run Code Online (Sandbox Code Playgroud) Say I have a 2d list like this:
list = [[1, 2, 3], ["a", 5, 6], [7, 8, 9], [10, 11, 12]]
and I want to delete a list that starts with a. Here is what I tried:
for i in range(0, len(list)):
if list[i][0] == "a":
list.pop(i)
Run Code Online (Sandbox Code Playgroud)
Here, I am trying to delete a list that starts with a. However, it gives me an error saying
IndexError: list index out of range
I am not sure what …
在python中,假设我们有以下列表:
my_list = ['a', 'b', 'c', 'd', ...]
会my_list.pop(3)比效率更高my_list.remove('d')吗?
我的目标是遍历列表并返回列表中的每个元素,每次迭代的当前索引除外。
例如:
my_list = [1,2,3]
应该返回:
>>>[2,3]
>>>[1,3]
>>>[1,2]
Run Code Online (Sandbox Code Playgroud)
我已经尝试创建一个 for 循环来创建列表的副本,从副本中弹出当前索引并返回它。
my_list = [1,2,3]
for a in my_list:
the_index = my_list.index(a)
print(my_list.copy().pop(the_index))
Run Code Online (Sandbox Code Playgroud)
返回
>>>1
>>>2
>>>3
Run Code Online (Sandbox Code Playgroud)
关于如何正确实施这一点的任何想法?
list = [0, 1, 2, 8, 2, 9, 2]
Run Code Online (Sandbox Code Playgroud)
有没有办法删除元素2,恰好一次?
所以你会得到:
list = [0, 1, 2, 8, 9, 2]
Run Code Online (Sandbox Code Playgroud)
我曾尝试过,index()但我没有找到它.
它可以是随机的2.
所以我不能使用remove()或pop()因为它不会删除2随机位置上的数字.
python ×9
list ×5
python-3.x ×3
del ×1
github3.py ×1
indexing ×1
indices ×1
optimization ×1
performance ×1
runtime ×1
set ×1
time ×1