python FAQ
时间: 2023-11-03 19:02:00 浏览: 66
引用:3.sorted()方法和list.sort()方法有什么区别? list = ["hello","hey","hi","allow","consent","approve"] print(list) print("-"*100) list_sorted = sorted(list) print(list_sorted) print("-"*100) print(list) list_sort = list.sort() print(list) print(list_sort()) ['hello', 'hey', 'hi', 'allow', 'consent', 'approve'] ---------------------------------------------------------------------------------------------------- ['allow', 'approve', 'consent', 'hello', 'hey', 'hi'] ---------------------------------------------------------------------------------------------------- ['hello', 'hey', 'hi', 'allow', 'consent', 'approve'] ['allow', 'approve', 'consent', 'hello', 'hey', 'hi'] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-26-b80c1a015c3a> in <module>() 8 list_sort = list.sort() 9 print(list) ---> 10 print(list_sort()) TypeError: 'NoneType' object is not callable 。引用:2.字典是可迭代的吗? for i in t: print(i) print("-"*100) for i in t.keys(): print(i) print("-"*100) for i in t.values(): print(i) print("-"*100) for i in t.items(): print(i) year month day hour minute second ---------------------------------------------------------------------------------------------------- year month day hour minute second ---------------------------------------------------------------------------------------------------- 2018 12 4 21 52 20 ---------------------------------------------------------------------------------------------------- ('year', '2018') ('month', '12') ('day', '4') ('hour', '21') ('minute', '52') ('second', '20') 。引用:1.如何取得字典中的键和值? t = {'year':'2018','month':'12','day':'4','hour':'21','minute':'52','second':'20'} print(t.keys()) print(t.values()) print(t.items()) dict_keys(['year', 'month', 'day', 'hour', 'minute', 'second']) dict_values(['2018', '12', '4', '21', '52', '20']) dict_items([('year', '2018'), ('month', '12'), ('day', '4'), ('hour', '21'), ('minute', '52'), ('second', '20')]) 。Python FAQ
Q: sorted()方法和list.sort()方法有什么区别?
A: sorted()方法和list.sort()方法都可以对列表进行排序,但是它们有一些区别。sorted()方法会返回一个新的已排序的列表,而不会修改原始列表。而list.sort()方法会直接修改原始列表,但不返回任何值。此外,list.sort()方法只能用于列表,而sorted()方法可以用于任何可迭代对象。
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)