Python 2: AttributeError: 'list'对象没有'strip'属性

时间:2020-12-25 18:18:50

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.

可以看到,我的列表中只有两个字符串。我想把我的列表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

谢谢

7 个解决方案

#1


14  

strip() is a method for strings, you are calling it on a list, hence the error.

strip()是一个字符串方法,您正在一个列表上调用它,因此出现了错误。

>>> '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.

因为,您希望元素位于一个列表(而不是列表列表)中,所以您有两个选项。

  1. Create an empty list and append elements to it.
  2. 创建一个空列表并向其添加元素。
  3. Flatten the list.
  4. 压平。

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']

#2


2  

This should be what you want:

这应该是你想要的:

[x for y in l for x in y.split(";")]

output:

输出:

['Facebook', 'Google+', 'MySpace', 'Apple', 'Android']

#3


2  

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']
>>> 

#4


1  

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-

第一行加a;在MySpace结束时,它不会释放MySpaceApple这个会把l连接到一个字符串然后你可以

l1 = strtemp.split(";")

This works because strtemp is a string which has .split()

这样做是因为strtemp是一个具有.split()的字符串

#5


1  

One possible solution I have tried right now is: (Make sure do it in general way using for, while with index)

我现在尝试的一个可能的解决方案是:(在使用索引时,一定要用一般的方法)

>>> 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']

#6


0  

You split the string entry of the list. l[0].strip()

分割列表的字符串条目。l[0].strip()

#7


0  

Split the strings and then use chain.from_iterable to 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']

#1


14  

strip() is a method for strings, you are calling it on a list, hence the error.

strip()是一个字符串方法,您正在一个列表上调用它,因此出现了错误。

>>> '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.

因为,您希望元素位于一个列表(而不是列表列表)中,所以您有两个选项。

  1. Create an empty list and append elements to it.
  2. 创建一个空列表并向其添加元素。
  3. Flatten the list.
  4. 压平。

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']

#2


2  

This should be what you want:

这应该是你想要的:

[x for y in l for x in y.split(";")]

output:

输出:

['Facebook', 'Google+', 'MySpace', 'Apple', 'Android']

#3


2  

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']
>>> 

#4


1  

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-

第一行加a;在MySpace结束时,它不会释放MySpaceApple这个会把l连接到一个字符串然后你可以

l1 = strtemp.split(";")

This works because strtemp is a string which has .split()

这样做是因为strtemp是一个具有.split()的字符串

#5


1  

One possible solution I have tried right now is: (Make sure do it in general way using for, while with index)

我现在尝试的一个可能的解决方案是:(在使用索引时,一定要用一般的方法)

>>> 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']

#6


0  

You split the string entry of the list. l[0].strip()

分割列表的字符串条目。l[0].strip()

#7


0  

Split the strings and then use chain.from_iterable to 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']