列表列表的最小值 [英] minimum of list of lists

查看:52
本文介绍了列表列表的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的列表列表:

I have a list of lists like so:

[[10564, 15], [10564, 13], [10589, 18], [10637, 39], [10662, 38], [10712, 50], [10737, 15], [10762, 14], [10787, 9], [10812, 12], [10837, 45], [3, 17], [7, 21], [46, 26], [48, 12], [49, 24], [64, 14], [66,
 17], [976, 27], [981, 22], [982, 22], [983, 17], [985, 13], [517, 9], [521, 15], [525, 11], [526, 13], [528, 14], [698, 14], [788, 24], [792, 19]]

我正在尝试查找每个列表中第二个元素的最小值(因此比较15到13到18等,而不是比较10564和15),还要将其分成多个范围,所以我可以说,第二个元素的最低值每个列表中的[1],仅当element [0]大于10000等时,我该怎么做?我试了一下,只能比较同一列表中的元素,这不是我想要的.在我提到的情况下,我将返回[10787,9],但是如果另一个值大于10000且为9,我也想返回该值.

I am trying to find the lowest value for the second element in each list(so compare 15 to 13 to 18 etc not comparing 10564 and 15 ), but also to separate it into ranges, so I could say, lowest second element[1] in each list, only if element[0] is over 10000 etc. How might I do this? I tried it and can only compare elements in the same list as of yet, which is not what I want. In the case I mentions I would then be returning [10787, 9] but if there was another value over 10000 with 9 I would want to return that also.

推荐答案

这取决于您要输出的内容.首先,您需要根据范围" 1

This depends on what you want for output. First, you'll need to filter your list based on the "ranges" 1

gen = (x for x in lists if x[0] > 10000)

if 条件可以任意复杂(在有效语法中).例如:

The if condition can be as complicated as you want (within valid syntax). e.g.:

gen = (x for x in lists if 5000 < x[0] < 10000)

很好.

现在,如果只需要子列表中的第二个元素:

Now, If you want only the second element from the sublists:

min(x[1] for x in gen)

当然,您可以内联整个内容:

Of course, you could inline the whole thing:

min(x[1] for x in lists if x[0] > 10000)

如果您想要整个子列表:

If you want the entire sublist:

from operator import itemgetter
min(gen,key=itemgetter(1))

示例:

>>> lists = [[10564, 15], [10564, 13], [10589, 18], [10637, 39], [10662, 38], [10712, 50], [10737, 15], [10762, 14], [10787, 9], [10812, 12], [10837, 45], [3, 17], [7, 21], [46, 26], [48, 12], [49, 24], [64, 14], [66,17], [976, 27], [981, 22], [982, 22], [983, 17], [985, 13], [517, 9], [521, 15], [525, 11], [526, 13], [528, 14], [698, 14], [788, 24], [792, 19]]
>>> gen = (x for x in lists if x[0] > 10000)
>>> min(x[1] for x in gen)
9
>>> gen = (x for x in lists if x[0] > 10000)
>>> from operator import itemgetter
>>> min(gen,key=itemgetter(1))
[10787, 9]

不幸的是,这些仅给您提供了符合条件的 first 子列表.要获得所有这些信息:

Unfortunately, these only give you the first sublist which matches the criteria. To get all of them:

target = min(x[1] for x in lists if x[0] > 10000)
matches = [x for x in lists if (x[1] == target) and (x[0] > 10000)]

如果您确定知道少于 N 个匹配项,则可以使用 heapq itertools.takewhile .在一般情况下,您不知道匹配数的上限,我认为这种解决方案更好(与O(NlogN)排序相比,它是O(N)).

If you know for sure that there will be less than N matches, you could do this a little more efficiently with heapq and itertools.takewhile. In the general case where you don't know an upper limit on the number of matches, I think this solution is better (It's O(N) compared to sorting which is O(NlogN)).

1 请注意,生成器表达式"在用尽之前只能重复一次

1Note that the "generator expression" can only be iterated over once before it is exhausted

这篇关于列表列表的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆