Home > Net >  How to count the numer of rows containing a string in a multiline list?
How to count the numer of rows containing a string in a multiline list?

Time:07-14

I'm a newbie of python...

I have created a list like a table with rows and 4 columns

70782763 Pilot 17,54 4,9

74006423 Pilot 19,95 4,9

74924394 Pilot 16,06 4,9

70782763 Auto 20,9 6,5

70706423 Track 19,1 5,9

74924394 Track 19,4 5,9

how can I count how many rows contain in the second column [1] the word "Pilot"?

the normal

table.count('Pilot') or table[1].count('Pilot')

returns me wrong values

and, eventually, how can I delete all the rows that have in the second column a value found less then 3 times?

Thanks

CodePudding user response:

if I understood correctly:

table = [[70782763, 'Pilot', 54, 9],
         [74006423, 'Pilot', 14, 9],
         [74924394, 'Pilot', 4, 9],
         [70782763, 'Auto', 9, 5],
         [70706423, 'Track', 15, 9],
         [74924394, 'Track', 45, 9]]

print(sum([x[1] == 'Pilot' for x in table]))

CodePudding user response:

possibly

pilotCount = 0
for x in y:
    if "Pilot" in x:
        pilotCount = pilotCount  1
        list.pop(list.index(x))
print(f"Pilot appears {pilotCount} times in the list.")
  • Related