简体   繁体   中英

Python create and sort list based on variable value

I was wondering if someone can guide me in the right direction here. I'm working on something but I am stuck on creating and sorting a list based on their value. For example below

Sam = 10
John = 20
David = 5
Anthony = 7
Michael = 6

test[Sam, John, David, Anthony, Michael]
print(test)

So basically I want to be able to print the values assigned to the variable and then rank them from lowest to highest, for example; 5, 6, 7, 10, 20

Any help will be greatly appreciated

try sorted

Sam = 10
John = 20
David = 5
Anthony = 7
Michael = 6

test = [Sam, John, David, Anthony, Michael]
print(sorted(test))

# [5, 6, 7, 10, 20]

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