sorted

匿名函数和sorted函数

匿名 (未验证) 提交于 2019-12-02 23:55:01
使用lambda函数取出字典中的value:d = {'a': 24, 'g': 52, 'i': 12, 'k': 33}print(list(map(lambda x:x[1],d.items())))[24, 52, 12, 33]使用sort函数排序 d_1=sorted(d.items(),key=lambda x:x[1],reverse=False) print('d_1:',d_1)d_1: [('i', 12), ('a', 24), ('k', 33), ('g', 52)] d_2=sorted(d.items(),key=lambda x:x[1],reverse=True)print('d_2:',d_2)d_2: [('g', 52), ('k', 33), ('a', 24), ('i', 12)] """按照每个元组元素的长度排序"""l = [(1,5,3),(1,3,6,3),(1,1,2,4,5,6),(1,9)]def func(item): return len(item)l1=sorted(l,key=func)print(l1)[(1, 9), (1, 5, 3), (1, 3, 6, 3), (1, 1, 2, 4, 5, 6)]l2=sorted(l,key=lambda x:len(x))print(l2) [(1, 9), (1

21 Merge Two Sorted Lists

匿名 (未验证) 提交于 2019-12-02 23:26:52
将两个链表合并成一个 输入两个链表节点指针 输出,链表节点 要求:节点不是新建的,而是原来的两个链表中的节点 ListNode * mergeTwoLists ( ListNode * l1 , ListNode * l2 ) { ListNode * head = new ListNode ( 0 ); ListNode * currentNode ; head -> next = NULL ; currentNode = head ; while ( l1 || l2 ){ if ( l1 && l2 ){ if ( l1 -> val < l2 -> val ){ currentNode -> next = l1 ; l1 = l1 -> next ; } else { currentNode -> next = l2 ; l2 = l2 -> next ; } } else if ( l1 ){ currentNode -> next = l1 ; l1 = l1 -> next ; } else if ( l2 ){ currentNode -> next = l2 ; l2 = l2 -> next ; } currentNode = currentNode -> next ; } currentNode -> next = NULL ; currentNode =

[转].Python中sorted函数的用法

匿名 (未验证) 提交于 2019-12-02 22:56:40
原文地址为: [转].Python中sorted函数的用法 我们需要对List、Dict进行排序,Python提供了两个方法 对给定的List L进行排序, 方法1.用List的成员函数sort进行排序, 在本地进行排序,不返回副本 方法2.用built-in函数sorted进行排序(从2.4开始), 返回副本,原始输入不变 --------------------------------sorted--------------------------------------- >>> help(sorted) Help on built-in function sorted in module __builtin__: sorted(...) ---------------------------------sort---------------------------------------- >>> help(list.sort) Help on method_descriptor: sort(...) ----------------------------------------------------------------------------- iterable:是可迭代类型; cmp:用于比较的函数,比较什么由key决定; key

python sorted(data, key=lambda _:_[0]) 排序解释

匿名 (未验证) 提交于 2019-12-02 22:11:45
>>>data = ([1, 4, 3], [3, 2, 5], [5, 1, 2], [4, 3, 1], [2, 5, 3]) >>>sorted(data, key=lambda _: _[0]) [[1, 4, 3], [2, 5, 3], [3, 2, 5], [4, 3, 1], [5, 1, 2]] >>>sorted(data, key=lambda _: _[1]) [[5, 1, 2], [3, 2, 5], [4, 3, 1], [1, 4, 3], [2, 5, 3]] >>>sorted(data, key=lambda _: _[2]) [[4, 3, 1], [5, 1, 2], [1, 4, 3], [2, 5, 3], [3, 2, 5]] 可以看出key=lambda_:_[0]相当于(此处元素指的是data中的任意列表): def f(元素): return 元素[字段索引] key = f(元素) 简单来说:将data中的多组列表,按照它们的第几个元素([0]~[2])的大小对比,进行组排序。 文章来源: https://blog.csdn.net/richand112233/article/details/89789383

Python中sorted函数使用,一看就会

匿名 (未验证) 提交于 2019-12-02 22:11:45
我们需要对List、Dict进行排序,Python提供了两个方法 对给定的List L进行排序, 方法1.用List的成员函数sort进行排序, 在本地进行排序,不返回副本 方法2.用built-in函数sorted进行排序(从2.4开始), 返回副本,原始输入不变 --------------------------------sorted--------------------------------------- >>> help(sorted) Help on built-in function sorted in module __builtin__: sorted(...) ---------------------------------sort---------------------------------------- >>> help(list.sort) Help on method_descriptor: sort(...) ----------------------------------------------------------------------------- iterable:是可迭代类型; cmp:用于比较的函数,比较什么由key决定; key:用列表元素的某个属性或函数进行作为关键字,有默认值,迭代集合中的一项; 返回值

java stream sorted排序 考虑null值

匿名 (未验证) 提交于 2019-12-02 21:53:52
项目里使用到排序, java里没有像C# 里的linq,只有stream,查找stream.sorted源码看到有个 Comparator.nullsLast 然后看了一下实现,果然是能够处理null值的排序,如: minPriceList.stream().sorted(Comparator. nullsLast (Comparator. comparing (l -> l.getCreateDate()))); 其中minPriceList是一个对象List,其中getCreateDate是日期字段,有可能为null,需求是对这组list的创建日期进行排序,达到目的。 文章来源: java stream sorted排序 考虑null值

Does Python sorted() actually CHANGE the list being sorted?

喜你入骨 提交于 2019-12-02 17:09:17
问题 rat_nreads = sorted([(self.markers2lens[m],n) for m,n in self.markers2nreads.items()],key = lambda x: x[1]) rat_nreads, removed = [], [] I saw this code and was really confused; what is the point of assigning rat_nreads to a sorted list if it's going to be assigned to an empty list right after? Does the first line actually change self.markers2nreads.items() in any way? 回答1: No, sorted creates a new list. So the code you posted doesn't make sense. Does the first line actually change self

Python: Determine if an unsorted list is contained in a 'list of lists', regardless of the order to the elements

假装没事ソ 提交于 2019-12-02 17:04:25
问题 I have a similar question to this question: Determine if 2 lists have the same elements, regardless of order? What is the best/quickest way to determine whether an unsorted list list1 is contained in a 'list of lists' myListOfLists , regardless of the order to the elements in list1 ? My attempt is wrapped up in the function doSomething(...) which I call many times: def doSomething(myListOfLists, otherInputs): list1 = [] ... # do something here with `otherInputs' ... # which gives `list1' some

Index of element in sorted()

 ̄綄美尐妖づ 提交于 2019-12-02 11:19:16
问题 When you call sorted(<#source: C#>, <#isOrderedBefore: (C.Generator.Element, C.Generator.Element) -> Bool##(C.Generator.Element, C.Generator.Element) -> Bool#>) you can access the two source elements for comparison via $0 and $1 . But how can I determine the index from where they were taken from the source? 回答1: You could pass into sorted not a collection of elements, but the indices of the elements: let a = ["hello","i","must","be","going"] let idxs = sorted(indices(a)) { a[$0] < a[$1] } //

python--sort()和sorted()高级排序

独自空忆成欢 提交于 2019-12-02 10:39:12
python--sort()和sorted()高级排序 转载: https://www.cnblogs.com/dyl01/p/8563550.html 1、list中的sort()方法:  ''' ## 一个list调用sort方法后,对原list进行排序 def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__ """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """ pass ''' key:是排序的条件,可以是:key=int,key=len, key=lambda.. reverse:表示是否反序,默认从小到大,默认为Flase ''' ##一个list调用sort方法后,对原list进行排序 ## 1、最简单的排序 ## 1、最简单的排序 l = [5,2,3,1,4 ] l .sort() print(l) ##输出:[1, 2, 3, 4, 5] l.sort(reverse=True)#反序 print(l) ##输出:[5, 4, 3, 2, 1] ##2、字符串排序 StrList = ['fb', 'bx', 'csw', 'qb', 'qqa',