简体   繁体   English

按升序对数字列表进行排序

[英]To sort a list of numbers in an ascending order

I am new to python programming and trying to write a code to answer the question below: Create a lambda function named sort to sort a list of numbers in an ascending order.我是 Python 编程的新手,正在尝试编写代码来回答以下问题:创建一个名为 sort 的 lambda 函数以按升序对数字列表进行排序。 The output is given as: sort([6,2,3,9,1,5]) == [1, 2, 3, 5, 6, 9] I have written the code below as my response and its working:输出为: sort([6,2,3,9,1,5]) == [1, 2, 3, 5, 6, 9]我写了下面的代码作为我的响应和它的工作:

a = [6,2,3,9,1,5]
sort = list(map(lambda a:a,a))
sort.sort()
print(sort)

My question is this is a genuine code or I missing something?我的问题是这是一个真正的代码还是我遗漏了什么?

Well I don't know if you're missing something, but as per the question, sort must be a lambda function;but in accordance to your code, sort seems like returning a list instead of being a lambda function.好吧,我不知道您是否遗漏了什么,但根据问题, sort必须是一个lambda函数;但根据您的代码, sort似乎返回一个list而不是一个lambda函数。

The following will satisfy the question :-以下将满足这个问题:-

a = [6,2,3,9,1,5]

sort = lambda x : sorted(x)

print(sort(a))

Here sort is a lambda function that returns a sorted list, as asked in the question.这里sort是一个lambda函数,它返回一个排序列表,如问题中所问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM