Today, I'd like to share three Python programming tips to see if you have used them?
1. How to sort by the size of dictionary values
We know that the essence of a dictionary is a hash table, which cannot be sorted by itself, but after Python 3.6, the dictionary can be traversed in the order of insertion. This is the principle of an ordered dictionary. You can read why Python 3 After 6, the dictionary is orderly.
Knowing this, it's easy to do. First sort the list with the key values of the dictionary, and then re insert the new dictionary, so that the new dictionary can traverse the output according to the size of the value. The code is as follows:
>>> xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1} >>> for k,v in xs.items():#Traversal dictionary ... print(k,v) ... a 4 b 3 c 2 d 1 >>> new_order = sorted(xs.items(), key=lambda x: x[1]) #Sort the list of dictionary key values >>> new_xs = { k : v for k,v in new_order} #Insert new dictionary into ordered list >>> new_xs {'d': 1, 'c': 2, 'b': 3, 'a': 4} >>> for k,v in new_xs.items(): ##The output of the new dictionary is ordered ... print(k,v) ... d 1 c 2 b 3 a 4
You can also use the following methods to sort the list:
>>> import operator >>> sorted(xs.items(), key=operator.itemgetter(1)) [('d', 1), ('c', 2), ('b', 3), ('a', 4)]
2. Elegant one-time judgment of multiple conditions
If there are three conditions, you can pass as long as one is true, maybe you will write this:
x, y, z = 0, 1, 0 if x == 1 or y == 1 or z == 1: print('passed')
In fact, the following three methods are more python
if 1 in (x, y, z): print('passed') if x or y or z: print('passed') if any((x, y, z)): print('passed')
The last one uses Python's built-in method any(). Any accepts an iteratable object as a parameter, such as a list or tuple. As long as one of them is true, the method any() returns true. The usage example is as follows:
>>> any(['a',(2,4),3,True]) True >>> any(['a',(2,4),3,False]) True >>> any(['a',(),3,False]) True >>> any(['',(),0,False]) False >>> any(('a',(),3,False)) True >>> any(('',(),0,False)) False ##Note that an empty iteratable object returns False >>> any(()) False >>> any([]) False >>> any('') False >>> any({}) False
Corresponding to any(), the method all() is true only if it is all true. Note that empty iteratable objects always return true.
>>> all(['a',(2,4),1,True]) //All list s are "true" True >>> all(['a',(),1,True]) //Empty tuple in list element False >>> all(['a',(2,4),0,True]) False >>> all(['a',(2,4),3,False]) False ##Note that an empty iteratable object returns True >>>all([]) True >>> all(()) True >>> all({}) True >>> all('') True
To view the help document, you can enter help:
>>> help(all) Help on built-in function all in module __builtin__: all(...) all(iterable) -> bool Return True if bool(x) is True for all values x in the iterable. If the iterable is empty, return True.
3. How to merge two dictionaries gracefully
Operators can unpack dictionaries, which is very useful when merging dictionaries, such as:
>>> x = {'a': 1, 'b': 2} >>> y = {'b': 3, 'c': 4} >>> z = {**x, **y} >>> z {'c': 4, 'a': 1, 'b': 3}
If in Python 2 In X, you need to do this:
>>> z = dict(x, **y) >>> z {'a': 1, 'c': 4, 'b': 3}
Well, my sharing is over. If there are other small partners, there are better tips. Welcome to discuss in the comments!