检查对象列表是否包含具有特定属性值的对象

发布于 2025-01-07 03:06:04 字数 954 浏览 0 评论 0原文

我想检查我的对象列表是否包含具有特定属性值的对象。

class Test:
    def __init__(self, name):
        self.name = name

# in main()
l = []
l.append(Test("t1"))
l.append(Test("t2"))
l.append(Test("t2"))

我想要一种方法来检查列表是否包含名称为 "t1" 的对象。怎么办呢?我发现https://stackoverflow.com/a/598415/292291

[x for x in myList if x.n == 30]               # list of all matches
any(x.n == 30 for x in myList)                 # if there is any matches
[i for i,x in enumerate(myList) if x.n == 30]  # indices of all matches

def first(iterable, default=None):
    for item in iterable:
        return item
    return default

first(x for x in myList if x.n == 30)          # the first match, if any

我不想经历整个过程每次都列出来;我只需要知道是否有 1 个匹配的实例。 first(...)any(...) 或其他东西会这样做吗?

I want to check if my list of objects contain an object with a certain attribute value.

class Test:
    def __init__(self, name):
        self.name = name

# in main()
l = []
l.append(Test("t1"))
l.append(Test("t2"))
l.append(Test("t2"))

I want a way of checking if list contains an object with name "t1" for example. How can it be done? I found https://stackoverflow.com/a/598415/292291,

[x for x in myList if x.n == 30]               # list of all matches
any(x.n == 30 for x in myList)                 # if there is any matches
[i for i,x in enumerate(myList) if x.n == 30]  # indices of all matches

def first(iterable, default=None):
    for item in iterable:
        return item
    return default

first(x for x in myList if x.n == 30)          # the first match, if any

I don't want to go through the whole list every time; I just need to know if there's 1 instance which matches. Will first(...) or any(...) or something else do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流心雨 2025-01-14 03:06:04

正如您可以从文档中轻松看到,any()<一旦找到匹配项,/code> 函数就会短路并返回 True

any(x.name == "t2" for x in l)

As you can easily see from the documentation, the any() function short-circuits an returns True as soon as a match has been found.

any(x.name == "t2" for x in l)
奢欲 2025-01-14 03:06:04

另一个内置函数 next() 可用于此工作。它在条件为 True 的第一个实例处停止,与 any() 非常相似。

next((True for x in l if x.name=='t2'), False)

此外,next() 可以在条件为 True 的情况下返回对象本身(因此其行为类似于 OP 中的 first() 函数)。

next((x for x in l if x.name == 't2'), None)

Another built-in function next() can be used for this job. It stops at the first instance where the condition is True much like any().

next((True for x in l if x.name=='t2'), False)

Also, next() can return the object itself where the condition is True (so behaves like first() function in the OP).

next((x for x in l if x.name == 't2'), None)
鸠魁 2025-01-14 03:06:04

扩展这里已经给出的非常出色的答案,我编写了一个 lambda:

InArray = lambda elem, arr: bool(any(elem == x for x in arr))   #   InArray: Is elem part of arr?

我们可以通过以下方式使用它:

def main():
    DataType = ctypes.c_byte
    AllowedTypes = (ctypes.c_byte , ctypes.c_ubyte, 
                    ctypes.c_int32, ctypes.c_uint32, 
                    ctypes.c_float, ctypes.c_double)

    if InArray(DataType, AllowedTypes): pass

    if InArray(DataType, (ctypes.c_double, ctypes.c_float)): pass

Extending the very excellent answer given here already, I wrote a lambda:

InArray = lambda elem, arr: bool(any(elem == x for x in arr))   #   InArray: Is elem part of arr?

which we can use in these ways:

def main():
    DataType = ctypes.c_byte
    AllowedTypes = (ctypes.c_byte , ctypes.c_ubyte, 
                    ctypes.c_int32, ctypes.c_uint32, 
                    ctypes.c_float, ctypes.c_double)

    if InArray(DataType, AllowedTypes): pass

    if InArray(DataType, (ctypes.c_double, ctypes.c_float)): pass
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文