如何在列表列表中找到具有最大值的列表(其中嵌套列表包含字符串和数字)?
How to find the lists with max values in a list of lists (where nested lists contain strings and numbers)?
我有一个列表列表
list_of_lists = [['a',1,19,5]['b',2,4,6],['c',22,5,9],['d',12,19,20]]
我想获得最高值的前 x 个列表,所以
前 3 max(list_of_lists)
会 return
[['c',22, 5,9],['d',12,19,20],['a',1,19,5]]
或者如果我正在循环 list_of_lists
我可以根据所选列表的索引将每个具有前 x 个最大值的列表附加到另一个列表列表。
这是我正在使用的代码,但它有缺陷,因为我认为我需要在每个循环结束时删除选定的答案,这样它就不会出现在下一个循环中,它只会查看第 4 列( x[3])
for y in case_list:
last_indices = [x[3] for x in case_list]
print("max of cases is: ",max(last_indices))
目前的输出是:
max of cases is: 22
max of cases is: 22
max of cases is: 22
这个 answer 给出了最高的最大列表,但我希望能够灵活地 return 最高 x 而不是只有一个。
此 answer 给出单个列表中的前 x 个最大值。
如果您的嵌套列表在第一个索引处始终只有一个字符串(如您的示例所示),则您使用 max()
在每个嵌套列表的一部分上按最大值对列表列表进行排序,不包括第一项。然后,根据您想要的 "top" 个结果的数量对最终输出进行切片。以下是获取 "top" 3 个具有最大值的列表的示例。
list_of_lists = [['a',1,19,5],['b',2,4,6],['c',22,5,9],['d',12,19,20]]
# sort nested lists descending based on max value contained
sorted_list = sorted(list_of_lists, key=lambda x: max(x[1:]), reverse=True)
# slice first 3 lists (to get the "top" 3 max values)
sliced_list = sorted_list[:3]
print(sliced_list)
# OUTPUT
# [['c', 22, 5, 9], ['d', 12, 19, 20], ['a', 1, 19, 5]]
你可以把它变成一个简单的函数来获取前 "x" 个嵌套列表(函数后面的循环纯粹是打印类似于你的例子的东西)。
def max_lists(data, num):
results = sorted(data, key=lambda x: max(x[1:]), reverse=True)
return results[:num]
list_of_lists = [['a',1,19,5],['b',2,4,6],['c',22,5,9],['d',12,19,20]]
top_three = max_lists(list_of_lists, 3)
print(top_three)
for x in top_three:
print(f'max value: {max(x[1:])} list: {x}')
# OUTPUT
# [['c', 22, 5, 9], ['d', 12, 19, 20], ['a', 1, 19, 5]]
# max value: 22 list: ['c', 22, 5, 9]
# max value: 20 list: ['d', 12, 19, 20]
# max value: 19 list: ['a', 1, 19, 5]
我有一个列表列表
list_of_lists = [['a',1,19,5]['b',2,4,6],['c',22,5,9],['d',12,19,20]]
我想获得最高值的前 x 个列表,所以
前 3 max(list_of_lists)
会 return
[['c',22, 5,9],['d',12,19,20],['a',1,19,5]]
或者如果我正在循环 list_of_lists
我可以根据所选列表的索引将每个具有前 x 个最大值的列表附加到另一个列表列表。
这是我正在使用的代码,但它有缺陷,因为我认为我需要在每个循环结束时删除选定的答案,这样它就不会出现在下一个循环中,它只会查看第 4 列( x[3])
for y in case_list:
last_indices = [x[3] for x in case_list]
print("max of cases is: ",max(last_indices))
目前的输出是:
max of cases is: 22
max of cases is: 22
max of cases is: 22
这个 answer 给出了最高的最大列表,但我希望能够灵活地 return 最高 x 而不是只有一个。
此 answer 给出单个列表中的前 x 个最大值。
如果您的嵌套列表在第一个索引处始终只有一个字符串(如您的示例所示),则您使用 max()
在每个嵌套列表的一部分上按最大值对列表列表进行排序,不包括第一项。然后,根据您想要的 "top" 个结果的数量对最终输出进行切片。以下是获取 "top" 3 个具有最大值的列表的示例。
list_of_lists = [['a',1,19,5],['b',2,4,6],['c',22,5,9],['d',12,19,20]]
# sort nested lists descending based on max value contained
sorted_list = sorted(list_of_lists, key=lambda x: max(x[1:]), reverse=True)
# slice first 3 lists (to get the "top" 3 max values)
sliced_list = sorted_list[:3]
print(sliced_list)
# OUTPUT
# [['c', 22, 5, 9], ['d', 12, 19, 20], ['a', 1, 19, 5]]
你可以把它变成一个简单的函数来获取前 "x" 个嵌套列表(函数后面的循环纯粹是打印类似于你的例子的东西)。
def max_lists(data, num):
results = sorted(data, key=lambda x: max(x[1:]), reverse=True)
return results[:num]
list_of_lists = [['a',1,19,5],['b',2,4,6],['c',22,5,9],['d',12,19,20]]
top_three = max_lists(list_of_lists, 3)
print(top_three)
for x in top_three:
print(f'max value: {max(x[1:])} list: {x}')
# OUTPUT
# [['c', 22, 5, 9], ['d', 12, 19, 20], ['a', 1, 19, 5]]
# max value: 22 list: ['c', 22, 5, 9]
# max value: 20 list: ['d', 12, 19, 20]
# max value: 19 list: ['a', 1, 19, 5]