sorting a dictionary by mean of their values

ahmadre147

I've been trying to create a function that sorts a CSV file by mean of their values then write the output to another CSV file. The problem is I just don't know how to sort the dictionary by mean of their values.

Here's the input:

mandana,5,7,3,15
jach,3,9,4,20,9,1,8,16,0,5,2,4,7,2,1
sina,19,10,19,6,8,14,3
sarah,0,5,20,14
julie,13,2,5,1,3,10,12,4,13,17,7,7
ally,1,9
sarvin,0,16,16,13,19,2,17,8

The expected output:

ally,5.0
jach,6.066666666666666
mandana,7.5
julie,7.833333333333333
sarah,9.75
sina,11.285714285714286
sarvin,11.375

And Here's the code:

def calculate_sorted_averages(input_file_name, output_file_name):
    with open (input_file_name) as f:
        a = []
        file1 = csv.reader(f)
        file2 = OrderedDict()
        for element in list(file1):
            file2[element[0]] = [int(j) for j in element[1:]]
        for i in file2:
            a.append((i + ',' + str(mean(file2[i]))))
    with open (output_file_name, 'w', newline='') as o:
        file3 = csv.writer(o, delimiter='\n')
        file3.writerow(a)
Andrej Kesely

You can use this example to read the data, convert the numbers to integers, calculate the mean, sort it and write back to file:

data = []
with open('input.txt', 'r') as f_in:
    for line in f_in:
        name, *vals = line.split(',')
        data.append([name, sum(map(int, vals)) / len(vals)])

data = sorted(data, key=lambda k: k[1])

with open('output.txt', 'w') as f_out:
    for line in data:
        print(','.join(map(str, line)), file=f_out)

Creates output.txt:

ally,5.0
jach,6.066666666666666
mandana,7.5
julie,7.833333333333333
sarah,9.75
sina,11.285714285714286
sarvin,11.375

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sorting a dictionary with values as tuple

From Dev

Sorting dictionary list values

From Dev

Sorting the dictionary by values

From Dev

Sorting dictionary keys for the equal values

From Dev

Sorting Dictionary on two values in swift

From Java

python sorting dictionary by length of values

From Java

Sorting a dictionary with multiple sized values

From Dev

sorting a list by values of a dictionary python

From Dev

Sorting a dictionary by value with tuples as values

From Dev

Sorting a dictionary and then replace the values by ranking

From Dev

sorting dictionary values asc in python

From Dev

Adding multiple values to a dictionary and then sorting it

From Dev

Is it ok sorting a dictionary with 20 values by replacing the key?

From Java

python dictionary sorting in descending order based on values

From Java

python dictionary sorting in descending order based on values

From Dev

Sorting a dictionary by values if it occurs once otherwise by keys

From Dev

Sorting dictionary based on largest sum of array values

From Dev

Sorting a dictionary by keys in alphabetical order if their values are equal

From Dev

Sorting by the values of a sub-dictionary and then by primary keys

From Dev

Swift (Xcode 7.3.1) and sorting using values in a Dictionary

From Dev

Sorting Array values in Dictionary of Swift 3

From Dev

Python Sorting Dictionary by values can be unstable? (in Hackerrank)

From Dev

Sorting the keys of a dictionary based on different values

From Dev

Sorting dictionary values in a ascending order a python counter

From Dev

sorting dictionary values in python - descending alphabetically

From Dev

Sorting a dictionary by values when the value is a list

From Dev

Mean and STD for values based on key in a single dictionary

From Dev

Sorting all the sub Dictionary using values of one of the sub dictionary

From Dev

python sorting a dictionary based on values, and order based on key if values are repeating

Related Related

  1. 1

    Sorting a dictionary with values as tuple

  2. 2

    Sorting dictionary list values

  3. 3

    Sorting the dictionary by values

  4. 4

    Sorting dictionary keys for the equal values

  5. 5

    Sorting Dictionary on two values in swift

  6. 6

    python sorting dictionary by length of values

  7. 7

    Sorting a dictionary with multiple sized values

  8. 8

    sorting a list by values of a dictionary python

  9. 9

    Sorting a dictionary by value with tuples as values

  10. 10

    Sorting a dictionary and then replace the values by ranking

  11. 11

    sorting dictionary values asc in python

  12. 12

    Adding multiple values to a dictionary and then sorting it

  13. 13

    Is it ok sorting a dictionary with 20 values by replacing the key?

  14. 14

    python dictionary sorting in descending order based on values

  15. 15

    python dictionary sorting in descending order based on values

  16. 16

    Sorting a dictionary by values if it occurs once otherwise by keys

  17. 17

    Sorting dictionary based on largest sum of array values

  18. 18

    Sorting a dictionary by keys in alphabetical order if their values are equal

  19. 19

    Sorting by the values of a sub-dictionary and then by primary keys

  20. 20

    Swift (Xcode 7.3.1) and sorting using values in a Dictionary

  21. 21

    Sorting Array values in Dictionary of Swift 3

  22. 22

    Python Sorting Dictionary by values can be unstable? (in Hackerrank)

  23. 23

    Sorting the keys of a dictionary based on different values

  24. 24

    Sorting dictionary values in a ascending order a python counter

  25. 25

    sorting dictionary values in python - descending alphabetically

  26. 26

    Sorting a dictionary by values when the value is a list

  27. 27

    Mean and STD for values based on key in a single dictionary

  28. 28

    Sorting all the sub Dictionary using values of one of the sub dictionary

  29. 29

    python sorting a dictionary based on values, and order based on key if values are repeating

HotTag

Archive