The difference between Python sort, sorted, reverse, reverd

Source: Internet
Author: User

The difference between sort, sorted, reverse, reversed

!!! The first thing that error should distinguish is that sort and reverse are a way to list. strings, tuples, dictionaries, and collections do not have these two methods. and sorted and reversed are two built-in functions of Python, and only strings, lists, tuples, can be passed as parameters, dictionaries and collections are not supported, get a generator object
!!!

Sort and sorted ()

Sort
Only the list has this method, the original list is sorted directly, and no new list is generated

l1 = [12,2,34,54,5,17]l1.sort()  #[2, 5, 12, 17, 34, 54]l1.sort(reverse=True) #[54, 34, 17, 12, 5, 2]print(l1)

Sorted ()
will not change the order of the original sequence, you will get a new list from small to large, want to go from the big to the small directly after the sequence plus, reverse=true

s = "summer"res1 = sorted(s)print(s)  #summerprint(res1,type(res1)) #[‘e‘, ‘m‘, ‘m‘, ‘r‘, ‘s‘, ‘u‘] <class ‘list‘>l = [1,34,65,78,23]res2 = sorted(l)print(l) #[1, 34, 65, 78, 23]print(res2,type(res2)) #[1, 23, 34, 65, 78] <class ‘list‘>t = (12,34,32,12,4,23)res3 = sorted(t)print(t) #(12, 34, 32, 12, 4, 23)print(res3,type(res3)) #[4, 12, 12, 23, 32, 34] <class ‘list‘>d = {"name":"summer","age":"24","sex":"male"}res4 = sorted(d)print(d) #{‘name‘: ‘summer‘, ‘age‘: ‘24‘, ‘sex‘: ‘male‘}print(res4,type(res4))  #[‘age‘, ‘name‘, ‘sex‘] <class ‘list‘>#得到的是按照ASCII的顺序得到的keyst = {12,3,4,5}res5 = sorted(st)print(st) #{5, 3, 12, 4}print(res5,typsorted()与匿名函数配合使用l2 = [(1,1000),(2,18),(4,250),(3,500)]print(sorted(l2,key=lambda k:k[1]))print(sorted(l2,key=lambda k:k[1],reverse=True))[(2, 18), (4, 250), (3, 500), (1, 1000)][(1, 1000), (3, 500), (4, 250), (2, 18)]
Reverse and REVERESD ()

Reverse
Only the list has this method and reverses the original list directly

l1 = [1,2,10,7,5]res = l1.reverse()print(res)  #Noneprint(l1) #[5, 7, 10, 2, 1]

Reversed ()
Only strings, lists, tuples, can be passed as arguments, dictionaries and collections are not supported, and a generator object is not affected by the original sequence.

s = "summer"res1 = reversed(s)print(res1) #<reversed object at 0x101b60668>print(list(res1)) #[‘r‘, ‘e‘, ‘m‘, ‘m‘, ‘u‘, ‘s‘]print(s) #summerl = [1,34,65,78,23]res2 = reversed(l)print(res2) #<list_reverseiterator object at 0x101a60710>print(list(res2)) #[1, 34, 65, 78, 23]print(l) #[1, 34, 65, 78, 23]t = (12,34,32,12,4,23)res3 = reversed(t)print(res3) #<reversed object at 0x101a9a908>print(list(res3)) #[23, 4, 12, 32, 34, 12]print(t) #(12, 34, 32, 12, 4, 23)

The difference between Python sort, sorted, reverse, reverd

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.