[help] error deleting elements in python 2D list, ask for advice

topic description


algorithm description:

traverses the sublists in the list Users (in user1, user2 order) and matches the sublists in CSPs (using the fitfun function). After matching, delete the elements that match successfully in Users and CSPs, and continue to match the remaining elements.

sources of topics and their own ideas

algorithm design project

related codes

/ / Please paste the code text below (do not replace the code with pictures)

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()

output

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

what result do you expect? What is the error message actually seen?

expect the sublist of CSPs list [4,2.8,3.1] to match, then delete it from the CSPs list, and no longer match it in the future

actually still matches after deletion

Mar.28,2021

A big taboo: when you pass in a variable parameter, do not modify it in subsequent code ~ unless you really know what you are doing
for example: Users,CSPs


solve it in a different way. It is easy to make mistakes when traversing the list, so I use None to fill in the elements that need to be deleted, so the list index will not be messed up


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])



T.remove(C[minindex][2]); -sharp


print('updated Users Matrix:', Users)
print('updated CSPs Matrix:', CSPs)
print('allocation matrix:', G,'\n')
.
  • Python algorithm course

    when reading Magnus Lie Hetland s python algorithm tutorial, there are some questions. Chapter 2 after-class exercise 2-2 Let s make an assumption (which may be a little impractical): if we allow uninitialization when allocating memory (that is, thi...

    Mar.21,2021
  • Php algorithm-- catching water droplets

    php algorithm-- catching water droplets A novice PHP programmer, who has been on the job for half a year and is still hovering in endless additions, deletions, modifications and data optimization, has recently been brought into the algorithm pit by th...

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3725d-2c085.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3725d-2c085.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?