Python问题 如图,lists都能输出了,为什么排序后的结果为None?求大神 python中输出结果不对,请求大神们指出哪里的问题,多了两...

python\u4f7f\u7528lambda\u8fdb\u884c\u6392\u5e8f\uff0c\u4e3a\u4ec0\u4e48\u8f93\u51faNone

book_sort\u8f93\u51fanone\u662f\u56e0\u4e3aList.sort()\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c
book_list.sort(key=lambda x:(x[1],x[1]/x[2]))\u8c03\u7528\u540e\u4f1a\u5bf9book_list\u8fdb\u884c\u6392\u5e8f,\u4f60\u6253\u5370book_list\u5c31\u662f\u6392\u5e8f\u7684\u7ed3\u679c
print("\u6392\u5e8f\u540e\u7684\u987a\u5e8f\u4e3a:",book_list)

\u56e0\u4e3a\u51fd\u6570describe_restaurant\u548copen_restaurant\u6ca1\u6709\u8fd4\u56de\u503c,\u6240\u4ee5\u8c03\u7528\u540e,\u6253\u5370\u8fd4\u56de\u503c\u4e3aNone.
\u6240\u4ee5\u4e24\u51fd\u6570\u76f4\u63a5\u8c03\u7528\u5c31\u53ef\u4ee5\u4e86,\u4e0d\u7528\u518d\u6253\u5370.
\u5b8c\u6574\u7684Python\u7a0b\u5e8f\u5982\u4e0b(\u6539\u52a8\u7684\u5730\u65b9\u89c1\u6ce8\u91ca)
class Restaurant():
def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_type
def describe_restaurant(self):
print(self.restaurant_name.title()+" \u65b0\u54c1\u63a8\u51fa "+self.cuisine_type)
def open_restaurant(self):
print(self.restaurant_name+" \u6b63\u5728\u8425\u4e1a\u4e2d\u3002 ")
my_restaurant= Restaurant("\u70fd\u667a\u5473","\u7c73\u996d\uff0c\u6cb9\u6761\uff0c\u8c46\u6d46\uff0c\u5305\u5b50\uff0c\u997a\u5b50")
my_restaurant.describe_restaurant() #\u8fd9\u91cc\u53bb\u6389\u6253\u5370\u8bed\u53e5
my_restaurant.open_restaurant() #\u8fd9\u91cc\u53bb\u6389\u6253\u5370\u8bed\u53e5
\u6e90\u4ee3\u7801(\u6ce8\u610f\u6e90\u4ee3\u7801\u7684\u7f29\u8fdb)


List.sort() 是针对列表自己内部进行排序,不会有返回值,因此返回为None。

以下写法均会返回None:

  1. a = lists.sort()  

  2. print( lists.sort() )

  3. return lists.sort()

解决方法就是将排序语句单独作为一行。或者使用sorted(list) 函数。



list.sort()方法将列表中的元素进行排序,但此方法本身没有返回值。你用print()打印sort()方法的返回值,得到的自然是None。

lists.sort()

print(lists)
.sort()不做任何返回,所以你print这个只能得到none

sort()方法是把数组变量里的数据进行排序,但没返回值,你直接输出sort方法,当然是没有值的咯

扩展阅读:python求1+2+3+n的和 ... 猴子吃桃问题python答案 ... python编程入门自学 ... python百钱百鸡问题答案 ... python代码大全 ... python中li 1 什么作用 ... python代码自动生成器 ... 百钱买百鸡python解析 ... python ai ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网