如何在Python中删除列表中的项但保留原始索引
简单来说,我的程序里有一个列表,当某个项目不符合特定条件时,我会把它移除,然后用一个算法检查这个被移除的项目是否会影响列表中的其他项目。可是我遇到的问题是,当我把这个项目移除后,列表里的其他项目都会往前挪一个位置,这样在用算法检查其他项目时,索引就会不断变化,导致结果出错。下面是我的代码,还有一个运行的例子:
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的位置。
相关文章:
- 暂无相关问题
4 个回答
0
有几种方法可以做到,但这样会让代码看起来更乱。
你可以试试这个:
**remo_ve = [] #**
for i in range(len(assets)-1):
if(assets[i]<=limit):
print("Adding Bank:", i," to the list of unsafe banks.")
unsafe.append(i)
**remo_ve.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)
**for i in remo_ve:
assets.remove(assets[i])
banks.remove(banks[i])**
或者你可以使用不安全的列表,这样就不需要再添加一个新的列表变量了。
0
我建议你使用字典或者类来代替。下面是一个简单的类的设置方法,可以用来创建“银行”:
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...
1
在处理像列表这样的迭代器时,最好不要直接修改迭代器,因为这样会出现问题。你应该先复制一份迭代器,然后在循环中使用这个复制的版本。
所以在你的循环开始时,应该写类似这样的代码:
assets_copy = assets.copy()
然后在循环中进行添加或删除操作时,应该在这个复制的版本上进行。
6
列表的索引总是从 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'}