List “str”对象没有属性“remove”

List “str”对象没有属性“remove”,list,python-2.7,List,Python 2.7,我想从下面的列表中删除362968- 列表=[362976362974362971362968362969] 代码- 我收到错误:“str”对象没有属性“remove” 实际代码- def matchmaker(): exportersfree = exporters[:] engaged = {} exprefers2 = copy.deepcopy(exprefers) imprefers2 = copy.deepcopy(imprefers) while exportersfree:

我想从下面的列表中删除362968-

列表=[362976362974362971362968362969]

代码-

我收到错误:“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

这样试试吧,把你的清单命名为a

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

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

我想你需要检查一下X是否被改变了。 x=[1,40,33,20] x、 移除33
打印x

del list[3],尝试此操作。我希望通过引用项目本身来删除项目。嗯,这不是真的。这两行代码在Python2.7中运行良好。还可以将列表名更改为列表以外的名称,因为这是列表构造函数。@CristiFati我的列表名为expreferes2[importerslist[z]],expreferes2是字典,importerslist是列表,或者列表=“[362976362974362971362968362969]”是否可能?请注意方括号周围的引号。我的列表名为expreferes2[importerslist[z]],expreferes2是一个字典,importerslist是一个列表
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']