In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to sort Sorting Lists Tuples Dictionary in python? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Learn about python's Lists Tuples Dictionary sorting operation today.
Create a new python file named py3_sorting.py, and write the operation code in this file:
# define a listnums = [9, sorted, 8, 8, 4, 7, 6, 3] # use the sorted () function to sort # define a variable sort_nums to receive sort_nums = sorted (nums) print ('sorted list:', sort_nums) print ('original list:', nums) # sorted () function does not change the original list sort # returns a new list
# sorting nums.sort () print ('original list:', nums) # list.sort () using the list.sort () method changes the sort of the original list # list.sort () returns an one value # reverse sort sort_nums_reverse = sorted (nums,reverse=True) print ('reverse sort list:', sort_nums_reverse)
# so whether to use sorted () or list.sort () # choose according to your own needs # look at another listli = [- 6 meme 5meme 4meme 1meme 2jue 3] # sort by sorted () function # here adds a parameter key=abs# to sort by absolute value sort_li = sorted (li,key=abs) print ('sorted li list:', sort_li) # [1 minus 2 mine3,-4,-5 meme-6]
# Let's learn tuples sorting # define a tuple tup = (9, sort_tup, 8, 2, 4, 5, 7, 6, 3) # use the sorted () function to sort sort_tup = sorted (tup) print ('sorted tuples:', sort_tup) # tuples have no sort () method
# Let's learn Dictionary dictionary sorting dict_info = {'name':'yale','age':'25','os':'Windows'} # use the sorted () function to sort sort_dict = sorted (dict_info) print (' sorted dictionary:', sort_dict) # ['age',' name', 'os'] # here is to sort the key of the dictionary
Running effect:
Sorted list: [1, 2, 3, 4, 5, 6, 7, 8, 9] original list: [9, 2, 8, 1, 4, 5, 7, 6, 3] original list: [1, 2, 3, 4, 5, 6, 7, 8, 9] reverse sorted list: [9, 8, 7, 6, 5, 4, 3, 2, 1] sorted li list: [1, 2, 3,-4,-5] -6] sorted tuple: [1, 2, 3, 4, 5, 6, 7, 8, 9] sorted dictionary: ['age',' name', 'os'] the answer to the question about how to sort Sorting Lists Tuples Dictionary in python is shared here. I hope the above content can help you to a certain extent, if you still have a lot of doubts to be solved, you can follow the industry information channel to learn more related knowledge.
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.