Today we share 3 Python programming tips to see if you have used them before.

1, how to sort the dictionary by size

As we know, dictionaries are essentially hash tables, and they can’t be sorted by themselves, but after Python3.6, dictionaries can be traversed in the order they were inserted, and that’s how ordered dictionaries work, and you can read why dictionaries are ordered after Python3.6.

With this in mind, it’s easy to sort the dictionary’s key-value pair list and then reinsert the new dictionary so that it can iterate over the output by value size. The code is as follows:

>>> xs = {'a': 4.'b': 3.'c': 2.'d': 1}
>>> for k,v in xs.items():# walk through the dictionary
.    print(k,v)
...
a 4
b 3
c 2
d 1
>>> new_order = sorted(xs.items(), key=lambda x: x[1]) Sort the dictionary key-value pair list

>>> new_xs = { k : v for k,v in new_order} # ordered list inserts a new dictionary
>>> 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
Copy the code

You can also use the following methods to sort lists:

>>> import operator
>>> sorted(xs.items(), key=operator.itemgetter(1[())'d'.1), ('c'.2), ('b'.3), ('a'.4)]
Copy the code

2, elegant one-time judgment of multiple conditions

If you had three conditions and only one of them was true, you might write:

x, y, z = 0.1.0

if x == 1 or y == 1 or z == 1:
    print('passed')
Copy the code

In fact, the following three methods are more Pythonic

if 1 in (x, y, z):
    print('passed')

if x or y or z:
    print('passed')

if any((x, y, z)):
    print('passed')
Copy the code

The last one uses Python’s built-in method any(), which takes an iterable as an argument, such as a list or tuple, and returns true as long as one of them is true, as shown in the following example:

>>> 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 empty iterables return False
>>> any(())
False
>>> any([])
False
>>> any(' ')
False
>>> any({})
False
Copy the code

The counterpart of any() is the method all(), which is true only if all are true. Note that empty iterables always return true.

>>> all(['a', (2.4),1.True/ /])listfor"True"
True
>>> all(['a', ().1.True/ /])listEmpty elementtuple
False
>>> all(['a', (2.4),0.True])
False
>>> all(['a', (2.4),3.False])
False
  
## Note that empty iterables return True
>>>all([])
True 
>>> all(())
True
>>> all({})
True
>>> all(' ')
True
Copy the code

To view the help documentation, type help in the explanation:

>>> 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.

Copy the code

3. How to gracefully merge two dictionaries

The ** operator can unpack dictionaries, which is useful when merging dictionaries, for example:

>>> x = {'a': 1.'b': 2}
>>> y = {'b': 3.'c': 4}

>>> z = {**x, **y}

>>> z
{'c': 4.'a': 1.'b': 3}
Copy the code

If you are in Python2.x, you need to do this:

>>> z = dict(x, **y)
>>> z
{'a': 1.'c': 4.'b': 3}
Copy the code

4. Other practical programming tips

I have also written a number of such simple and practical programming tips, recommended reading:

1, 11 programming tips, easier to use

2. Share some practical questions

10 Tips to Make Your Code more elegant

4. These higher-order functions are built into Python

There are four commonly used data structures in the Standard library Collections

The last

If there is harvest, please also like, see support. You can also focus on learning a programming trick every day.