如何将列表中的元素合并在一起?

时间:2015-05-14 21:28:01

标签: python list

我有这个句子列表,我想将它们合并在一起创建一个完整的列表。

test = [['Hello my name is Py. How are you today?'],['The world is a great place. Another sentence.']]

如何合并元素以创建它?

test = ['Hello my name is Py. How are you today? The world is a great place. Another sentence.']

test = 'Hello my name is Py. How are you today? The world is a great place. Another sentence.'

由于

5 个答案:

答案 0 :(得分:2)

您可以使用itertools.chain链接每个子列表中的元素,在链对象上调用str.join以创建单个字符串。

test = [['Hello my name is Py. How are you today?'],['The world is a great place. Another sentence.']]

from itertools import chain

print(" ".join(chain.from_iterable(test)))
Hello my name is Py. How are you today? The world is a great place. Another sentence

或者只是使用join:

print(" ".join(["".join(sub) for sub in test]))
Hello my name is Py. How are you today? The world is a great place. Another sentence.

如果每个子列表中只有一个sting,只需索引:

print(" ".join([sub[0] for sub in test]))

Hello my name is Py. How are you today? The world is a great place. Another sentence.

要获取列表,只需将联接包装在列表中:

print([" ".join([sub[0] for sub in test])])
['Hello my name is Py. How are you today? The world is a great place. Another sentence.']

如果每个子列表中有很多子串,那么链将是最有效的解决方案。

答案 1 :(得分:1)

>>> test = [['Hello my name is Py. How are you today?'],
...         ['The world is a great place. Another sentence.']]
>>>
>>> print '\n'.join(a for b in test for a in b)
Hello my name is Py. How are you today?
The world is a great place. Another sentence.

>>>
>>> print ' '.join(a for b in test for a in b)
Hello my name is Py. How are you today? The world is a great place. Another sentence.

答案 2 :(得分:1)

连接列表,然后加入字符串:

' '.join(sum(test, []))

演示:

>>> test = [['Hello my name is Py. How are you today?'],['The world is a great place. Another sentence.']]
>>> ' '.join(sum(test, []))
'Hello my name is Py. How are you today? The world is a great place. Another sentence.'

警告:虽然这对于一些列表来说很简洁,但是你拥有的列表越多,它就越慢:

>>> for n in (10, 100, 1000, 10000, 100000):
        lists = [['Test'] for _ in range(n)]
        seconds = timeit(lambda: sum(lists, []), number=1)
        print('%10.7f' % seconds, 'seconds for', n, 'lists')


 0.0000109 seconds for 10 lists
 0.0001052 seconds for 100 lists
 0.0053068 seconds for 1000 lists
 0.5582595 seconds for 10000 lists
55.8725820 seconds for 100000 lists

正常列表理解更快:

>>> for n in (10, 100, 1000, 10000, 100000):
        lists = [['Test'] for _ in range(n)]
        seconds = timeit(lambda: [e for s in lists for e in s], number=1)
        print('%10.7f' % seconds, 'seconds for', n, 'lists')

 0.0000115 seconds for 10 lists
 0.0000327 seconds for 100 lists
 0.0002784 seconds for 1000 lists
 0.0024991 seconds for 10000 lists
 0.0228550 seconds for 100000 lists

答案 3 :(得分:0)

这样可以解决问题:

{{1}}

答案 4 :(得分:-1)

获取清单 -

>>> test = ["".join([j  for j in i for i in test])]
>>> test
['The world is a great place. Another sentence.The world is a great place. Another sentence.']

string

>>> test = "".join([j  for j in i for i in test])
>>> test
'The world is a great place. Another sentence.The world is a great place. Another sentence.'
相关问题