sorted

单例方式

不羁的心 提交于 2019-11-29 09:43:00
单例方式 通过类的绑定方法 class Person(): _instance=None def __init__(self,port,host): self.port=port self.host=host @classmethod def get_sigoleton(cls): import settings if not cls._instance: cls._instance = cls(settings.PORT, settings.HOST) return cls._instance s1=Person.get_sigoleton() s2=Person.get_sigoleton() s3=Person.get_sigoleton() print(s1) print(s2) s4=Person('33306','192.168.1.1') print(s4) 通过装饰器 def get_sigoleton(cls): import settings instance=cls(settings.PORT,settings.HOST) def wrapper(*args,**kwargs): if len(args)!=0 or len(kwargs)!=0 res=cls(*args,**kwargs) return res else: return instance

python sort(),sorted()的参数key

匆匆过客 提交于 2019-11-29 06:36:15
python sort(),sorted()的参数key array = [ 1 , 2 , 8 , 4 , 5 , 8 , 3 , 5 , - 1 ] #key参数,要指向一个函数,返回该计算方法得到的、排序用的权值. print ( sorted ( array , key = lambda x : x ) ) #[-1, 1, 2, 3, 4, 5, 5, 8, 8] print ( sorted ( array , key = lambda x : - x ) ) #[8, 8, 5, 5, 4, 3, 2, 1, -1] print ( sorted ( array , key = lambda x : ( 4 - x ) ** 2 ) ) #[4, 5, 3, 5, 2, 1, 8, 8, -1] array . sort ( key = lambda x : - x ) print ( array ) #[8, 8, 5, 5, 4, 3, 2, 1, -1] 来源: https://blog.csdn.net/weixin_42764391/article/details/100540071

python sort list of json by value

自闭症网瘾萝莉.ら 提交于 2019-11-29 05:22:10
问题 I have a file consists of JSON, each a line, and want to sort the file by update_time reversed. sample JSON file: { "page": { "url": "url1", "update_time": "1415387875"}, "other_key": {} } { "page": { "url": "url2", "update_time": "1415381963"}, "other_key": {} } { "page": { "url": "url3", "update_time": "1415384938"}, "other_key": {} } want output: { "page": { "url": "url1", "update_time": "1415387875"}, "other_key": {} } { "page": { "url": "url3", "update_time": "1415384938"}, "other_key":

匿名函数和sorted函数

大憨熊 提交于 2019-11-28 14:02:24
使用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

Sort array of dictionary

人盡茶涼 提交于 2019-11-28 10:10:41
问题 I need to sort an array of dictionaries in the following ways. The array contains dictionaries with the following format: [{ fecha = "09:54:51"; "nombre_vendedor" = Rafaela; numero = 501; precio = 52; "punto_venta" = Base; tipo = Gold; } { fecha = "09:54:51"; "nombre_vendedor" = Miguel; numero = 400; precio = 40; "punto_venta" = Base; tipo = Gold; }] All I have to sort by the "numero" key. Anyone have any idea how to do this. I've been trying to sortedArrayUsingComparator method but I get no

Algorithm to merge multiple sorted sequences into one sorted sequence in C++

与世无争的帅哥 提交于 2019-11-28 07:17:06
问题 I am looking for an algorithm to merge multiple sorted sequences, lets say X sorted sequences with n elements, into one sorted sequence in c++ , can you provide some examples? note: I do not want to use any library 回答1: There are three methods that do the merging :- Suppose you are merging m lists with n elements each Algorithm 1 :- Merge lists two at a time. Use merge sort like merge routine to merge as the lists are sorted. This is very simple to implement without any libraries. But takes

Will a Python dict with integers as keys be naturally sorted?

做~自己de王妃 提交于 2019-11-28 07:00:48
问题 If I create a Python dict which uses integers as keys, can I safely assume that iterating over the dict will retrieve items in order according to key value? i.e. will my_dict = {} for x in range(0,100): my_dict[x] = str(x) for item in my_dict.items(): print item always result in printing the list in order of key value? 回答1: In short, no. I'm betting you noted that dictionaries use the hashes of keys as indexes in to an array, and since ints hash to their own values, you inferred that inserted

Native C# support for checking if an IEnumerable is sorted?

我与影子孤独终老i 提交于 2019-11-28 04:57:43
问题 Is there any LINQ support for checking if an IEnumerable<T> is sorted? I have an enumerable that I want to verify is sorted in non-descending order, but I can't seem to find native support for it in C#. I've written my own extension method using IComparables<T> : public static bool IsSorted<T>(this IEnumerable<T> collection) where T : IComparable<T> { Contract.Requires(collection != null); using (var enumerator = collection.GetEnumerator()) { if (enumerator.MoveNext()) { var previous =

Automatically sorted by values map in Java

邮差的信 提交于 2019-11-27 13:56:24
I need to have an automatically sorted-by-values map in Java - so that It keeps being sorted at any time while I'm adding new key-value pairs or update the value of an existing key-value pair, or even delete some entry. Please also have in mind that this map is going to be really big (100's of thousands, or even 10's of millions of entries in size). So basically I'm looking for the following functionality: Supposed that we had a class 'SortedByValuesMap' that implements the aforementioned functionality and we have the following code: SortedByValuesMap<String,Long> sorted_map = new

Python sorted 函数用法

若如初见. 提交于 2019-11-27 03:56:25
1.背景   在python中,通常需要使用排序函数。而对字典针对一个键值进行排序会经常使用到。记录sorted 函数的键值排序用法。 2.代码   data 一个list,list 中的元素由字典组成 dic { "label":True "seqence":“I love Python” "rate":0.5 "length": 13 }    如果需要对每个字典中的rate值进行从低到高排序,代码如下: datas = sorted(datas, key=lambda d: d["rate"]) 来源: https://www.cnblogs.com/siyuan1998/p/11343147.html