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

Python 如何将列表中的元素合并在一起?,python,list,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.']]
我如何合并元素来创建它

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

谢谢,您可以使用来链接每个子列表中的元素,在链对象上调用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
或者仅使用“加入”:

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.']
如果每个子列表中都有很多子字符串,则链将是最有效的解决方案。

要获取列表-

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

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

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

' '.join(sum(test, []))
演示:

警告:虽然这对于一些列表来说很简单,但列表越多,速度越慢:

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

这将实现以下目的:

test = [['Hello my name is Py. How are you today?'],['The world is a great place. Another sentence.']]
import itertools
print list(itertools.chain(*test))

为什么每个字符串都有自己的子列表?@marmeladze,是的,对于多个子字符串来说,这将是最有效的。你的理解是反向的。我认为最好更精确一些,说“汇总列表”而不是“加入列表”,因为
join()
只处理字符串和
sum()
拒绝连接字符串。嗯。。。我认为“加入”是一个更自然的词。我不是在谈论Python函数名,而是在使用英语单词。我现在做了一个谷歌测试,“加入两个列表”的结果大约是“两个列表相加”结果的十倍。如果您觉得更好的话,我会切换到“concatenate”,但我真的不想说“sum”,因为这是我使用的Python函数的名称。“Join”确实是一个自然词,但当您使用它来指代
sum()
,它会掩盖
Join()
:P由于我过去曾尝试使用
sum()
join()
作为可互换的工具,因此我发现在心理上区分它们很有帮助。好吧,我不想被称为伪装者,所以我改为“串联”。并添加了一个关于速度的警告/测试。它不会,它将创建一个包含多个子字符串而不是单个字符串的列表
>>> 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
test = [['Hello my name is Py. How are you today?'],['The world is a great place. Another sentence.']]
import itertools
print list(itertools.chain(*test))