[英]How do you sort a python array into alphabetical order?
I was trying to do a task set by my friend (below) to help with my knowledge and I ran into a problem.我正在尝试执行我的朋友(下)设置的任务以帮助我学习知识,但遇到了问题。 I need to sort an array alphabetically, yet I cannot find anything anywhere to help with the problem.
我需要按字母顺序对数组进行排序,但我在任何地方都找不到任何可以帮助解决问题的东西。 I have tried to use some sorting code that I have found, but that is for when the coder knows the inputs.
我尝试使用我找到的一些排序代码,但那是在编码器知道输入的情况下使用的。 However, because the user enters the inputs into an array I do not know how to proceed.
但是,由于用户将输入输入到数组中,我不知道如何进行。 The code and the task are below.
代码和任务如下。
"Create a program that will let a teacher enter between 20-30 students( must be onput validation) once all 20-30 students entered make them in to an array with their last names deciding whether they are at the front or back of the register." “创建一个程序,让教师输入 20-30 名学生(必须进行输入验证),一旦所有 20-30 名学生输入,将他们放入一个数组中,他们的姓氏决定他们是在登记表的前面还是后面。”
print ("Title: School Register")
print ("Date: 25/01/2022")
pupilNumber = int(input("How many pupils are in your class?"))
while pupilNumber < 20 or pupilNumber > 30:
print ("That is not a valid number of pupils, please re-enter your number of pupils.")
pupilNumber = int(input())
pupilName = [""]*pupilNumber
for counter in range (0, pupilNumber):
pupilName[counter] = str(input("Enter your pupils last names in alphabetical order."))
print ("The first person on the register is", pupilName[0])
print ("The last person on the register is", pupilName[counter])
You can sort a list simply by calling its sort()
method.您可以简单地通过调用其
sort()
方法对列表进行排序。
my_list = ["c", "a", "b"]
my_list.sort()
print(my_list)
# ["a", "b", "c"]
If you have any trouble due to mixing and matching capitalization, you can provide a sorting key.如果由于大小写混合和匹配而遇到任何问题,您可以提供排序键。
my_list = ["c", "a", "B"]
my_list.sort()
print(my_list)
# ["B", "a", "c"]
my_list.sort(key=lambda entry: entry.lower())
print(my_list)
# ["a", "B", "c"]
These modify my_list
in place.这些修改
my_list
就地。 If you'd prefer to make a new list that is a sorted copy you can do new_list = sorted(my_list)
or new_list = sorted(my_list, key=...)
如果您希望创建一个排序副本的新列表,您可以执行
new_list = sorted(my_list)
或new_list = sorted(my_list, key=...)
The answer it depends if you want to store the name and surname in two separated fields or not.答案取决于您是否要将姓名和姓氏存储在两个单独的字段中。
If you want to use a single field just for last name you can use the normal sort approach used for dictionaries, like mentioned from @buran.如果您只想将单个字段用于姓氏,您可以使用用于字典的正常排序方法,就像 @buran 中提到的那样。
sorted(pupilName)
If you are going to collect the name and surname you need to use a lambda function like explained here https://stackoverflow.com/a/20099713/18026877如果您要收集姓名和姓氏,您需要使用 lambda function 就像这里解释的Z5E056C500A1C4B6A7110B50D807BADE5097731/18026/
sorted(pupilName, key=lambda x: x[1])
In this code I'm supposing that you store the name putting surname as second field.在此代码中,我假设您存储将姓氏作为第二个字段的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.