Python: I'm trying to find the maximum difference between two items in a list

I need to find the maximum difference in the list between any two items. In the list, the [1,2,3,4,5]maximum difference is 4 (between items 1 and 5) used for loops.

This program should infer the location of these two elements (0 and 4) and their values ​​(1 and 5).

I can only figure out how to find the maximum difference between consecutive values, but this creates a problem if the maximum starts elsewhere, for example. [4,1,6,3,10,8]where the greatest difference is between 1 and 10 (positions 1 and 4). Can anybody help me?

+4
source share
7 answers

max min, , list index, .

numlist = [1, 2, 3, 4, 5]

max_val = max(numlist)
min_val = min(numlist)

max_pos = numlist.index(max_val)
min_pos = numlist.index(min_val)
+6

, , . , , :

lst = [1, 2, 3, 4, 5]

max_i, max_j = None, None # stores the indexes
max_d = -1 # stores the maximum distance we have seen so far

# iterate through all indexes of the list
for i in range(len(lst)):
    # iterate through all indexes, but starting from the index `i+1`
    for j in range(i + 1, len(lst)):
        d = abs(lst[i] - lst[j])
        if d > max_d:
            # memorize everything if the distance is larger than what we know
            max_i, max_j, max_d = i, j, abs(d)

print(max_i, max_j, max_d) # 0 4 4

, , , , . , , , .


, , , , min/max, , :

# set the current maximum and minimum to the first index
max_i, min_i = 0, 0

# iterate the list from the second index
for i in range(1, len(lst)):
    # check if we’re larger than the current maximum
    if lst[i] > lst[max_i]:
        max_i = i

    # check if we’re smaller than the current minimum
    if lst[i] < lst[min_i]:
        min_i = i

distance = lst[max_i] - lst[min_i]
print(min_i, max_i, distance) # 0 0 4

, . max min .

+4

, . , index(), :

L = [1, 2, 3, 4, 5]

temp = sorted(L) # sorted list

min = temp[0]
max = temp[-1] # index -1 will give the last element

:

print "min", min, L.index(min)
print "max", max, L.index(max)
print "difference", max - min

:

min 1 0
max 5 4
difference 4
+2

. Python. - itemgetter. min/max , , min/max . :

>>> import operator
>>> values = [1, 2, 3, 4, 5]
>>>
>>> min_index, min_value = min(enumerate(values), key=operator.itemgetter(1))
>>> min_index, min_value
0, 1
>>> max_index, max_value = max(enumerate(values), key=operator.itemgetter(1))
4, 5
>>> difference = max_value - min_value
>>> difference
4
+1

max min + enumerate:

biggest_idx, biggest_value = max(enumerate(lst), key=lambda x: x[1])
smallest_idx, smallest_value = min(enumerate(lst), key=lambda x: x[1])

:.

>>> lst =  [1,2,3,4,5] 
>>> biggest_idx, biggest_value = max(enumerate(lst), key=lambda x: x[1])
>>> smallest_idx, smallest_value = min(enumerate(lst), key=lambda x: x[1])
>>> print biggest_idx, biggest_value
4 5
>>> print smallest_idx, smallest_value
0 1
+1
min_i = 0
max_i = 0
for i in xrange(len(alist)):
    if alist[i] < alist[min_i]:
        min_i = i
    if alist[i] > alist[max_i]:
        max_i = i
print "min=%d, max=%d" % (alist[min_i], alist[max_i])
print "min index=%d, max index=%d", (min_i, max_i)
print "difference=%d" % (alist[min_i] - alist[max_i])
0

, ...

numlist=[4,1,6,3,10,8]
print('min value index : ' , numlist.index(min(numlist)))
print('max value index : ' , numlist.index(max(numlist)))
print('Max Difference : ',max(numlist)-min(numlist))
0

Source: https://habr.com/ru/post/1533078/


All Articles