简体   繁体   English

如何获取列表中最大值的索引,然后使用最大值的索引从另一个列表中打印值?

[英]How can I get the index of max values in list and then print the values from another list with max's index?

d=[1,2,3,4,5,6,7]
g=[1,2,3,100,4,5,100]
m=max(g)
ll=[i for i, j in enumerate(g) if j == m]

print("The longest event time is",m,"for the event(s):",d[*ll])

I need to print after events the index of maximum values in d list Such that, (The longest event time is 100 for the event(s):4 7我需要在事件之后打印 d 列表中的最大值索引这样,(事件的最长事件时间为 100:4 7

you can simply create a function to find the max and index of max in a list and return the corresponding value of the same index in a second list.您可以简单地创建一个 function 来查找列表中的最大值和索引,并在第二个列表中返回相同索引的相应值。 here's the code:这是代码:

    # creating a function to get the index of Max in list A and return the corresponding value of the same index in list B
def get_id_max(first_list, second_list):
    # Finding the max, and then it's index in the first list
    global max_value
    global max_index
    max_value = max(first_list)
    max_index = first_list.index(max_value)

    # Return the corresponding value in the second list
    return second_list[max_index]

# Defining A, B, and C
A = [2011,2012,2013,2014,2015]
B = [50, 60, 15, 76, 55] 
C = [1.25, 2.2, 0.5, 1, 15]

print(f"The maximum value is {get_id_max(B,A)} and it's corresponding index is {max_index}")
# The maximum value is 2014 and it's corresponding index is 3
print(f"The maximum value is {get_id_max(C,A)} and it's corresponding index and value are {max_index,max_value}")
# The maximum value is 2015 and it's corresponding index and value are (4, 15)
events = [1, 2, 3, 4, 5, 6, 7]
durations = [1, 2, 3, 100, 4, 5, 100]
max_duration = max(durations)
longest_events = (str(event) for event, duration in zip(events, durations)
                  if duration == max_duration)
print(f"The longest event time is {max_duration}, for the event(s): {','.join(longest_events)}")

Your current approach requires several passes on the list.您当前的方法需要在列表中多次通过。 This is not the most optimal.这不是最优化的。

Here is an algorithm to compute the maxima in only one pass.这是一种仅在一次通过中计算最大值的算法。 It is similar to the classical manual computation of a maximum except it handles all maxima and the fact that the maximum is computed one one list, but the saved value is from another list (using zip )它类似于最大值的经典手动计算,除了它处理所有最大值以及最大值是在一个列表中计算的事实,但保存的值来自另一个列表(使用zip

m = float ('-inf')
out = [] # optional if -inf is not a possible value
for a,b in zip(d,g):
    if b>m:
        out = [a]
        m=b
    elif b == m:
        out.append(a)
print(out)

Output: [4, 7] Output: [4, 7]

Alternative without zip :没有zip替代方案:

m = float ('-inf')
out = [] # optional if -inf is not a possible value
for i,b in enumerate(g):
    if b>m:
        out = [d[i]]
        m=b
    elif b == m:
        out.append(d[i])

You can solve this with a little list comprehension:你可以通过一些列表理解来解决这个问题:

vals = [dat[0] for dat in zip(d, g) if dat[1] == max(g)]

print(f"The longest event times are {max(g)}, for the event(s): {vals}")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按从最大值到最小值的值顺序获取索引,而无需对输出索引列表进行排序,也可以修改另一个列表 - Get the index in order of values from max to min without sorting the output index list and amend another list as well 获取列表中具有相同最大值的最后一个索引 - Get the last index with same max values in a list 如何在不使用迭代的情况下从另一个 df 获取具有特定索引、列的值列表? - How can I get list of values with specific index, column from another df without using iteration? 如何将值列表转换为列表中的索引/切片? - How can I convert a list of values into an index/slice from a list? 通过嵌套列表中的元素索引查找嵌套列表的最小值和最大值 - find the min and max values of nested list by index of element in nested list 我如何从pandas数据框中的每一列获取最大(x)值,同时保持每个索引的索引? - How can I get the max (x) number of values from each column in a pandas dataframe while keeping the index for each? Python dataframe 返回所有最大值的列索引作为列表 - Python dataframe return column index of all max values as list 在具有列表值的字典中按索引计算最大元素的 Pythonic 方法 - Pythonic way of counting max elements by index in a dictionary with list values 我可以在Python中获取列表值的总和(不出现索引值) - Can I get sum of a list values (not occurrences of index values) in Python 如何获得数组列表中的最小/最大值? - How to get the min/max values in a list of arrays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM