Python 2:AttributeError:“list”对象没有“strip”属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17751322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 2: AttributeError: 'list' object has no attribute 'strip'
提问by Michael
I have a small problem with list. So i have a list called l
:
我的列表有一个小问题。所以我有一个名为l
:
l = ['Facebook;Google+;MySpace', 'Apple;Android']
And as you can see I have only 2 strings in my list. I want to separate my list l
by ';'and put my new 5 strings into a new list called l1
.
正如您所看到的,我的列表中只有 2 个字符串。我想l
用';'分隔我的列表 并将我的新 5 个字符串放入一个名为l1
.
How can I do that?
我怎样才能做到这一点?
And also I have tried to do this like this:
而且我也尝试这样做:
l1 = l.strip().split(';')
But Python give me an error:
但是 Python 给了我一个错误:
AttributeError: 'list' object has no attribute 'strip'
So if 'list' object has no attribute 'strip' or 'split', how can I split a list?
因此,如果 'list' 对象没有属性 'strip' 或 'split',我该如何拆分列表?
Thanks
谢谢
采纳答案by Sukrit Kalra
strip()
is a method for strings, you are calling it on a list
, hence the error.
strip()
是字符串的方法,您在 a 上调用它list
,因此出现错误。
>>> 'strip' in dir(str)
True
>>> 'strip' in dir(list)
False
To do what you want, just do
做你想做的,就去做
>>> l = ['Facebook;Google+;MySpace', 'Apple;Android']
>>> l1 = [elem.strip().split(';') for elem in l]
>>> print l1
[['Facebook', 'Google+', 'MySpace'], ['Apple', 'Android']]
Since, you want the elements to be in a single list (and not a list of lists), you have two options.
由于您希望元素位于单个列表(而不是列表列表)中,因此您有两个选择。
- Create an empty list and append elements to it.
- Flatten the list.
- 创建一个空列表并向其追加元素。
- 将列表展平。
To do the first, follow the code:
首先,请按照以下代码进行操作:
>>> l1 = []
>>> for elem in l:
l1.extend(elem.strip().split(';'))
>>> l1
['Facebook', 'Google+', 'MySpace', 'Apple', 'Android']
To do the second, use itertools.chain
要执行第二个操作,请使用 itertools.chain
>>> l1 = [elem.strip().split(';') for elem in l]
>>> print l1
[['Facebook', 'Google+', 'MySpace'], ['Apple', 'Android']]
>>> from itertools import chain
>>> list(chain(*l1))
['Facebook', 'Google+', 'MySpace', 'Apple', 'Android']
回答by Jiminion
You split the string entry of the list. l[0].strip()
您拆分列表的字符串条目。l[0].strip()
回答by ersran9
Split the strings and then use chain.from_iterableto combine them into a single list
拆分字符串,然后使用chain.from_iterable将它们组合成一个列表
>>> import itertools
>>> l = ['Facebook;Google+;MySpace', 'Apple;Android']
>>> l1 = [ x for x in itertools.chain.from_iterable( x.split(';') for x in l ) ]
>>> l1
['Facebook', 'Google+', 'MySpace', 'Apple', 'Android']
回答by ersran9
This should be what you want:
这应该是你想要的:
[x for y in l for x in y.split(";")]
output:
输出:
['Facebook', 'Google+', 'MySpace', 'Apple', 'Android']
回答by svineet
What you want to do is -
你想做的是——
strtemp = ";".join(l)
The first line adds a ;
to the end of MySpace
so that while splitting, it does not give out MySpaceApple
This will join l into one string and then you can just-
第一行在;
of 的末尾添加 aMySpace
以便在拆分时不会发出MySpaceApple
这会将 l 连接成一个字符串,然后您可以-
l1 = strtemp.split(";")
This works because strtemp is a string which has .split()
这是有效的,因为 strtemp 是一个具有 .split() 的字符串
回答by Pawel Miech
Hope this helps :)
希望这可以帮助 :)
>>> x = [i.split(";") for i in l]
>>> x
[['Facebook', 'Google+', 'MySpace'], ['Apple', 'Android']]
>>> z = [j for i in x for j in i]
>>> z
['Facebook', 'Google+', 'MySpace', 'Apple', 'Android']
>>>
回答by Ana Salinas
One possible solution I have tried right now is: (Make sure do it in general way using for, while with index)
我现在尝试过的一种可能的解决方案是:(确保以一般方式使用 for,同时使用索引)
>>> l=['Facebook;Google+;MySpace', 'Apple;Android']
>>> new1 = l[0].split(';')
>>> new1
['Facebook', 'Google+', 'MySpace']
>>> new2= l[1].split(';')`enter code here`
>>> new2
['Apple', 'Android']
>>> totalnew = new1 + new2
>>> totalnew
['Facebook', 'Google+', 'MySpace', 'Apple', 'Android']
回答by PilouPili
You can first concatenate the strings in the list with the separator ';' using the function join
and then use the split
function in order create the list:
您可以先使用分隔符 ';' 连接列表中的字符串 使用该函数join
,然后使用该split
函数来创建列表:
l = ['Facebook;Google+;MySpace', 'Apple;Android']
l1 = ";".join(l)).split(";")
print l1
outputs
产出
['Facebook', 'Google+', 'MySpace', 'Apple', 'Android']
['Facebook'、'Google+'、'MySpace'、'Apple'、'Android']