Concatenate elements of a tuple in a list in python(在python的列表中连接元组的元素)
问题描述
我有一个包含字符串的元组列表例如:
I have a list of tuples that has strings in it For instance:
[('this', 'is', 'a', 'foo', 'bar', 'sentences')
('is', 'a', 'foo', 'bar', 'sentences', 'and')
('a', 'foo', 'bar', 'sentences', 'and', 'i')
('foo', 'bar', 'sentences', 'and', 'i', 'want')
('bar', 'sentences', 'and', 'i', 'want', 'to')
('sentences', 'and', 'i', 'want', 'to', 'ngramize')
('and', 'i', 'want', 'to', 'ngramize', 'it')]
现在我希望将每个字符串连接到一个元组中以创建一个空格分隔的字符串列表.我使用了以下方法:
Now I wish to concatenate each string in a tuple to create a list of space separated strings. I used the following method:
NewData=[]
for grams in sixgrams:
NewData.append( (''.join([w+' ' for w in grams])).strip())
它工作得很好.
但是,我拥有的列表有超过一百万个元组.所以我的问题是这种方法是否足够有效,或者是否有更好的方法来做到这一点.谢谢.
However, the list that I have has over a million tuples. So my question is that is this method efficient enough or is there some better way to do it. Thanks.
推荐答案
对于大量数据,您应该考虑是否需要将它们全部保存在一个列表中.如果您一次处理每一个,则可以创建一个生成器,该生成器将生成每个连接的字符串,但不会让它们全部占用内存:
For a lot of data, you should consider whether you need to keep it all in a list. If you are processing each one at a time, you can create a generator that will yield each joined string, but won't keep them all around taking up memory:
new_data = (' '.join(w) for w in sixgrams)
如果您也可以从生成器中获取原始元组,那么您也可以避免将 sixgrams 列表放在内存中.
if you can get the original tuples also from a generator, then you can avoid having the sixgrams list in memory as well.
这篇关于在python的列表中连接元组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在python的列表中连接元组的元素
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
