Python列表返回语句破译

发布于 2024-12-14 00:44:42 字数 381 浏览 1 评论 0原文

我在 python 例程中有以下语句:

return [hulls[h][i] for h, i in hull]

我无法弄清楚它实际返回的内容。

我的意思是,hulls 是一个 hull 列表,所以 'hulls[n]' 的类型是 'hull'。此外, hull 的类型为 'Point' hull 是一个点列表,但是

for h, i in hull? 

文档没有提到为什么以及如何执行这样的调用,而且它闻起来像某种列表理解调用,但我仍然无法正确阅读该语法。

所以我希望帮助您理解如何用伪代码或 C# 翻译该句子,

非常感谢。

I have the following statement in a python routine:

return [hulls[h][i] for h, i in hull]

And I can't figure out what it does actually return.

I mean, hulls is a list of hull, so 'hulls[n]' is of type 'hull'. Additionally, hull is of type 'Point' hull is a list of points, but

for h, i in hull? 

The docs don't mention why and how you can perform such a call, and it smells like some sort of list comprehension call, but I still can't read that syntax properly.

So I'd like help in understanding how you can translate the sentence in pseudocode, or c#

Thanks a lot.

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

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

发布评论

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

评论(3

百善笑为先 2024-12-21 00:44:42

是的,这就是列表理解。您的 return 语句可以重写为不太紧凑:

result = []
for h, i in hull:
    result.append(hulls[h][i])
return result

Yes, it is list comprehension. Your return statement could be rewritten less compactly as:

result = []
for h, i in hull:
    result.append(hulls[h][i])
return result
朱染 2024-12-21 00:44:42

船体看起来是一个二维的物体阵列。 Hull 是整数对 (x,y) 的列表。对于 Hull 中的每个坐标,它返回 hulls 中该位置的项目。

Hulls looks to be a two dimensional array of things. Hull is a list of pairs of ints (x,y). For each coordinate in Hull, it returns the item in hulls in that place.

纵情客 2024-12-21 00:44:42

是的,大卫是对的。如果你仍然对线路感到困惑
对于 h,i 在船体中:
我认为这意味着 hull 是一个包含多个元素的元组列表。因此,您使用 hull 中的每个元素作为 hull 的索引。

Yes, David is correct. If you're still confused about the line
for h, i in hull:
I think it means that hull is a list of tuples that have multiple elements. So you're using every element in hull to use as indices for hulls.

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