<tfoot id='dIMSa'></tfoot>

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

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

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

        在python的List中通过其成员查找对象

        Find object by its member inside a List in python(在python的List中通过其成员查找对象)
      2. <small id='0uqeT'></small><noframes id='0uqeT'>

          • <bdo id='0uqeT'></bdo><ul id='0uqeT'></ul>
            • <tfoot id='0uqeT'></tfoot>

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

                    <tbody id='0uqeT'></tbody>
                  <legend id='0uqeT'><style id='0uqeT'><dir id='0uqeT'><q id='0uqeT'></q></dir></style></legend>
                • 本文介绍了在python的List中通过其成员查找对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  让我们假设以下简单对象:

                  lets assume the following simple Object:

                  class Mock:
                      def __init__(self, name, age):
                          self.name = name
                          self.age = age
                  

                  然后我有一个列表,其中包含一些像这样的对象:

                  then I have a list with some Objects like this:

                  myList = [Mock("Dan", 34), Mock("Jack", 30), Mock("Oli", 23)...]
                  

                  是否有一些内置功能可以让我获得所有年龄为 30 岁的 Mocks?当然我可以遍历他们并比较他们的年龄,但是像

                  Is there some built-in feature where I can get all Mocks with an age of ie 30? Of course I can iterate myself over them and compare their ages, but something like

                  find(myList, age=30)
                  

                  会很好.有这样的吗?

                  推荐答案

                  您可能希望对它们进行预索引 -

                  You might want to pre-index them -

                  from collections import defaultdict
                  
                  class Mock(object):
                      age_index = defaultdict(list)
                  
                      def __init__(self, name, age):
                          self.name = name
                          self.age = age
                          Mock.age_index[age].append(self)
                  
                      @classmethod
                      def find_by_age(cls, age):
                          return Mock.age_index[age]
                  

                  一张图片胜过千言万语:

                  a picture is worth a thousand words:

                  X 轴是 myList 中的 Mocks 数量,Y 轴是运行时间,以秒为单位.

                  X axis is number of Mocks in myList, Y axis is runtime in seconds.

                  • 红点是@dcrooney 的 filter() 方法
                  • 蓝点是@marshall.ward 的列表理解
                  • 隐藏在 X 轴后面的绿点是我的索引 ;-)

                  这篇关于在python的List中通过其成员查找对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)
                    <tbody id='O122c'></tbody>

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

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

                      <tfoot id='O122c'></tfoot>
                        • <bdo id='O122c'></bdo><ul id='O122c'></ul>
                        • <small id='O122c'></small><noframes id='O122c'>