Search code examples
pythonlistfilefieldaverage

Displaying indicators below the average in the list


I am newbie in python

My program should output less than average weight, but for some reason nothing happens

def minavg():
    print("\n" * 5)
    print(" List of passengers with baggage weight below average:")
    data = []
    from tabulate import tabulate
    with open('list.txt','r', encoding="utf-8") as f: 
        total = 0
        count = 0
        data = []
        for line in f:
            field1, field2, field3, field4 = line.split(',')
            total += int(field4)
            count += 1
    result = round(total / count)
    a = int(result)
    b = int(field4)
    if a < b in line.split():
            data.append(list(map(str.strip, line.split(',')))) 
    print(tabulate(data, tablefmt='grid', headers=('Name', 'ID', 'Place', 'Baggage Weight')))

The list itself:

Petter, 2345, 13, 12
Anna, 2243, 23, 31
Bob, 9112, 1, 0
Sergey, 9921, 32, 8
Jine, 1230, 5, 0
Tom, 9222, 6, 1

In theory, the code should display the names of people whose weight (last line) is less than 10 (since 10 is the average number).

what shows cmd:


Solution

  • Changes made:-

    (1) `if a < b in line.split():` -> `if a>b:` coz a storing the average value and b storing line value.
    (2) Remove the function min avg.
    (3) Redundancy of `data` initialization removed [i.e you have initialize data again when you have open file.].
    

    Code:-

    from tabulate import tabulate
    print(" List of passengers with baggage weight below average:")
    data = []
    with open('list.txt','r', encoding="utf-8") as f: 
        total = 0
        count = 0
        for line in f:
            field1, field2, field3, field4 = line.split(',')
            total += int(field4)
            count += 1
            result = round(total / count)
            a = int(result)
            b = int(field4)
            if a>b:
              data.append(list(map(str.strip, line.split(',')))) 
    print(tabulate(data, tablefmt='grid', headers=('Name', 'ID', 'Place', 'Baggage Weight')))
    

    Output:-

    List of passengers with baggage weight below average:
    +--------+------+---------+------------------+
    | Name   |   ID |   Place |   Baggage Weight |
    +========+======+=========+==================+
    | Bob    | 9112 |       1 |                0 |
    +--------+------+---------+------------------+
    | Sergey | 9921 |      32 |                8 |
    +--------+------+---------+------------------+
    | Jine   | 1230 |       5 |                0 |
    +--------+------+---------+------------------+
    | Tom    | 9222 |       6 |                1 |
    +--------+------+---------+------------------+
    

    Updated query:-
    Is there a way to sort the weight in ascending order?

    just sort before the print statement..

    data.sort(key= lambda x:x[3]) #Baggage weight in ascending order
    print(tabulate(data, tablefmt='grid', headers=('Name', 'ID', 'Place', 'Baggage Weight')))
    

    Output:-

     List of passengers with baggage weight below average:
    +--------+------+---------+------------------+
    | Name   |   ID |   Place |   Baggage Weight |
    +========+======+=========+==================+
    | Bob    | 9112 |       1 |                0 |
    +--------+------+---------+------------------+
    | Jine   | 1230 |       5 |                0 |
    +--------+------+---------+------------------+
    | Tom    | 9222 |       6 |                1 |
    +--------+------+---------+------------------+
    | Sergey | 9921 |      32 |                8 |
    +--------+------+---------+------------------+
    

    for descending try- data.sort(key= lambda x:x[3],reverse=True).