【求助】删除python二维列表中的元素出错,求指点
题目描述
算法描述:
遍历列表Users中的子列表(按照user1, user2顺序),和CSPs中的子列表匹配(用fitfun函数匹配),匹配完以后删除Users和CSPs中匹配成功的元素,剩下的元素继续匹配。
题目来源及自己的思路
算法设计项目
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
import numpy
G = []
def fitfun(user, csp):
fitness = round(numpy.sqrt(pow((user[0] - csp[0]), 2) + pow((user[2] - csp[1]), 2)),3)
return fitness
def allocation(Users, CSPs):
T = []
print('input Users Matrix:', Users)
print('input CSPs Matrix:', CSPs,'\n')
for i, user in enumerate(Users):
for j, csp in enumerate(CSPs):
if (csp[0] > user[0] and csp[1] < user[2]):
T.append(csp)
print('updated2 Users Matrix:', Users)
print('updated2 CSPs Matrix:', CSPs)
print('user', i, 'suitCSPs:', T)
if T:
C = []
for suitcsp in T:
result = fitfun(user, suitcsp)
print('fitness:', result)
C.append([result, user, suitcsp])
print('suitable group:', C,'\n')
minindex = 0
for z, item in enumerate(C):
if item[0] < C[minindex][0]:
minindex = z
print('minindex:', minindex)
print('dealusercsp', [C[minindex][1],C[minindex][2]],'\n')
G.append([C[minindex][1],C[minindex][2]])
print('Users Matrix:', Users)
print('CSPs Matrix:', CSPs,'\n')
Users.remove(C[minindex][1])
CSPs.remove(C[minindex][2])
print('updated Users Matrix:', Users)
print('updated CSPs Matrix:', CSPs)
print('allocation matrix:', G,'\n')
def main():
Users = [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
CSPs = [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
round = 1
print('origin Users Matrix:', Users)
print('origin CSPs Matrix:', CSPs,'\n')
allocation(Users, CSPs)
print('round', round, 'over\n\n')
if __name__ == '__main__':
main()
输出
C:\Users\KING\PycharmProjects\originauction\venv\Scripts\python.exe C:/Users/KING/PycharmProjects/originauction/temp.py
origin Users Matrix: [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
origin CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
input Users Matrix: [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
input CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
updated2 Users Matrix: [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
updated2 CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
user 0 suitCSPs: []
Traceback (most recent call last):
updated2 Users Matrix: [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
updated2 CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
user 1 suitCSPs: [[4, 2.8, 3.1]]
fitness: 1.02
File "C:/Users/KING/PycharmProjects/originauction/temp.py", line 55, in <module>
suitable group: [[1.02, [3, 30, 3, 5.0], [4, 2.8, 3.1]]]
main()
minindex: 0
File "C:/Users/KING/PycharmProjects/originauction/temp.py", line 51, in main
allocation(Users, CSPs)
dealusercsp [[3, 30, 3, 5.0], [4, 2.8, 3.1]]
File "C:/Users/KING/PycharmProjects/originauction/temp.py", line 40, in allocation
CSPs.remove(C[minindex][2])
Users Matrix: [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
ValueError: list.remove(x): x not in list
CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
updated Users Matrix: [[7, 24, 7, 13.6], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
updated CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [5, 3.5, 2.0]]
allocation matrix: [[[3, 30, 3, 5.0], [4, 2.8, 3.1]]]
updated2 Users Matrix: [[7, 24, 7, 13.6], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
updated2 CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [5, 3.5, 2.0]]
user 2 suitCSPs: [[4, 2.8, 3.1]]
fitness: 2.417
suitable group: [[2.417, [5, 34, 5, 7.4], [4, 2.8, 3.1]]]
minindex: 0
dealusercsp [[5, 34, 5, 7.4], [4, 2.8, 3.1]]
Users Matrix: [[7, 24, 7, 13.6], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [5, 3.5, 2.0]]
Process finished with exit code 1
你期待的结果是什么?实际看到的错误信息又是什么?
期待CSPs列表的子列表[4, 2.8, 3.1]匹配后,从CSPs列表中删除,以后不再匹配
实际上删除后依然匹配
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个大忌:当你传入一个可变参数时,不要在后续的代码中对其做修改~ 除非你真正的了解自己在做什么
比如:Users,CSPs
换了个思路解决了吧,遍历列表的时候再删除其中的元素容易出错,所以我用None 填充需要删除的元素,这样列表索引就不会乱了