'str'对象没有属性'remove'

时间:2015-07-04 00:10:44

标签: list python-2.7

我想从列表中删除362968 -

列表= [362976,362974,362971,362968,362969]

代码 -

list.remove(362968)

我收到错误:'str'对象没有属性'remove'

实际代码 -

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

3 个答案:

答案 0 :(得分:3)

没有额外的代码来真正调试,exprefers2显然是一个字符串的字典;但是,如果你真的想删除它。您可以将字符串转换为列表,或者将值转换为将其转换为列表,然后使用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']

答案 1 :(得分:1)

以这种方式尝试,然后命名您的列表" a"。

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

答案 2 :(得分:-1)

我认为您需要检查X是否已更改。 x = [1、40、33、20] x。删除(33) 打印(x)