<tfoot id='MDWwa'></tfoot>

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

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

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

      使用 'in' 匹配数组中 Python 对象的属性

      时间:2023-10-19
        <bdo id='HY6KR'></bdo><ul id='HY6KR'></ul>
      • <i id='HY6KR'><tr id='HY6KR'><dt id='HY6KR'><q id='HY6KR'><span id='HY6KR'><b id='HY6KR'><form id='HY6KR'><ins id='HY6KR'></ins><ul id='HY6KR'></ul><sub id='HY6KR'></sub></form><legend id='HY6KR'></legend><bdo id='HY6KR'><pre id='HY6KR'><center id='HY6KR'></center></pre></bdo></b><th id='HY6KR'></th></span></q></dt></tr></i><div id='HY6KR'><tfoot id='HY6KR'></tfoot><dl id='HY6KR'><fieldset id='HY6KR'></fieldset></dl></div>
        <tfoot id='HY6KR'></tfoot><legend id='HY6KR'><style id='HY6KR'><dir id='HY6KR'><q id='HY6KR'></q></dir></style></legend>
            <tbody id='HY6KR'></tbody>

              • <small id='HY6KR'></small><noframes id='HY6KR'>

                本文介绍了使用 'in' 匹配数组中 Python 对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我不记得我是不是在做梦,但我似乎记得有一个函数允许类似,

                I don't remember whether I was dreaming or not but I seem to recall there being a function which allowed something like,

                foo in iter_attr(array of python objects, attribute name)

                我查看了文档,但这类内容不属于任何明显列出的标题

                I've looked over the docs but this kind of thing doesn't fall under any obvious listed headers

                推荐答案

                使用列表推导会构建一个临时列表,如果正在搜索的序列很大,它可能会占用你所有的内存.即使序列不大,构建列表也意味着在 in 开始搜索之前遍历整个序列.

                Using a list comprehension would build a temporary list, which could eat all your memory if the sequence being searched is large. Even if the sequence is not large, building the list means iterating over the whole of the sequence before in could start its search.

                使用生成器表达式可以避免临时列表:

                The temporary list can be avoiding by using a generator expression:

                foo = 12
                foo in (obj.id for obj in bar)
                

                现在,只要 obj.id == 12 靠近 bar 的开头,搜索就会很快,即使 bar无限长.

                Now, as long as obj.id == 12 near the start of bar, the search will be fast, even if bar is infinitely long.

                正如@Matt 建议的那样,如果 bar 中的任何对象可能缺少 id 属性,则使用 hasattr 是个好主意:

                As @Matt suggested, it's a good idea to use hasattr if any of the objects in bar can be missing an id attribute:

                foo = 12
                foo in (obj.id for obj in bar if hasattr(obj, 'id'))
                

                这篇关于使用 'in' 匹配数组中 Python 对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在使用 python 迭代列表时修改列表 下一篇:在 Python 3 中不使用“break"就停止迭代

                相关文章

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

                1. <tfoot id='Ma9w8'></tfoot>

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

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