<tfoot id='f3NR2'></tfoot>
    <legend id='f3NR2'><style id='f3NR2'><dir id='f3NR2'><q id='f3NR2'></q></dir></style></legend>
    <i id='f3NR2'><tr id='f3NR2'><dt id='f3NR2'><q id='f3NR2'><span id='f3NR2'><b id='f3NR2'><form id='f3NR2'><ins id='f3NR2'></ins><ul id='f3NR2'></ul><sub id='f3NR2'></sub></form><legend id='f3NR2'></legend><bdo id='f3NR2'><pre id='f3NR2'><center id='f3NR2'></center></pre></bdo></b><th id='f3NR2'></th></span></q></dt></tr></i><div id='f3NR2'><tfoot id='f3NR2'></tfoot><dl id='f3NR2'><fieldset id='f3NR2'></fieldset></dl></div>

        <bdo id='f3NR2'></bdo><ul id='f3NR2'></ul>
    1. <small id='f3NR2'></small><noframes id='f3NR2'>

    2. 在python的列表中连接元组的元素

      时间:2023-08-31

            <tbody id='1cBTd'></tbody>
            <bdo id='1cBTd'></bdo><ul id='1cBTd'></ul>

              <legend id='1cBTd'><style id='1cBTd'><dir id='1cBTd'><q id='1cBTd'></q></dir></style></legend>
            1. <tfoot id='1cBTd'></tfoot>
              <i id='1cBTd'><tr id='1cBTd'><dt id='1cBTd'><q id='1cBTd'><span id='1cBTd'><b id='1cBTd'><form id='1cBTd'><ins id='1cBTd'></ins><ul id='1cBTd'></ul><sub id='1cBTd'></sub></form><legend id='1cBTd'></legend><bdo id='1cBTd'><pre id='1cBTd'><center id='1cBTd'></center></pre></bdo></b><th id='1cBTd'></th></span></q></dt></tr></i><div id='1cBTd'><tfoot id='1cBTd'></tfoot><dl id='1cBTd'><fieldset id='1cBTd'></fieldset></dl></div>

              <small id='1cBTd'></small><noframes id='1cBTd'>

                本文介绍了在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) 下一篇:Python中的多个元组到两对元组?

                相关文章

                <legend id='QKUV7'><style id='QKUV7'><dir id='QKUV7'><q id='QKUV7'></q></dir></style></legend>

                    • <bdo id='QKUV7'></bdo><ul id='QKUV7'></ul>

                    <i id='QKUV7'><tr id='QKUV7'><dt id='QKUV7'><q id='QKUV7'><span id='QKUV7'><b id='QKUV7'><form id='QKUV7'><ins id='QKUV7'></ins><ul id='QKUV7'></ul><sub id='QKUV7'></sub></form><legend id='QKUV7'></legend><bdo id='QKUV7'><pre id='QKUV7'><center id='QKUV7'></center></pre></bdo></b><th id='QKUV7'></th></span></q></dt></tr></i><div id='QKUV7'><tfoot id='QKUV7'></tfoot><dl id='QKUV7'><fieldset id='QKUV7'></fieldset></dl></div>
                    <tfoot id='QKUV7'></tfoot>

                    <small id='QKUV7'></small><noframes id='QKUV7'>