我怎么能从这个清单中分离出来?

时间:2010-12-05 16:54:42

标签: python list arraylist

text = [('good', 'Title meta tag contains no errors.'), ('good', 'Title relevancy to page content is good.'), ('good', 'Description meta tag contains no errors.'), ('good', 'Description relevancy to page content is excellent.'), ('good', 'Keywords meta tag contains no errors.'), ('good', 'Keyword relevancy to page content is excellent.'), ('good', 'The Robots meta tag contains no errors.'), ('good', 'The Author meta tag contains no errors.'), ('good', 'The size of the web page.'), ('good', 'The web page load time.')]

问题是如何将列表分开到

text = ['good', 'Title meta tag contains no errors.', 'good', 'Title relevancy to page content is good.', 'good', 'Description meta tag contains no errors.', 'good', 'Description relevancy to page content is excellent.', 'good', 'Keywords meta tag contains no errors.', 'good', 'Keyword relevancy to page content is excellent.', 'good', 'The Robots meta tag contains no errors.', 'good', 'The Author meta tag contains no errors.', 'good', 'The size of the web page.', 'good', 'The web page load time.']

任何答案?

2 个答案:

答案 0 :(得分:5)

这可以给你你想要的东西:

result = [y for x in text for y in x]

您可能会发现更具可读性的另一种选择是使用itertools.chain

from itertools import chain
result = list(chain(*text))

答案 1 :(得分:1)

看看这个问题,非常好的解决方案:

这样的事情应该有效:

>>> a=[(1,2),(3,4),(5,6)]
>>> [i for sl in a for i in sl]
[1, 2, 3, 4, 5, 6]