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. The items () : # traversal dictionary... print(k,v) ... A 4 b 3 c 2 d 1 >>> new_order = sorted(xs.items(), key=lambda x: x[1]) >>> new_xs = {k: V is for k, v # new_order} in an ordered list to insert the new dictionary > > > new_xs {' d ': 1, "c" : 2,' b ': 3,' a ': 4} > > > for k, v in new_xs. The items () : The output of the new dictionary is ordered... print(k,v) ... d 1 c 2 b 3 a 4Copy 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 object iterative returns False > > > any(()) False >>> any([]) False >>> any('') False >>> any({}) FalseCopy 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]) / / the list for the "real" True > > > all ([' a ', (), 1, True]) / / free list element in a tuple False > > > all ([' a ', (2, 4), and 0, True]) False > > > all ([' a ', (2, 4), 3, False]) False # # note that empty object iterative returns True > > > all ([]) True > > > all True (()) > > > all ({}) True > > > all('') TrueCopy the code

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

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

Operators can unpack dictionaries, which is 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}
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

Well, that’s the end of my sharing. If someone else has a better trick. Feel free to discuss it in the comments!