The distance from the smallest to the largest item in the lists (inside the list)

I have a list the_list = [[3, 2, 0, 1, 4, 5], [4, 2, 1, 3, 0, 5], [0, 1, 2, 3, 4, 5], [1, 5, 2, 4, 3, 0]]. How to find out the distance from the smallest element to the largest element in the list. For example, for the first subscriptions in the the_listindex for the smallest element 0is 2, and the index for the largest element 5is equal 5. Therefore, the distance between the two indices is equal. 3Thus, I get the following output:

3
1
5
0

Change: . For the last output, it is 0, because the list ends there and assumes that the list only looks for the distance to the right

+4
source share
3 answers

Try the following:

lst = [[3, 2, 0, 1, 4, 5], [4, 2, 1, 3, 0, 5], [0, 1, 2, 3, 4, 5], [1, 5, 2, 4, 3, 0]]
[max(s.index(max(s)) - s.index(min(s)), 0) for s in lst]
=> [3, 1, 5, 0]
+1
>>>list(map(lambda x: x.index(max(x)) - x.index(min(x)) if x.index(max(x)) - x.index(min(x)) > 0 else 0 ,l))
[3, 1, 5, 0]
+1

python, ( ):

Foreach list in the_list do begin 
    Min:=maxint;
    MinPos:=0;
    Max:=0;
    MaxPos:=0;
    For I := 0 to list.length do begin 
        If list[i] > Max then begin
            Max := list[i];
            MaxPos := i;
        End;
        If list[i] < Min then begin
            Min := list[i];
            MinPos := i;
        End;
    End;
    If MinPos  < MaxPos then
        Write MaxPos - MinPos;
    Elsewhere
        Write 0;
End;

(, , ).

0
source

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


All Articles