Python sequence featuring unpack, assignment chain, chain compare

  A sequence unpack

  Unpacking sequence (or iterables unpack): unpacking element is removed from the sequence in which the process, a sequence (or any iterables) unpacked, and the obtained value is stored into a series of variables.

  To unpack the number of elements of the sequence must contain the same number of goals you listed on the left side of the equal sign Under normal circumstances, otherwise Python will raise an exception. However, if you want to unpack the sequence number of different elements of the left case where the number of variables, the operator can use the asterisk (*) to collect excess value so not necessary to ensure the same value and the number of variables, it may also be with an asterisk the variables in any position. The right side of an assignment statement can be any type of sequence, but with an asterisk variables included in the final is always a list. But also the case in the number of variables and values ​​of phase. This embodiment can also be used to unpack the function parameter list.

  For example:

  \ >>> l, s, t, d = [ 'a', 'bc', 'de'], 'abcd', (1,2,3), {1: 'a', 2: 'b' } # define four lists, strings, tuples and dictionaries variables;

  \ >>> l1, l2, l3 = l # with l1, l2, l3 unpacking list, l1, l2, l3, respectively, to give values ​​of 'a', 'bc', 'de';

  \ >>> s1, * s2, s3 = s # use s1, s2, s3 unpack string, s1, s2, s3, respectively, to give values ​​of 'a', [ 'b', 'c'], 'd', Note the use of an asterisk;

  \ >>> t1, * t2 = t # with t1, t2 unpack tuple, t1, t2, respectively, to give a value of 1, [2, 3];

  \>>>d={i:chr(ord('a')+i) for i in range(5)} #d={0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}

  \ >>> d1, * d2, d3 = d # d1 = 0, d2 = [1,2,3], d3 = 4, that is unpacked dictionary key, not a dictionary entry

  Code execution screenshot below: Wuxi see male Which http://www.03913881333.com/

  

 

  

 

  In the above code, to get the dictionary keys and values ​​may be used: k, v = d.popitem () # k = 4, v = 'e'.

  Second, a complicated sequence of unpacking

  The above described sequence of unpacking the examples given is a relatively simple case, i.e., elements of a sequence type is not the type of container, but if the element in the sequence is the sequence occurs, at this time requires a complicated sequence unpacked.

  Unpacking sequence complex is embedded in a case where in the sequence the sequence, the left unpacked statement may comprise a corresponding sequence, so that the sequence of the sequence unpacked. See the examples below:

  \>>>a,[b,c],*d=(1,(2,3),4,5)

  \>>>a,(b,c),*d=(1,[2,3],4,5)

  After executing these two statements, a, b, c, d correspond to the corresponding values ​​are: 1, 2, 3, [4, 5]

  \>>>a,(b,*c),*d=(1,[2,3,10],4,5)

  After unpacking, a, b, c, d corresponding to the values ​​of: 1, 2, [3, 10], [4, 5]

  \>>>a,(b,c,e),*d=(1,[2,3,{10,11}],4,5)

  After unpacking, a, b, c, d, e corresponding values ​​were: 1, 2, 3, [4, 5], {10, 11}

  Third, the chain assignment

  Chain assignment statement is a single line of a number of variables assigned to the same value, the following syntax:

  2 = 1 = variable variable variable assignment expression n =

  The effect is similar to the actual implementation of the syntax:

  N = variable assignment expression

  Variable variable n = 2

  Variable 1 Variable 2 =

  This represents the implementation of the results of all the variables point to the same object.

  NOTE: The above description of the error on part of the assignment order, a sense of Xiebo You Johnny592 reminder to let people see this article deeper impression, old ape did not directly modify this part, but in this description, and in addition the blog "about chain Problematic assignment evaluation order "in the detailed description.

  Fourth, the chain compare

  Comparative Comparative chain is a statement in the middle of the direct use of logical operators to determine whether a certain interval of data, such as: if 0 =

  This section describes the sequence unpacking, chain assignments and comparisons chain, sequence unpacking a little more complicated, chain and chain relatively simple assignment, it did not begin to elaborate.

Guess you like

Origin www.cnblogs.com/djw12333/p/11350340.html