繁体   English   中英

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

[英]How do i merge elements in a list together?

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

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

谢谢

您可以使用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

或者只是使用加入:

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

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

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

这样就可以了:

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

获取清单 -

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM