In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "dict.keys().sort() in python3 can't use how to solve". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
The reason is that dict.keys() no longer returns a list.
Python3 keys(), values(), items() return iterators. If you need to return a list like Python2, just pass it to list:
keys = list(mydict.keys())
If you want to get keys and values at the same time, you should always use items
items = list(mydict.items())
The resulting tuple sequence can then be split into keys and values:
keys = [k for k,v in items]values = [v for k,v in items]
Or zip:
keys, values = tuple(zip(*items))
Python 3 zip also returns iterators, so you need to add tuple or list in front of it
Note that the sort() method is defined only in lists, while the sorted() method works for all iterable sequences.
And for any iterable sequence, sorted() returns a list,
The sorted function sorts dictionaries by key value
Sorted(iterable,key,reverse) has three parameters: iterable,key,reverse.
Iterable represents an object that can be iterated, such as dict.items(), dict.keys(), etc. Key is a function used to select the elements involved in the comparison, reverse is used to specify whether the sorting is reverse or sequential, reverse=true is reverse, reverse=false is sequential, reverse=false by default.
To sort dictionaries by key value, you can use the following statement:
Sorted(d.keys()) is used directly to sort the dictionary by key value. Here, the key value is sorted in order. If you want to sort in reverse order, you just need to set reverse to true.
The sorted function sorts dictionaries by value.
To sort the dictionary value, you need to use the key parameter. Here, we mainly provide a method using lambda expressions, as follows:
d.items() here actually converts d into an iterable object whose elements are ('lilee ', 25),('wangyan', 21),('liqun ', 32),('lidaming', 19). The items() method converts dictionary elements into tuples, and the lambda expression corresponding to the key parameter here means to select the second element of the tuple as the comparison parameter (if it is written as key=lambda item:item[0], it selects the first element as the comparison object, that is, the key value as the comparison object). lambda x:y where x represents the output parameter and y represents the return value of the lambda function), so this method can be used to sort the dictionary values. Note that the sorted return value is a list, and the name-value pairs in the original dictionary are converted to tuples in the list.
The content of "dict.keys().sort() in python3 can't be solved" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.