Measuring open 3 - python (sliced, and other small set of methods)

First, sliced

In fact, to list / one kind of way string values, the only value, does not change the original value, the following applies:

. 1 List = [ ' Apple ' , ' Banana ' , ' Orange ' , ' Blue ' , ' Green ' , ' Red ' , ' Yellow ' ]
 2  Print (List [: 3])     # slice values care regardless tail 
3  # results: [ 'Apple', 'Banana'] 
. 4  
. 5 S = ' 123456789 ' 
. 6  Print (S [::    -1]) # minister last digit on behalf of, one by one take 1:00, after a lapse of 2 1 take 1, take the reverse -1, but this time write range, then it should be a negative number 
7 # Result: 987654321

Be the first practice after the introduction of a more important concepts

1 li = [1,1,2,3,3,4,5,6,7,8,9 ]
 2  for i in li:
 3      if i% 2! = 0:
 4        li.remove (i)
 5  print (li)

For this exercise, the list looks like the odd deleted through the loop, but the actual operation, we know the results did not think so simple) results: [1, 2, 4, 6, 8]

Why would it like this, because, list at the time of the cycle, the actual circulation is an index to it, when an element is deleted, the index will change accordingly, so that some elements of it less than a judge. So it should find a way to make the cycle time do not delete the list of elements, such subscript is not disturbed, each element will be able to be judged.

Here can be used for the slice value, plus list2 = li [:]

1 li = [1,1,2,3,3,4,5,6,7,8,9 ]
 2 list2 = li [:]
 3  for i in li2:
 4      if i% 2! = 0:
 5          li .remove (i)
 6  print (li)

Why do not list2 = li This direct assignment, where they pulled the two concepts, deep and shallow copy copy

Deep copy is only the value assignment over, but another has opened a piece of memory space, does not change list2 change when li

  the. list2 = it [:]

  b. list2 = copy.deepcopy(li)

Shallow copy is 'list2 = li' in this way, such an operation is in fact a two shared memory address list, Li changed the time, will change List2

 

Second, the collection

Collection of natural de-emphasis, is disorderly, defined by:

SET = A ()      # empty set 
B = { ' A ' , ' B ' , ' C ' , ' D ' }     # non-empty

list translated into strings manner also applies to turn into a set of strings, it may be used when necessary to write the contents of a collection of files.

1. Some sets of operating

. 1 S = SET ()     # empty set 
2 s.add (. 1)     # additive element 
. 3 s.remove (. 1)     # remove elements

2. intersection, union, difference, symmetric difference

Intersection: Take two sets of common portions - a.intersection (b) or a & b

And sets: two sets were combined and deduplication - a.union (b) or a | b

Difference set: a.difference (b) has taken a set, but there is no set of elements b - a.difference (b) or ab

Symmetric difference: the two sets are removed, the two sets are not the same retention portion - a.symmetric_difference (b) or a ^ b

. 1 Xn = [ ' xiaohei ' , ' xiaobai ' , ' xiaohuang ' , ' Xiaolan ' ]
 2 ZDH = [ ' xiaohei ' , ' xiaobai ' , ' xiaozi ' , ' xiaolv ' , ' Xiaohong ' ]
 . 3  
. 4 xn_set = SET ( Xn)
 . 5 zdh_set = SET (ZDH)
 . 6  
. 7  #print (xn_set.intersection (zdh_set)) # intersected 
. 8  # Print (xn_set & zdh_set) # intersected 
. 9  
10  # Print (xn_set.union (zdh_set)) # # Set and adding together the two set of repeating removal 
11  # print (xn_set | zdh_set) # union 
12 is  
13 is  # Print (xn_set.difference (zdh_set)) # difference sets, a set in which there is, there is no set of the b 
14  # Print (xn_set - zdh_set) difference set # 
15  
16  Print (xn_set.symmetric_difference (zdh_set)) # symmetric difference, there are two sets of the removal 
. 17  Print (xn_set ^ zdh_set)   # symmetric difference

 

Third, the true non-empty i.e. non-zero real i.e., formula list, ternary expressions

1. That is true not empty i.e., non-real 0

May be used in determining conditions, such as determining whether a list is empty, a direct 'if list' can be judged, when preceded Not negated, i.e., 'if not list'

2. The list of formula

1  # list formula 
2 Result = [STR (I) .zfill (2)   for I in Range (1,20) IF I <. 8   ]
 . 3  Print (Result)
 . 4  
. 5  # Common implementations 
. 6 result2 = []
 . 7  for I in Range (1,10 ):
 . 8      IF I <. 8 :
 . 9          J = STR (I) .zfill (2 )
 10          result2.append (J)

A first implementation of the above is equivalent to a second, and a first execution order that is the second performance. Role: make the code look cleaner.

3. a triplet of expressions

. 1 id_card = ' 410881199211111141 ' 
2  
. 3  # a first implementation 
. 4  # IF int (id_card [-2])% 2 == 0: 
. 5  #      Sex = 'M' 
. 6  # the else: 
. 7  #      Sex = 'M' 
8  # Print (Sex) 
. 9  
10  # a second implementation 
. 11 Sex = ' M '  IF int (id_card [-2])% 2 == 0 the else  ' M '  # a triplet of expressions 
12 is  Print (Sex)

Satisfies condition to give the variable previous value, or else the back of the assignment. Role: make the code look cleaner.

Guess you like

Origin www.cnblogs.com/april-aaa/p/10974024.html