A sorting problem for dictionaries in the Python list. How to sort by the size of a key?

-sharp 
a = [{"info1" : "someinfo", "date" : 1}, {"info1" : "someinfo", "date" : 4}, {"info1" : "someinfo", "date" : 7}, {"info1" : "someinfo", "date" : 13}]
b = [{"info1" : "someinfo", "date" : 3}, {"info1" : "someinfo", "date" : 8}]
-sharp date,

-sharp c
c = [{"info1" : "someinfo", "date" : 1}, {"info1" : "someinfo", "date" : 3}, {"info1" : "someinfo", "date" : 4}, {"info1" : "someinfo", "date" : 7}, {"info1" : "someinfo", "date" : 8}, {"info1" : "someinfo", "date" : 13}]
The order of

an is: 1, 4, 7, 13
b: 3, 8

after sorting: the order of
c is: 1, 3, 4, 7, 8, 13

needs to be sorted according to the size of the date of the dictionary in the list, from small to large. How to write the python code?

there may be more than one arem b, and there may be several lists that need to be sorted like this.

how to sort faster and keep the code concise?

Thank you

Aug.27,2021

a = [{'info1': 'someinfo', 'date': 1}, {'info1': 'someinfo', 'date': 4}, {'info1': 'someinfo', 'date': 7}, {'info1': 'someinfo', 'date': 13}]
b = [{'info1': 'someinfo', 'date': 3}, {'info1': 'someinfo', 'date': 8}]

new_dic, result = dict(), []
-sharp datekey
for d in a + b:
    new_dic[d.get('date')] = d
-sharp sorted()
for d in sorted(new_dic.items()):
    result.append(d[1])

print(result)
The

function is implemented, but the time complexity.
the easiest way should be to do it one line:

print(sorted(a + b, key=lambda dic: dic['date']))

excuse me. When I remember how to do it, I usually use less sort. No, no, no.

this problem can be solved by using sorted function plus high-order key function directly, and the code is very simple.

all_bar = []
a = [{'info1' : 'someinfo', 'date' : 1}, {'info1' : 'someinfo', 'date' : 4}, {'info1' : 'someinfo', 'date' : 7}, {'info1' : 'someinfo', 'date' : 13}]
b = [{'info1' : 'someinfo', 'date' : 3}, {'info1' : 'someinfo', 'date' : 8}]

c = [{'info1' : 'someinfo', 'date' : 1}, {'info1' : 'someinfo', 'date' : 3}, {'info1' : 'someinfo', 'date' : 4}, {'info1' : 'someinfo', 'date' : 7}, {'info1' : 'someinfo', 'date' : 8}, {'info1' : 'someinfo', 'date' : 13}]

all_bar.extend(a)
all_bar.extend(b)

sorted(all_bar, key=lambda dic : dic['date'])

in this way, no matter how many original data there are, just extend, first and then sort them.

efficiency will not be too bad

  • How python violently cracked the URL back-end website

    http: httpbin.org get?user=r. < H1 > is not an official website, but the URL format is like this < H1 > password is correct return text 10000 password error return text 20000 ! ! [- sharp presentation data us] [1] er.txt root admin passwor...

    Jun.23,2021
  • What is the use of the Python compile function?

    describes the compile () function to compile a string into bytecode. Syntax the following is the syntax of the compile () method: compile (source, filename, mode [, flags [, dont_inherit]]) parameter source-- string or AST (Abstract Syntax Tree...

    Dec.22,2021
Menu