在Python中返回多维的最小/最大?

发布于 2024-12-22 02:00:05 字数 149 浏览 3 评论 0原文

我有一个列表,其形式为

[ [[a,b,c],[d,e,f]] , [[a,b,c],[d,e,f]] , [[a,b,c],[d,e,f]] ... ] etc.

我想返回最小 c 值和最大 c+f 值。这可能吗?

I have a list in the form of

[ [[a,b,c],[d,e,f]] , [[a,b,c],[d,e,f]] , [[a,b,c],[d,e,f]] ... ] etc.

I want to return the minimal c value and the maximal c+f value. Is this possible?

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

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

发布评论

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

评论(5

亣腦蒛氧 2024-12-29 02:00:05

对于最小 c

min(c for (a,b,c),(d,e,f) in your_list)

对于最大 c+f

max(c+f for (a,b,c),(d,e,f) in your_list)

示例:

>>> your_list = [[[1,2,3],[4,5,6]], [[0,1,2],[3,4,5]], [[2,3,4],[5,6,7]]]
>>> min(c for (a,b,c),(d,e,f) in lst)
2
>>> max(c+f for (a,b,c),(d,e,f) in lst)
11

For the minimum c:

min(c for (a,b,c),(d,e,f) in your_list)

For the maximum c+f

max(c+f for (a,b,c),(d,e,f) in your_list)

Example:

>>> your_list = [[[1,2,3],[4,5,6]], [[0,1,2],[3,4,5]], [[2,3,4],[5,6,7]]]
>>> min(c for (a,b,c),(d,e,f) in lst)
2
>>> max(c+f for (a,b,c),(d,e,f) in lst)
11
把昨日还给我 2024-12-29 02:00:05

列表理解来救援

a=[[[1,2,3],[4,5,6]], [[2,3,4],[4,5,6]]]
>>> min([x[0][2] for x in a])
3

>>> max([x[0][2]+ x[1][2] for x in a])
10

List comprehension to the rescue

a=[[[1,2,3],[4,5,6]], [[2,3,4],[4,5,6]]]
>>> min([x[0][2] for x in a])
3

>>> max([x[0][2]+ x[1][2] for x in a])
10
孤云独去闲 2024-12-29 02:00:05

您必须将您的列表映射到仅包含您关心的项目的列表。

这是一种可能的方法:

x = [[[5, 5, 3], [6, 9, 7]], [[6, 2, 4], [0, 7, 5]], [[2, 5, 6], [6, 6, 9]], [[7, 3, 5], [6, 3, 2]], [[3, 10, 1], [6, 8, 2]], [[1, 2, 2], [0, 9, 7]], [[9, 5, 2], [7, 9, 9]], [[4, 0, 0], [1, 10, 6]], [[1, 5, 6], [1, 7, 3]], [[6, 1, 4], [1, 2, 0]]]

minc = min(l[0][2] for l in x)
maxcf = max(l[0][2]+l[1][2] for l in x)

minmax 调用的内容就是所谓的 "generator",负责生成原始数据到过滤后数据的映射。

You have to map your list to one containing just the items you care about.

Here is one possible way of doing this:

x = [[[5, 5, 3], [6, 9, 7]], [[6, 2, 4], [0, 7, 5]], [[2, 5, 6], [6, 6, 9]], [[7, 3, 5], [6, 3, 2]], [[3, 10, 1], [6, 8, 2]], [[1, 2, 2], [0, 9, 7]], [[9, 5, 2], [7, 9, 9]], [[4, 0, 0], [1, 10, 6]], [[1, 5, 6], [1, 7, 3]], [[6, 1, 4], [1, 2, 0]]]

minc = min(l[0][2] for l in x)
maxcf = max(l[0][2]+l[1][2] for l in x)

The contents of the min and max calls is what is called a "generator", and is responsible for generating a mapping of the original data to the filtered data.

命比纸薄 2024-12-29 02:00:05

当然有可能。您有一个列表,其中包含一个二元素列表的列表,这些列表本身就是列表。你的基本算法是

for each of the pairs
    if c is less than minimum c so far
       make minimum c so far be c
    if (c+f) is greater than max c+f so far
       make max c+f so far be (c+f)

Of course it's possible. You've got a list containing a list of two-element lists that turn out to be lists themselves. Your basic algorithm is

for each of the pairs
    if c is less than minimum c so far
       make minimum c so far be c
    if (c+f) is greater than max c+f so far
       make max c+f so far be (c+f)
只等公子 2024-12-29 02:00:05

假设您的列表存储在 my_list 中:

min_c = min(e[0][2] for e in my_list)
max_c_plus_f = max(map(lambda e : e[0][2] + e[1][2], my_list))

suppose your list is stored in my_list:

min_c = min(e[0][2] for e in my_list)
max_c_plus_f = max(map(lambda e : e[0][2] + e[1][2], my_list))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文