I have a list of file names in this form:
L='csv (2).zip', 'csv (5).zip', 'csv (1).zip', etc...
what is the easiest way to order this data numerically? So that I have:
csv (1).zip, csv (2).zip, csv (5).zip, ...
You can get the number within the parentheses, like this x.index("(") + 1 : x.rindex(")")
. So, we apply that on each and every element and convert that to a number
my_list = ['csv (2).zip', 'csv (5).zip', 'csv (1).zip']
print(sorted(my_list, key = lambda x: int(x[x.index("(") + 1 : x.rindex(")")])))
# ['csv (1).zip', 'csv (2).zip', 'csv (5).zip']
How could you use this to sort an array in python, for example, if I wanted to print the highest and lowest mark from this code.
print("Student Grades" ) #Title of the program
studentGrades = [] # Creating an array to hold the grades
for i in range(6):
grade = float(input("Please Enter percentage: "))
studentGrades.append(grade)
grade = str(grade)
saveFile = open('studentGrades.txt', 'a')
saveFile.write(grade)
saveFile.close()
print(studentGrades)
gradeTotal = 0
for i in range(len(studentGrades)):
gradeTotal = gradeTotal + studentGrades[i]
gradeAverage = gradeTotal/6
print"Your average grade is", gradeAverage, "%"
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.