3 Python Programming Skills
- 2021-12-09 09:10:49
- OfStack
Today, share 3 Python programming tips to see if you have used them.
1. How to sort according to the size of dictionary values
As we know, the essence of dictionaries is hash tables, which cannot be sorted by themselves. However, after Python 3.6, dictionaries can be traversed in the order of insertion, which is the principle of ordered dictionaries. We can read why dictionaries are ordered after Python 3.6.
Knowing this 1 point, it is easy to do. First, sort the key values of the dictionary on the list, and then reinsert 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 key-value pairs in a dictionary
>>> new_xs = { k : v for k,v in new_order} # Ordered list inserts 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 orderly
... 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 once judgment of multiple conditions
If there are three conditions, as long as one of them is true, you can pass. 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
Pythonic
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 was used
Python
Built-in method
any()
,
any
The any () method returns true as long as one of the iterable objects, such as a list or tuple, is accepted as an argument, 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 iterative objects return False
>>> any(())
False
>>> any([])
False
>>> any('')
False
>>> any({})
False
And
any()
Correspondingly, it is the method
all()
Is true only if it is all true. Note that the empty iterable object 1 returns true straight.
>>> all(['a',(2,4),1,True]) //list Dall for " True "
True
>>> all(['a',(),1,True]) //list There is space in the element tuple
False
>>> all(['a',(2,4),0,True])
False
>>> all(['a',(2,4),3,False])
False
## Note that empty iterative objects return True
>>>all([])
True
>>> all(())
True
>>> all({})
True
>>> all('')
True
View the help document, and you can enter it in the interpreter
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
The ** operator 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}
If you are in Python2.x, you need to do this:
>>> z = dict(x, **y)
>>> z
{'a': 1, 'c': 4, 'b': 3}
These are the details of 3 Python programming skills. For more information about Python programming skills, please pay attention to other related articles on this site!