Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythonbasic:如何仅根据数字对列表进行排序?_Python_Sorting - Fatal编程技术网

pythonbasic:如何仅根据数字对列表进行排序?

pythonbasic:如何仅根据数字对列表进行排序?,python,sorting,Python,Sorting,我承认我不知道如何给这个问题一个更好的标题-(有谁对标题有更好的想法吗 我的问题是: 我想对这个列表进行排序: a = ['at10', 'at11', 'at12', 'at13', 'at9', 'at1', 'at8'] 列表中的每一项都以两个字母开头,并有一些数字 预期结果如下。列表按数字排序 ['at1', 'at8', 'at9', 'at10', 'at11', 'at12', 'at13'] 我尝试了排序(a)和它的许多键设置。但是我没有得到结果。有人请帮助我吗?提前谢谢!因

我承认我不知道如何给这个问题一个更好的标题-(有谁对标题有更好的想法吗

我的问题是:

我想对这个列表进行排序:

a = ['at10', 'at11', 'at12', 'at13', 'at9', 'at1', 'at8']
列表中的每一项都以两个字母开头,并有一些数字

预期结果如下。列表按数字排序

['at1', 'at8', 'at9', 'at10', 'at11', 'at12', 'at13']

我尝试了排序(a)和它的许多
键设置。但是我没有得到结果。有人请帮助我吗?提前谢谢!

因为你的数据有一个模式(上面的索引2是一个数字),你可以通过定义下面的
getnum
(这是一种方式)函数来获得每个数字

sorted(a, key=lambda x:int(x[2:]))
#['at1', 'at8', 'at9', 'at10', 'at11', 'at12', 'at13']
在此之后,您可以根据这些值(按字符串中的数字排序)使用
sorted
函数


您可以使用排序方法:
a.sort(key=lambda i:int(i[2:])
a = ['at10', 'at11', 'at12', 'at13', 'at9', 'at1', 'at8']

def getnum(s):
    num = int(s[2:]);
    return num

b=sorted(a, key = getnum)
print(b)