从列表中删除字符串 [英] Removing a string from a list

查看:314
本文介绍了从列表中删除字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个列表,我想从其中删除一个字符串.

I create a list, and I want to remove a string from it.

例如:

>>> myList = ['a', 'b', 'c', 'd']   
>>> myList = myList.remove('c')
>>> print(myList)
None

我在这里做错了什么?我只想从myList中删除'c'

What am I doing wrong here? All I want is 'c' to be removed from myList!

推荐答案

我不确定a是什么(我猜是另一个列表),您应该单独做myList.remove(),而不要赋值.

I am not sure what a is (I am guessing another list), you should do myList.remove() alone, without assignment.

示例-

>>> myList = ['a', 'b', 'c', 'd']
>>> myList.remove('c')
>>> myList
['a', 'b', 'd']

myList.remove()不返回任何内容,因此当您执行myList = <anotherList>.remove(<something>)时,它将myList设置为None

myList.remove() does not return anything, hence when you do myList = <anotherList>.remove(<something>) it sets myList to None

这篇关于从列表中删除字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆