Home > front end >  how to remove a string from a list in Python
how to remove a string from a list in Python

Time:10-21

the first element of the list is a string and the rest are numbers. I want to perform mathematical operations between the numbers like median etc but I can't remove the first element a.k.a the string because I keep getting this error

AttributeError: 'str' object has no attribute 'pop'

I tried pop, remove etc

with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as infile:
    for line in infile:
        alter = line.split()[0]
        alter.pop()
        statistics.median(alter)

print(Alter)

Alter
28
25
28
26
22
20
25
21
21
25
24
25
26
22
26
20
27
22
22
26
23
20
22
26
24
22
20
20
19
21
19
19
33
23
21
29
21
25
26
19
23
20
25
21

input(alter) output = 22.5 the median

this is how the file data_exercise looks like

Alter   Km  Geschlecht  Raucher Transport   Farbe   Angst
28  444 m   1   Bahn    rot 0
25  50  m   0   Bahn    gelb    0
28  8000    m   0   Bahn    rot 1
26  9000    m   1   Bahn    blau    1
22  4000    m   0   Bahn    blau    0
20  0   m   0   Fahrrad grün    0
25  2500    f   0   Fuss    rot 0
21  5   m   0   Bahn    grün    0
21  200 m   0   Bahn    blau    0
25  100 m   0   Bahn    blau    0
24  326 m   0   Bahn    rot 0
25  5246    m   0   Bahn    rot 1
26  20000   m   0   Bahn    rot 0
22  10000   m   1   Bahn    blau    1
26  400 m   0   Bahn    blau    0
20  70  m   0   Bahn    rot 0
27  120 m   0   Bahn    rot 0
22  0   m   0   Bahn    grün    0
22  95  f   0   Fuss    rot 0
26  9000    f   0   Fuss    gelb    0
23  150 f   1   Fuss    rot 1
20  0   m   0   Fuss    gelb    0
22  320 m   1   Fuss    blau    0
26  400 m   0   Fuss    grün    0
24  0   m   0   Fuss    blau    0
22  20  f   0   Auto    blau    0
20  95  f   0   Auto    grün    0
20  100 m   0   Auto    grün    1
19  50  m   0   Auto    gelb    1
21  5   m   0   Auto    blau    1
19  25  m   0   Auto    grün    0
19  25  m   0   Auto    blau    0
33  230 m   1   Auto    blau    0
23  30  m   0   Auto    rot 0
21  96  m   1   Auto    blau    1
29  156 m   0   Auto    grün    0
21  7.5 m   0   Auto    grün    0
25  71  f   0   Bahn    grün    0
26  218 f   0   Bahn    blau    0
19  5632    f   0   Bahn    rot 1
23  600 f   0   Bahn    blau    0
20  0   f   0   Bahn    rot 1
25  2300    f   0   Bahn    blau    0
21  200 m   0   Bahn    blau    0

CodePudding user response:

Simply skip the first line. next consumes one element of the file iterator:

import statistics

with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as infile:
    header = next(infile)
    data = [int(line.split()[0]) for line in infile]
print(statistics.median(data))

Output:

22.5

CodePudding user response:

Your mistake is taking the first element of the List at line.split()[0].

If each line is formed by a string followed by numbers, and you just want to remove the first string, it should be done like this:

with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as infile:
    for line in infile:
        alter = line.split()
        alter.pop(0)
        statistics.median(line)

Notice as well that you must introduce an index of the position of the List that you want to delete with pop().

CodePudding user response:

Get rid of the string by not including the 0 index, then convert the strings representing numbers to actual floats so that they can be used in your calculations.

with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as file:
    for line in file:
        numbers = line.split()[1:]
        numbers = map(float, numbers)
        statistics.median(numbers)

CodePudding user response:

You can use something like this:

import statistics
list_ = []
with open('1.txt') as infile:
    for line in infile:
        if line.split()[0].isdigit():
            list_.append(int(line.split()[0]))
statistics.median(list_)
  • Related