【问题标题】:Python how to minus a string from a list of string? [duplicate]Python如何从字符串列表中减去一个字符串? [复制]
【发布时间】:2016-07-10 10:31:38
【问题描述】:

我正在做一个迷你项目,我遇到了标题中建议的问题。下面是一个例子:

list = ["bird", "deer", "dog", "bat"]
list -= "bird"
print list

错误提示:

TypeError: -=: 'list' 和 'str' 的操作数类型不受支持

这似乎是一个无所不在的问题,所以我搜索了它,但找不到相关答案,如果这个问题对本网站上的其他人重复出现,非常抱歉。非常感谢您的帮助!

【问题讨论】:

    标签: python string list python-2.x


    【解决方案1】:

    使用list.remove()

    例如:

    example_lst = [123, 'xyz', 'zara', 'abc', 'xyz']; 
    example_lst.remove('xyz');
    print "List : ", example_lst
    example_lst.remove('abc');
    print "List : ", example_lst
    

    输出:

    List :  [123, 'zara', 'abc', 'xyz']
    List :  [123, 'zara', 'xyz']
    

    上面的例子取自tutorialspoint.com

    【讨论】:

      【解决方案2】:

      你可以使用删除:

      list.remove("bird")
      

      【讨论】:

        猜你喜欢
        • 2019-07-27
        • 1970-01-01
        • 1970-01-01
        • 2016-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多