Python parsing of various operations, and parsing of mathematical concepts What is the connection?

Parsing in python

Python supports a variety of analytical (comprehension) operation, such as a list comprehension, a collection of analytical, analytical tuples, dictionaries resolution. They create (derive) a new list, set of tuples, dictionaries according to certain elements. It is sometimes called derivation, such as a list comprehension, collection derivations.

Here is an example of a list comprehension:

1 >>> [ i*2 for i in range(10) if i % 2 == 0 ]
2 [0, 4, 8, 12, 16]

Here is a list of resolution because brackets [XXXX] used, it represents a new list is derived according to the conditions. Several built-in types in Python parsing rules:

  • If you are using square brackets to indicate a list comprehension
  • If you are using braces, it represents the set of analytic
  • If you are using braces, and which elements are key: value mode, parsing the dictionary

Note: If you are using parentheses represents the generator expressions, rather than analytical.

E.g:

1 # 集合解析
2 >>> { i*2 for i in "abcd"}
3 {'aa', 'cc', 'dd', 'bb'}
4
5 # 字典解析
6 >>> { k:v for k,v in zip(("one","two","three"),(1,2,3)) }
7 {'one': 1, 'two': 2, 'three': 3}
8 >>> { k: k*2 for k in "abcd" }
9 {'a': 'aa', 'b': 'bb', 'c': 'cc', 'd': 'dd'}
在学习过程中有什么不懂得可以加我的
python学习交流扣扣qun,×××
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容

Of course, Python there are other parsing scheme.

Python parsing of various operations, and parsing of mathematical concepts What is the connection?

Analytical mathematical concept of

Analytical computer language from the set of mathematical concepts described (corresponding to the set resolution). As shown below:

This corresponds to the following list for analysis:

[ i * 2 for i in range(10) if i % 2 == 0 ]

among them:

  • x ∈ N represents the element of containers, the container element is iterated objects parsed
  • This corresponds to a range (10) in the list of parsed for Python, as long as the data objects is iterative, may be provided as a container element
  • x is a variable, a container element
  • Corresponding to the list of analytical i
  • x²> 3 is represented by predicates conditional formula is optional, the parsing process for screening qualifying element
  • This corresponds to the list of parsed if i% 2 == 0, note that the predicate part is optional
  • 2 * x represents the external expression, the elements used to generate a new list / set / dictionary / tuple
  • Corresponding to the list of analytical i * 2
  • {} Means that the expression of outer container element is stored in a collection container
  • Parsing the list corresponding to [], the newly generated element is represented in the list of elements

The article also think I may wish to take your collection together, have any comments or opinions are welcome to comment!

Guess you like

Origin blog.51cto.com/14568144/2444883