简体   繁体   中英

How to sort 1 list based on how 2 other lists were sorted?

I have two lists containing float values:

mean_fall_1 = [statistics.mean(d) for d in fall_1_gpa]
stdev_fall_1 = [statistics.stdev(d) for d in fall_1_gpa]

where:

fall_1_gpa = [[mean(sub_list) for sub_list in list] for list in fall1_grades]

Furthermore, I have a list of strings:

combination_fall_1 = [['CS105','MATH101','ENG101','GER'],['CS105','MATH101','GER','GER']]
fall1_grades = [[[4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0]],[[4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0], [4.0, 3.33, 3.33, 4.0]]]
mean_fall_1 = [2.9687393162393163,3.419960107803423]
stdev_fall_1 = [0.33945301919611576,0.2821718924791329]

What I am trying to do is to find the best combination of mean_fall_1 and stdev_fall_1 and list them(show first the highest mean possible with the lowest stdev possible and rank them like this). What I do is:

mean_fall_1, stdev_fall_1 = sorted(
        list(zip(*(zip(mean_fall_1, stdev_fall_1)))))
    mean_fall_1, stdev_fall_1 = (list(t) for t in sorted(list(zip(*(zip(mean_fall_1, stdev_fall_1))))))

and when I print(stdev and then mean) I get this result:

[0.2821718924791329, 0.33945301919611576]
[3.419960107803423, 2.9687393162393163]

but I want the combination_fall_1 list to be sorted accordingly with this in order for me to be able to show the user the combination of courses and not the mean and stdev only. I tried doing this:

mean_fall_1, stdev_fall_1,combination_fall_1  = sorted(
            list(zip(*(zip(mean_fall_1, stdev_fall_1,combination_fall_1 )))))
        mean_fall_1, stdev_fall_1 = (list(t) for t in sorted(list(zip(*(zip(mean_fall_1, stdev_fall_1,combination_fall_1 ))))))

But I keep getting this error:

TypeError: '<' not supported between instances of 'list' and 'float'

Is there another way to sort the combination_fall_1 list according to the other 2? or am I missing something?

The desired output:

[['CS105','MATH101','GER','GER'],['CS105','MATH101','ENG101','GER']]

Since the mean of ['CS105','MATH101','GER','GER'] is 3.419960107803423 and its st.dev 0.2821718924791329 which is better combination of ['CS105','MATH101','ENG101','GER'] with mean 2.9687393162393163 and st.dev 0.33945301919611576

Zip your strings, mean, and stdev together, then the problem boils down to sorting by one field descending (mean) and another ascending (stdev) while ignoring the strings, and after you just need to get the strings back out.

Here's a simplified example:

names = ['a', 'b']
mean = [2.96, 3.41]
stdev = [0.33, 0.28]

groups = list(zip(names, mean, stdev))
groups.sort(key=lambda x: (-x[1], x[2]))
# [('b', 3.41, 0.28), ('a', 2.96, 0.33)]

print([x[0] for x in groups])
# -> ['b', 'a']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM