[英]How to remove an item from a list but keep original indices in Python
所以基本上在我的程序中,在我的列表中的某个项目不满足某个条件后,我将其取出,然后通过算法运行它以检查被取出的项目是否影响列表中的其余项目。 然而,我的问题是,当我取出这个项目时,它会将我所有其他列表项目推回 1 个索引,所以当我去检查它是否使用我的算法影响其他项目时,它搞砸了,因为索引在迭代中不断变化for 循环。 这是我的代码,我将展示一个运行示例:
for l in range(len(assets)-1):
print("The current unsafe banks are:",unsafe)
print("Current assets of the banks which are not yet in unsafe list:")
for h in range(len(assets)):
print("Bank ", h ," Current assets: ",assets[h]," millions.")
print()
for i in range(len(assets)-1):
if(assets[i]<=limit):
print("Adding Bank:", i," to the list of unsafe banks.")
unsafe.append(i)
assets.remove(assets[i])
banks.remove(banks[i])
i=float(i)
for j in range(len(banks)):
for k in range(len(banks[j])-1):
if i == banks[j][k]:
banks[j][k+1]=0
assets=current_Assets(banks)
print(banks)
我的问题是我如何制作它,以便在我从中删除和删除项目后,列表会保留项目的原始索引,而不是将其推回。 谢谢你。
示例运行:
未列入不安全名单的银行流动资产:
Bank 0 Current assets: 446.0 millions.
Bank 1 Current assets: 250.0 millions.
Bank 2 Current assets: 269.0 millions.
Bank 3 Current assets: 200.0 millions.
Bank 4 Current assets: 375.0 millions.
Bank 5 Current assets: 250.0 millions.
Bank 6 Current assets: 280.0 millions.
Adding Bank: 3 to the list of unsafe banks.
[
[25.0, 1.0, 100.5, 4.0, 320.5],
[125.0, 2.0, 40.0, 3.0, 0],
[69.0, 0.0, 125.0, 3.0, 0],
[250.0, 2.0, 125.0],
[100.0, 0.0, 80.0, 4.0, 70.0],
[150.0, 1.0, 10.0, 2.0, 80.0, 3.0, 0]
]
The current unsafe banks are: [3]
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 446.0 millions.
Bank 1 Current assets: 165.0 millions.
Bank 2 Current assets: 194.0 millions.
Bank 3 Current assets: 375.0 millions.
Bank 4 Current assets: 250.0 millions.
Bank 5 Current assets: 240.0 millions.
如您所见,它将银行 4 推回到银行 3。
列表索引始终为0
到len(lst) - 1
。 因此,当您从列表中删除一个项目时, len(lst)
在您删除的项目更新后减少一个和所有索引。 没有办法改变这种行为; 它完全是设计使然,因此您可以正确地迭代列表的项目。
如果您需要静态索引,那么您应该查看字典。 在那里,你可以使用任何你想要的键,包括数字,所以如果你从字典中删除一个项目,它只是被删除,但不会影响其他项目。 一把钥匙就不见了。
>>> banks = {
0: 'bank 0',
1: 'bank 1',
2: 'bank 2',
3: 'bank 3'
}
>>> banks
{0: 'bank 0', 1: 'bank 1', 2: 'bank 2', 3: 'bank 3'}
>>> del banks[2] # remove bank 2
>>> banks
{0: 'bank 0', 1: 'bank 1', 3: 'bank 3'}
在处理像列表这样的迭代器时,最好不要像您遇到的那样弄乱迭代器。 应该制作迭代器的副本,并且应该在循环中使用它。 所以在你的循环开始时应该有类似的东西:
assets_copy = assets.copy()
并且循环中的任何追加或删除都应该在副本上完成。
我建议改用 dict 和/或类。 以下是如何设置一个基本类来创建“银行”:
class bank():
def __init__(self, name):
self.name = name
self.currentAssets = 0
self.safe = True
def foo()
limit = 275.0 # some arbitrary threshold, modify as needed
# Initialize your list of banks:
banks = [bank('Bank ' + str(i)) for i in range(7)]
# assign their properties
# you may be reading this from a stream or another input, modify as needed
banks[0].currentAssets = 446.0
banks[1].currentAssets = 250.0
banks[2].currentAssets = 269.0
banks[3].currentAssets = 200.0
banks[4].currentAssets = 375.0
banks[5].currentAssets = 250.0
banks[6].currentAssets = 280.0
# flag the unsafe banks:
for b in banks:
b.safe = (b.currentAssets < limit)
if b.safe:
# do something to the 'safe' banks
else:
print '{} is unsafe with assets: {} millions which is less than the limit of {}'.format(b[i].name, b.currentAssets, limit)
# etc...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.