python-7- encapsulation and deconstruction

Encapsulation and deconstruction

  • Package
    • Separate multiple values ​​with commas and combine them
    • Essentially, return a tuple, but omit the parentheses
    • Python unique grammar, learn and learn from many languages ​​# For example ES6
    • t1 = (1,2) # defined as tuple
    • t2 = 1,2 # Pack 1 and 2 into tuples
    • type(t1)
    • type(t2)

Tuples are simpler than lists, and are lighter than lists in memory, so the list is closed

  • Examples
a = 4
b = 5
temp = a
a = b
b = temp
a,b=b,a 
# 上面语句当中,等号右边使用了封装,而左边就使用了解构
Deconstruction
  • Untie the elements of the linear structure and assign them to other variables in sequence

  • The number of variables accepted on the left must be the same as the number of elements unwrapped on the right

  • Examples

    • lst=[3,5]
    • first,second=lst
    • print(first,second)
  • Examples

a,b=1,2
a,b=(1,2)
a,b=[1,2]
a,b=[10.20]
a,b={10.20}
a,b=('a':10,'b':20) # 非线性结构也可以解构
a,b={10,20,30}
a,*b={10,20,30}
[a,b]=(1,2)
[a,b]=10,20
(a,b)={30,40}
python3 deconstruction
  • Use *变量名receiving, but can't use alone
  • After being *变量名collected, make up a list
  • Examples
    • lst=list(range(1,101,2))
    • head,*mid,tail=lst
    • *lst2 = lst
    • *body,tail=lst
    • head,*tail=lst
    • head,*m1,*m2,tail=lst
    • head,*mid,tail=*abcdefghijklmn
    • type(mid)
Discard variables
  • This is a convention, an unwritten agreement, not a standard
  • If you don't care about a variable, you can define the variable name as_
  • _It is a legal identifier and can also be used as a valid variable.
  • But defined as an underscore is to hope that it will not be used unless you clearly know that this data needs to be used
  • Examples
    • lst=[9,8,7,20]
    • first,*second=lst
    • head,*_,tail=lst
    • print(head)
    • print(tail)
    • #_It is a legal identifier. You will know that this variable just doesn't want to be used when you see the underline
    • print(_)
lst=[9,8,7,20]
first,*second=lst
_,*_,tail=lst
print(_)
print(tail)
print(_)
  • to sum up
    • _This variable itself has no semantics and no readability, so it is not intended for human use.
    • Many libraries in Python use this variable, which is very widely used,
    • Please do not _cause _conflicts with the library without specifying the scope of the variable
  • Exercise
    • lst = list (range (10)) # Such a list, take out the second, fourth, second to last
Exercise
  • From lst=[1,(2,3,4),5]extracted out 4
  • Environment variable JAVA_HOME=/usr/bin, return variable name and path
  • [1,9,8,5,6,7,4,3,2]Use the bubble method to sort the list , requiring the use of encapsulation and structure to interact with the data
lst=[1,(2,3,4),5]
a,(b,c,d),e=lst
print(a,b,c,d,e)

_,(*_,val),*_=lst
print(val)

_,[*_,val],*_=lst
print(val)
key,_,val='JAVA_HOME=/usr/bin'.partition('=')
print(key)
print(val)
  • to sum up
    • Deconstruction is a good function provided by Python, which can easily extract the value of complex data structures
    • With _the use of it, it will be more convenient!

Guess you like

Origin www.cnblogs.com/gnuzsx/p/12732889.html