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

    1. <tfoot id='3fIYX'></tfoot>

        <small id='3fIYX'></small><noframes id='3fIYX'>

          <bdo id='3fIYX'></bdo><ul id='3fIYX'></ul>

      1. 比较Python中连续元组列表的第一个元素

        时间:2023-09-27
      2. <tfoot id='Cnp0H'></tfoot>

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

                    <tbody id='Cnp0H'></tbody>
                • <legend id='Cnp0H'><style id='Cnp0H'><dir id='Cnp0H'><q id='Cnp0H'></q></dir></style></legend>
                  <i id='Cnp0H'><tr id='Cnp0H'><dt id='Cnp0H'><q id='Cnp0H'><span id='Cnp0H'><b id='Cnp0H'><form id='Cnp0H'><ins id='Cnp0H'></ins><ul id='Cnp0H'></ul><sub id='Cnp0H'></sub></form><legend id='Cnp0H'></legend><bdo id='Cnp0H'><pre id='Cnp0H'><center id='Cnp0H'></center></pre></bdo></b><th id='Cnp0H'></th></span></q></dt></tr></i><div id='Cnp0H'><tfoot id='Cnp0H'></tfoot><dl id='Cnp0H'><fieldset id='Cnp0H'></fieldset></dl></div>
                  本文介绍了比较Python中连续元组列表的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个元组列表,每个元组包含两个元素.少数子列表的第一个元素很常见.我想比较这些子列表的第一个元素并将第二个元素附加到一个列表中.这是我的清单:

                  I have a list of tuples, each containing two elements. The first element of few sublists is common. I want to compare the first element of these sublists and append the second element in one lists. Here is my list:

                  myList=[(1,2),(1,3),(1,4),(1,5),(2,6),(2,7),(2,8),(3,9),(3,10)]
                  

                  我想从中列出一个列表,看起来像这样:`

                  I would like to make a list of lists out of it which looks something like this:`

                  NewList=[(2,3,4,5),(6,7,8),(9,10)]
                  

                  我希望有什么有效的方法.

                  I hope if there is any efficient way.

                  推荐答案

                  您可以使用 OrderedDict 按每个元组的第一个子元素对元素进行分组:

                  You can use an OrderedDict to group the elements by the first subelement of each tuple:

                  myList=[(1,2),(1,3),(1,4),(1,5),(2,6),(2,7),(2,8),(3,9),(3,10)]
                  
                  from collections import OrderedDict
                  
                  od  = OrderedDict()
                  
                  for a,b in myList:
                      od.setdefault(a,[]).append(b)
                  
                  print(list(od.values()))
                  [[2, 3, 4, 5], [6, 7, 8], [9, 10]]
                  

                  如果你真的想要元组:

                  print(list(map(tuple,od.values())))
                  [(2, 3, 4, 5), (6, 7, 8), (9, 10)]
                  

                  如果您不关心元素出现的顺序,而只是想要最有效的分组方式,您可以使用 collections.defaultdict:

                  If you did not care about the order the elements appeared and just wanted the most efficient way to group you could use a collections.defaultdict:

                  from collections import defaultdict
                  
                  od  = defaultdict(list)
                  
                  for a,b in myList:
                      od[a].append(b)
                  
                  print(list(od.values()))
                  

                  最后,如果您的数据按照您的输入示例排序,您可以简单地使用 itertools.groupby 按每个元组的第一个子元素分组,并从分组的元组中提取第二个元素:

                  Lastly, if your data is in order as per your input example i.e sorted you could simply use itertools.groupby to group by the first subelement from each tuple and extract the second element from the grouped tuples:

                  from itertools import groupby
                  from operator import itemgetter
                  print([tuple(t[1] for t in v) for k,v in groupby(myList,key=itemgetter(0))])
                  

                  输出:

                  [(2, 3, 4, 5), (6, 7, 8), (9, 10)]
                  

                  同样,groupby 仅在您的数据至少按第一个元素排序时才有效.

                  Again the groupby will only work if your data is sorted by at least the first element.

                  合理大小列表中的一些时间安排:

                  Some timings on a reasonable sized list:

                  In [33]: myList = [(randint(1,10000),randint(1,10000)) for _ in range(100000)]
                  
                  In [34]: myList.sort()
                  
                  In [35]: timeit ([tuple(t[1] for t in v) for k,v in groupby(myList,key=itemgetter(0))])
                  10 loops, best of 3: 44.5 ms per loop
                  
                  In [36]: %%timeit                                                               od = defaultdict(list)
                  for a,b in myList:
                      od[a].append(b)
                     ....: 
                  10 loops, best of 3: 33.8 ms per loop
                  
                  In [37]: %%timeit
                  dictionary = OrderedDict()
                  for x, y in myList:
                       if x not in dictionary:
                          dictionary[x] = [] # new empty list
                      dictionary[x].append(y)
                     ....: 
                  10 loops, best of 3: 63.3 ms per loop
                  
                  In [38]: %%timeit   
                  od = OrderedDict()
                  for a,b in myList:
                      od.setdefault(a,[]).append(b)
                     ....: 
                  10 loops, best of 3: 80.3 ms per loop
                  

                  如果顺序很重要并且数据已排序,请使用groupby,如果需要将所有元素映射到,它会更接近defaultdict方法默认字典中的元组.

                  If order matters and the data is sorted, go with the groupby, it will get even closer to the defaultdict approach if it is necessary to map all the elements to tuple in the defaultdict.

                  如果数据没有排序或者您不关心任何顺序,您将找不到比使用 defaultdict 方法更快的分组方法.

                  If the data is not sorted or you don't care about any order, you won't find a faster way to group than using the defaultdict approach.

                  这篇关于比较Python中连续元组列表的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:append 和 insert 都存在有什么原因吗? 下一篇:将一个 numpy 数组附加到一个列表 - 奇怪的事情

                  相关文章

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

                  <small id='5d7UP'></small><noframes id='5d7UP'>

                • <legend id='5d7UP'><style id='5d7UP'><dir id='5d7UP'><q id='5d7UP'></q></dir></style></legend>

                  <tfoot id='5d7UP'></tfoot>
                      <bdo id='5d7UP'></bdo><ul id='5d7UP'></ul>