vendredi 19 juin 2020

Counting how many times a variable appears in a list to work out proportions within data in python [closed]

I am trying to create a counter to determine how many times a type of fault appears within a list. The code is currently

  ini_list = [('G 05', 'Over-Speed', '1.63'), ('Load 23A_UF','over-Voltage', '11.37'), ('Load 21A_UF', '11.38'), ('Load 08A_UF', '11.38'), ('Load 07A_UF', '11.38'), ('Load 12A_UF', '11.38'), ('Load 24A_UF', '11.38'), ('Load 15A_UF', '11.38'), ('Load 16A_UF', '11.38'), ('Load 04A_UF', '11.38'), ('Load 03A_UF', '11.38'), ('Load 18A_UF', '11.38'), ('Load 25A_UF', '11.38'), ('Load 27A_UF', '11.39'), ('Load 26A_UF', '11.39'), ('G 05', 'Over-Speed', '1.63'), ('G 05', 'Over-Voltage', '1.63'),[('NSG_2', 'OverVoltage', '2.72'), ('G 01', 'Out of Step', '2.72')]


Counter_Speed = 0
Counter_voltage = 0
Counter_Out_Step =0
for thing in ini_list:
    if len(thing) == 3:
        element = thing[0]
        reason = thing[1]
        time = thing[2]
    else:
        element = thing[0]
        time = thing[1]
        reason = ""
    if reason == "'Over-Speed'":
        Counter_Speed = +1
    else reason == "'over-voltage'":
        Counter_voltage =+1
    else reason == "'Out of step'"
        Counter_out_step = +1
 #   print(f"element: {element}")
  #  print(f"reason: {reason}")
   # print(f"time: {time}")
    print(Counter_Speed)

I want to compare the amount of times different fault reasons occur.

For example have an output stating that the reason over voltage makes up 3 cases of all the fault reasons which is for instance 20% of the faults that occur. While reason over speed appears once which makes up 5% of the total fault reasons for example and reason out of step appears 1 time out of 133 cases making up for 0.75 percent of cases.

The code should thus count all the bracket instances, as each bracket content represents one fault, with the second entry reason being the reason for the fault. In some cases no reason has been recorded so this should be recorded as No_reason_given and also accounted for in the total proportion of faults accounted for.

I want to both display this numerically as percentages and visually in the form of plots.

Aucun commentaire:

Enregistrer un commentaire