Python 相当于 C# 的 .Select?

发布于 2024-10-03 21:29:07 字数 173 浏览 7 评论 0原文

我有一个 Python 对象列表,它们每个都有一个 id 属性。我想获得这些 ID 的列表。

在 C# 中,我会写

myObjects.Select(obj => obj.id);

How would I do this in Python?

I've got an list of objects in Python, and they each have an id property. I want to get a list of those IDs.

In C# I'd write

myObjects.Select(obj => obj.id);

How would I do this in Python?

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

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

发布评论

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

评论(5

终陌 2024-10-10 21:29:07

在这里查看“列表理解”部分:http://docs.python.org/tutorial/ datastructs.html

如果您的起始列表名为 original_list 并且新列表名为 id_list,您可以执行以下操作:

id_list = [x.id for x in original_list]

Check out the section on "List Comprehension" here: http://docs.python.org/tutorial/datastructures.html

If your starting list is called original_list and your new list is called id_list, you could do something like this:

id_list = [x.id for x in original_list]
兔姬 2024-10-10 21:29:07
[obj.id for obj in myObjects]
[obj.id for obj in myObjects]
那请放手 2024-10-10 21:29:07

如果列表很大并且您只需要处理一次 id,那么还有生成器表达式。

ids = (obj.id for obj in my_objects)

for id in ids:
    do_something(id)

生成器表达式不支持随机访问,但会根据需要获取每个 id,因此可以避免一次构建列表。生成器表达式是 xrange,就像列表推导式是 range 一样。

关于生成器表达式的另一个警告是,只有当其中的任何资源仍然打开时才可以访问它。例如,以下代码将失败。

with open(filename) as f:
    lines = (line for line in f)

# f is now closed
for line in lines:
    print line

在这种情况下,等效的列表理解将起作用。

If it's a big list and you only need to process the ids once then there are also generator expressions.

ids = (obj.id for obj in my_objects)

for id in ids:
    do_something(id)

A generator expression doesn't support random access but will get you each id on demand and so avoids building a list all at once. generator expressions are to xrange as list comprehensions are to range.

One more caveat with generator expressions is that it can only be accessed for as long as any resource within it is still open. For example, the following code will fail.

with open(filename) as f:
    lines = (line for line in f)

# f is now closed
for line in lines:
    print line

The equivalent list comprehension would work in this case.

心在旅行 2024-10-10 21:29:07

如果您想要在 Python 中直接等效于 C# select,但需要使用第三方库,则可以使用 asq< /a> 包,它通过 Python 可迭代提供受 LINQ-for-objects 启发的实现。使用 asq,您的问题中的 C# 代码将变为:

from asq.initiators import query
query(myObjects).select(lambda obj: obj.id)

或者,与 asq 的另一个功能相结合:

from asq.selectors import a_
query(myObjects).select(a_("id"))

If you want a direct equivalent of C# select in Python, at the cost of using a third-party library, you could use the asq package which provides a LINQ-for-objects inspired implementation over Python iterables. Using asq the C# code in your question would become:

from asq.initiators import query
query(myObjects).select(lambda obj: obj.id)

or, combined with another feature of asq:

from asq.selectors import a_
query(myObjects).select(a_("id"))
聽兲甴掵 2024-10-10 21:29:07

头脑正常的人不会按照以下方式执行此操作,但这里是为了以防万一它在更复杂的示例中派上用场

import operator
map(operator.attrgetter("id"), myObjects)

Nobody in their right mind would do this the following way, but here it is in case it comes in handy in a more complex example

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