Get the App
SLTechnology News&Howtos  ›  Development  › 

What programming skills does Python have?

Shulou Source: shulou.com Published: 2022-06-03 05:24:50 10月02日 Update

Editor to share with you what programming skills Python has, I hope you will gain something after reading this article, let's discuss it together!

1. How to sort according to the value of the dictionary

We know that dictionaries are essentially hash tables, which cannot be sorted by themselves, but after Python3.6, dictionaries can be traversed in the order in which they are inserted. This is the ordered dictionary, the principle of which is why dictionaries are ordered after Python3.6.

Knowing this, it's easy to sort the dictionary's key values on the list, and then reinsert the new dictionary so that the new dictionary can traverse the output according to the value. The code is as follows:

> xs = {'aqure: 4,' baked: 3, 'caged: 2,' dumped: 1} > for k in xs.items v dictionary (): # traversal dictionary. Print (KBI 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 of dictionaries > new_xs = {k: v for kPowerv in new_order} # insert a new dictionary > new_xs {'Dictionary: 1,' cations: 2, 'baked: 3,' asides: 4} > for k V in new_xs.items (): the output of the new dictionary is ordered. Print (KBI 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)) [('daddy, 1), (' cantilever, 2), ('baked, 3), (' averse, 4)] 2. Elegant one-time judgment of multiple conditions

If there are three conditions, as long as one of them is true, you may write:

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 uses Python's built-in method any (). Any accepts an iterable object as a parameter, such as a list or tuple, and the any () method returns true as long as one of them is true. An example of usage is as follows:

> > any (['aura, (2pyr4), 3dje true]) True > any ([' await, (2p4), 3dhoe false]) True > any (['axie, (), 3pjue false]) True > any (['', (), 0False]) False > any (('ajar, (), 3flore) True > any (('', (), 0) False) False # # Note that the empty iterable object returns False > any (()) False > any ([]) False > any ('') False > any ({}) False

Corresponding to any () is the method all (), which is true only if it is all true. Note that empty iterable objects always return true.

> all (['aura, (2pr 4), ldre true]) / / list are all "true" True > all ([' averse, (), 1dre true]) / / list elements are empty tuple False > all (['ajar, (2jue 4), 0jue True]) False > all ([' averse, (2p4), 3) False]) False # # Note that the empty iterable object returns True > > all ([]) True > all (()) True > all ({}) True > all ('') True

To view the help documentation, you can enter 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.3, how to merge two dictionaries gracefully

The * * operator can unpack dictionaries, which is useful when merging dictionaries, such as:

> x = {'averse: 1,' baked: 2} > y = {'baked: 3,' caged: 4} > z = {* * x, * * y} > > z {'clocked: 4,' averse: 1, 'baked: 3}

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

> z = dict (x, * * y) > > z {'aqu: 1, 'censor: 4,' baked: 3} after reading this article, I believe you have a certain understanding of "what programming skills does Python have". If you want to know more related knowledge, welcome to follow the industry information channel, thank you for reading!

Tags: Dictionaries methods sorting ordering objects iterations that is techniques programming skills programming elegance size conditions articles output one-time three two code elements Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi MySQL MariaDB Shulou Tech Info