[英]Get max value and index of max value from last item in nested lists
I have a list of nested lists:我有一个嵌套列表列表:
scoring_matrix = [[1, 2, 7], [3, 5, 2], [2, 2, 7]]
I am trying to get the max value from all last items in the nested lists, together with the index/indices of the nested list(s) with that max value.我正在尝试从嵌套列表中的所有最后一项中获取最大值,以及具有该最大值的嵌套列表的索引/索引。 All nested lists are of equal length.
所有嵌套列表的长度相同。
In this example:在这个例子中:
item output (value, index)
2 [7, [0, 2]]
I also needed to get the highest value and index/indices for the last nested list, which I worked out:我还需要获得最后一个嵌套列表的最高值和索引/索引,我计算出了:
max_last_list = [index for index, item in enumerate(scoring_matrix[-1]) if item == max(scoring_matrix[-1])]
I am attempting to rewrite this line to make it work on my problem, but I can't find out how to do it.我正在尝试重写这一行以使其解决我的问题,但我不知道该怎么做。
The solution cannot make use of non-built in modules该解决方案不能使用非内置模块
Based on my first answer, and other answers below:根据我的第一个答案以及以下其他答案:
Compute the end's max and find it in the sublists计算结束的最大值并在子列表中找到它
# Find the max of the ends value max_end = max(map(lambda x: x[-1], scoring_matrix)) # Find the sublist whe the last if the overall max indexes = [index for index, item in enumerate(scoring_matrix) if item[-1] == max_end] # Group result result = [max_end, indexes] print(result)
Keep the ends together and work on them将两端保持在一起并继续努力
# Put all ends value in a list ends = list(zip(*scoring_matrix))[2] # Iterate over the ends to find the overall max indexes = [i for i, v in enumerate(ends) if v == max(ends)] # Group result result = [max(ends), indexes] print(result)
Try using the below code:尝试使用以下代码:
l = list(zip(*scoring_matrix))[2]
print([max(l), [i for i, v in enumerate(l) if v == max(l)]])
Output: Output:
[7, [0, 2]]
Adding to @U10-Forward's answer,添加到@U10-Forward 的答案,
l = list(zip(*scoring_matrix))[2]
print([max(l), [i for i, v in enumerate(l) if v == max(l)]])
You can also try this:你也可以试试这个:
maxi = max(sum(scoring_matrix,[]))
res = [maxi,[n for n,i in enumerate(scoring_matrix) if maxi in i]]
Output: Output:
[7, [0, 2]]
Keep it simple, get the max, then create the list of indices:保持简单,获取最大值,然后创建索引列表:
scoring_matrix = [[1, 2, 7], [3, 5, 2], [2, 2, 7]]
m = max(sublist[2] for sublist in scoring_matrix)
out = [m, [i for i, sub in enumerate(scoring_matrix) if sub[2] == m]]
print(out)
#[7, [0, 2]]
Try to make your solution readable.尝试使您的解决方案可读。 Instead of a clever one-liner, more code with clear variable names is often easier to comprehend.
与聪明的单行代码相比,更多具有清晰变量名的代码通常更容易理解。
I would first calculate the maximum and in a second step find the indices for that maximum.我将首先计算最大值,然后在第二步中找到该最大值的索引。
scoring_max = max(l[-1] for l in scoring_matrix)
indices = [i for i, sublist in enumerate(scoring_matrix) if sublist[-1] == scoring_max]
return [scoring_max, indices]
The solution to your second problem is very similar.您的第二个问题的解决方案非常相似。
last_sublist = scoring_matrix[-1]
list_max = max(last_sublist)
indices = [i for i, val in enumerate(last_sublist) if val == list_max]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.