How to convert nested list strings to integers then sort them in python 3?

user4617305

Not an experienced programmer! Currently studying a computing GCSE in school and need help with a problem.

I have a nested list that holds the information of student names and then their score in a text file, this file then needs to be imported into a nested list. I have done this using the code -

scoresave = []
with open('class1quizscoreboard.txt') as scoreboard:
    for line in scoreboard:
        scoresave.append(line.strip().split(','))
print (scoresave)

And this works fine with the output of

[['Emily Scott', ' 7'], ['Student Name', ' 6'], ['Another Student', ' 2']]

This is what I expected, but how would I change the scores of the students into integers?

I have tried multiple solutions from this site that are similar but none have worked for me.

Secret

You can use:

sorted(map(lambda x: [x[0], int(x[1])], scoresave), key=lambda x: x[1])

to get what you want.

Explanation:

This converts your nested list's second element into ints.

map(lambda x: [x[0], int(x[1])], scoresave)

We pass that whole into these:

sorted({}, key=lambda x: x[1])

which sorts the list

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert strings into integers in Python?

Nested list sort python 3

How to convert nested list of numbers to list of strings?

In PyParsing, how to define a setParseAction function to convert a list of strings to a list of integers?

convert a list of strings list into integers

How to convert a list of strings representing tuples to tuples of integers?

Convert integers inside a list into strings and then a date in python 3.x

How to convert list of strings to list of integers?

How to convert a list of strings to a list of ints in Python 3x

To sort a file which contains both strings and integers in python3

Convert a list of strings to a list of integers

Convert list of strings into tuple of integers

python capitalize nested list with strings and integers

How to convert nested list into list containing strings in python?

Left trim strings and convert them to integers

python convert string representation of nested list of integers to nested list of integers

Convert array of Strings to list of Integers?

How do I convert a list of strings to integers in Python

How to convert a nested list of strings to a one list?

create sub-list of order integers present in a list and store them in another nested list in python

How to type check a nested list of integers and convert to string?

Sort a list that contains both strings and integers (Python)

How to replace strings from a list and replace them with integers?

Convert a list of integers to list of strings

How to sort non-numeric strings by converting them to integers? Is there a way to convert strings to unique integers while being ordered?

How to sort a list of strings in Python

Convert list of strings into list of integers

Python: Nested list with repeated name of strings and different integers. How can I

How to sort list of classes, as if they were integers in python