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

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

      1. 如何比较python中的多个元组列表?

        How to Compare Multiple lists of tuples in python?(如何比较python中的多个元组列表?)

          • <bdo id='DJBMb'></bdo><ul id='DJBMb'></ul>
          • <tfoot id='DJBMb'></tfoot>

              1. <small id='DJBMb'></small><noframes id='DJBMb'>

                    <tbody id='DJBMb'></tbody>

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

                  问题描述

                  如何比较多个这样的元组列表:

                  How can I compare Multiple lists of tuples like this:

                  [[(1,2), (3,6), (5,3)], [(1,5), (3,5)], [(2,1), (1,8), (3,9)]]
                  

                  输出应该是:

                  [(1,2), (1,5), (1,8)],[(3,6), (3,5), (3,9)]
                  

                  这意味着我只想要那些 x 轴 值与其他值匹配的值.
                  (5,3) 和 (2,1) 应该被丢弃!

                  It means that i want just those values whose x-axis value matches others.
                  (5,3) and (2,1) should be discarded!

                  推荐答案

                  一种可能的选择

                  >>> def group(seq):
                      for k, v in groupby(sorted(chain(*seq), key = itemgetter(0)), itemgetter(0)):
                          v = list(v)
                          if len(v) > 1:
                              yield v
                  
                  
                  >>> list(group(some_list))
                  [[(1, 2), (1, 5), (1, 8)], [(3, 6), (3, 5), (3, 9)]]
                  

                  另一个受欢迎的选择

                  >>> from collections import defaultdict
                  >>> def group(seq):
                      some_dict = defaultdict(list)
                      for e in chain(*seq):
                          some_dict[e[0]].append(e)
                      return (v for v in some_dict.values() if len(v) > 1)
                  
                  >>> list(group(some_list))
                  [[(1, 2), (1, 5), (1, 8)], [(3, 6), (3, 5), (3, 9)]]
                  

                  那么它们中的哪一个更适合示例数据?

                  So which of them fairs better with the example data?

                  >>> def group_sort(seq):
                      for k, v in groupby(sorted(chain(*seq), key = itemgetter(0)), itemgetter(0)):
                          v = list(v)
                          if len(v) > 1:
                              yield v
                  
                  
                  >>> def group_hash(seq):
                      some_dict = defaultdict(list)
                      for e in chain(*seq):
                          some_dict[e[0]].append(e)
                      return (v for v in some_dict.values() if len(v) > 1)
                  
                  >>> t1_sort = Timer(stmt="list(group_sort(some_list))", setup = "from __main__ import some_list, group_sort, chain, groupby")
                  >>> t1_hash = Timer(stmt="list(group_hash(some_list))", setup = "from __main__ import some_list, group_hash,chain, defaultdict")
                  >>> t1_hash.timeit(100000)
                  3.340240917954361
                  >>> t1_sort.timeit(100000)
                  0.14324535970808938
                  

                  还有一个更大的随机列表

                  And with a much larger random list

                  >>> some_list = [[sample(range(1000), 2) for _ in range(100)] for _ in range(100)]
                  >>> t1_sort.timeit(100)
                  1.3816694363194983
                  >>> t1_hash.timeit(1000)
                  34.015403087978484
                  >>> 
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    <tfoot id='dnrfU'></tfoot>
                      <tbody id='dnrfU'></tbody>

                        <legend id='dnrfU'><style id='dnrfU'><dir id='dnrfU'><q id='dnrfU'></q></dir></style></legend>
                        • <bdo id='dnrfU'></bdo><ul id='dnrfU'></ul>

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

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