Karvy1
Karvy1

Reputation: 1075

'str' object has no attribute 'remove'

I want to remove 362968 from below list-

list=[362976,362974,362971,362968,362969]

code-

list.remove(362968)

I am getting error: 'str' object has no attribute 'remove'

Actual code -

def matchmaker():
exportersfree = exporters[:]
engaged  = {}
exprefers2 = copy.deepcopy(exprefers)
imprefers2 = copy.deepcopy(imprefers)
while exportersfree:
    exporter = exportersfree.pop(0)
    exporterslist = exprefers2[exporter]
    importer = exporterslist.pop(0)
    match = engaged.get(importer)
    if not match:
        # impo's free
        engaged[importer] = exporter #both parties are added to the engaged list
        importerslist = imprefers2[importer]
        for z in range (importerslist.index(exporter)-1):
                    importerslist.index(exporter)
                    exprefers2[importerslist[z]].remove(importer)
        del importerslist[0:(importerslist.index(exporter)-1)]

    else
            engaged[importer] = exporter
            if exprefers2[match]:
                # Ex has more importers to try
                exportersfree.append(match)

return engaged

Upvotes: 2

Views: 43011

Answers (3)

Ellen
Ellen

Reputation: 1

I think you need to check if the X has been changed. x = [1, 40, 33, 20] x.remove(33) print (x)

Upvotes: -1

AdriVelaz
AdriVelaz

Reputation: 563

Without additional code to really debug, exprefers2is clearly a dict of strings; however, if you really want to delete it. You can cast the string to a list, or eval the value to convert it into a list, then use list.remove

import ast

list = [1, 2, 3, 4, 5, 6, 7]
list.remove(5)
print list
#[1, 2, 3, 4, 6, 7]


#Data Structure you most likely have
import_list = [1, 2]
exprefers2 = {1: "abc", 2: "xyz"}
print exprefers2[import_list[1]]
#xyz


#Or need to eval the string of a list
import_list = [1, 2]
exprefers2 = {1: u'[ "A","B","C" , " D"]', 2: u'[ "z","x","y" , " y"]'}
exprefers2[import_list[1]] = ast.literal_eval(exprefers2[import_list[1]])
exprefers2[import_list[1]].remove("y")
print exprefers2[import_list[1]]
#['z', 'x', ' y']

Upvotes: 4

EugenSunic
EugenSunic

Reputation: 13703

Try it in this way then, name your list "a".

 a = [362976,362974,362971,362968,362969]
     a.remove(362968) 
     print a

Upvotes: 1

Related Questions