Home > OS >  Python: How to verify data in a list is downward or upward trend
Python: How to verify data in a list is downward or upward trend

Time:10-17

Without depending on external modules like numpy or pandas, is there any basic and simply python way to determine each column of follow example data, whether they are Upward, Constant or Downward

Input data:

-Column1--Column2--Column3--Column4-
98394112 78677690 20070400 134579200
98418688 78684002 20070400 134554624
98418688 78692194 20070400 134554624
98443264 78708578 20070400 134530048
98443264 78724962 20070400 134530048
98467840 78733154 20070400 134505472
98467840 78749538 20070400 134505472
98516992 78774114 20070400 134456320
98516992 78798690 20070400 134456320
98566144 78831458 20070400 134407168
98615296 78864226 20070400 134358016
98615296 78888802 20070400 134358016

Expected output:

Column1: Upward
Column2: Upward
Column3: Constant
Column4: Downward

CodePudding user response:

Example with the last column:

lst=[134579200,134554624,134554624,134530048,134530048,134505472,134505472,134456320,134456320,134407168,134358016,134358016]

sorted_lst=sorted(lst)

if(sorted_lst==sorted_lst[::-1]):
    print("Constant")
    
elif(lst==sorted_lst):
    print("Upward")

elif(lst==sorted_lst[::-1]):
    print("Downward")

[Output]

Downward
  • Related