使用列表中这是一个列表初学者问题与蟒蛇
['lady']
['hen']
['horse']
我需要删除"[","'","]"
,看起来像
lady
hen
horse
我不能使用索引,因为每个单词有不同的长度,我知道我想改变使用替换功能。你能帮我吗?
使用列表中这是一个列表初学者问题与蟒蛇
['lady']
['hen']
['horse']
我需要删除"[","'","]"
,看起来像
lady
hen
horse
我不能使用索引,因为每个单词有不同的长度,我知道我想改变使用替换功能。你能帮我吗?
您可以str.strip()
方法剥去你的元素:
>>> l =["['lady']", "['hen']", "['horse']"]
>>> [i.strip("'][") for i in l]
['lady', 'hen', 'horse']
它看起来好像你可能有嵌套列表。
这是你在追求什么?
>>>animals = ["lady", "hen", "horse"]
>>>for i in animals:
... print i
lady
hen
horse
您可以使用列表理解:
>>>animalsnested = [['pig'], ['cat'], ['dog']]
>>>animals = [i for x in animals for i in x]
>>>animals
['pig', 'cat', 'dog']
好像你有3只列出了那里,你的意思是'[”女士','母鸡','马']' –
@joe没有它喜欢['女'],['母鸡'] ['马']对夫人马母鸡 – hanish
它不清楚你在问什么。这些单个列表是否包含一个字符串,还是包含所有3个列表的较大列表的一部分? –