Common interview questions in Python. Unpacking operations in Python and their application scenarios

  • This is a simple knowledge point, but some students do not understand

  • unpacking解包

  • Solution, corresponding to * or **, there is also a saying of automatic unpacking

  • iterable object corresponding to the package

Python student gift package click to jump to get

1. Automatic unpacking

Assigned demo

a,b = [1,2]
print(a) # 1
print(b) # 2

Take out the elements in the container one by one and assign them separately
. It doesn’t have to be a list, it can be any iterable object such as a tuple, a dictionary, a string, etc.
For example, this

t1,t2 = (1,2)
s1,s2 = 'ab'
d1,d2 = {
    
    'name':'wuxianfeng','age':18}  # d1 是 name, d2 是age

However, it should be noted that the unpacking of the dictionary is just a key
.

a, *b, c = range(5)  # b 就是[1,2,3]

2. Unpacking too much data*

Sometimes the data may be too much, then use the asterisk

a,b,c = 1,2,3,4
# 报错了
ValueError                                Traceback (most recent call last)
<ipython-input-6-e6e12dfe99e4> in <module>
----> 1 a,b,c = 1,2,3,4

ValueError: too many values to unpack (expected 3)

what to do?

a,*b,c = 1,2,3,4
# 此时的b的值为  [2, 3],是个列表

# 如果这样
a,b,*c = 1,2,3,4  # 那么c就是[3,4]

sort of like this

list1 = [1,2,3,4,5,6,7,8]
first , rest = list1[0],list1[1:]   
first , *rest = list1  # 以上两种写法一样的,都能让rest是 [2,3,4,5,6,7,8]

3. The use of asterisks in functions

code demo

def fun(x,y):
    print(f'x={
      
      x}')
    print(f'y={
      
      y}')

fun(1,2) 
# 输出如下..
# x=1
# y=2

What if this is the case?

fun([1,2]) 
# 报错了
TypeError                                 Traceback (most recent call last)
<ipython-input-8-6924a6444e6f> in <module>
----> 1 fun([1,2])

TypeError: fun() missing 1 required positional argument: 'y'

but it can

fun(*[1,2])  # x=1 y=2

fun(*'ab') # x=a y=b

fun(*(3,4)) # x=3 y=4

What if this is the case?

fun(*{
    
    'name':'wuxianfeng','age':18})  
# x=name
# y=age


What if this is the case?

for a, *b in [(1, 2, 3), (4, 5, 6, 7)]:
    print(b) 



Are the following two sentences correct? If yes, what is the value of a

*a, = range(5)
*a  = range(5)


4. Unpacking of two asterisks

change function

def fun(x=1,y=2):
    print(f'x={
      
      x}')
    print(f'y={
      
      y}')


All previous tests were ok

If it is a dictionary, you have to pass the value into it

def fun(x=1,y=2):
    print(f'x={
      
      x}')
    print(f'y={
      
      y}')
    
fun(**{
    
    'x':'wuxianfeng','y':18})
# 等价于
fun(x='wuxianfeng',y=18)


Specifically, you can look at the definition and application of the indeterminate parameters of the function, and I will not explain more here

Five, the application of the scene

merge dictionaries

d1 = {
    
    'x':1}
d2 = {
    
    'y':2}
d3 = {
    
    **d1,**d2}
d3  # {'x': 1, 'y': 2}

The same list can also be merged

li1 = [1,2]
li2 = [3,4]
li3 = [*li1,*li2]
li3

zip is an interesting built-in function

like this

li1 = ['a','b']
li2 = [1,2]
li = list(zip(li1,li2))
li # [('a', 1), ('b', 2)]
di = dict(zip(li1,li2))  # {'a': 1, 'b': 2}

# 如果要逆向呢? 
li1 = ['a','b']
li2 = [1,2]
result = zip(li1,li2)
print(list(zip(*result)))   # [('a', 'b'), (1, 2)]

Back to the topic, in the selenium course, there is such a paragraph

def find_element(self, by=By.ID, value=None) -> WebElement:
    pass
# 在后面项目课中的调用我们是这样做的
locator = 'id','ls_username'
driver.find_element(*locator) 
# 如果这样driver.find_element(locator)  显然报错在上面已经提到了。

↓ ↓ ↓ Add the business card below to find me, directly get the source code and cases ↓ ↓ ↓

Please add a picture description

Guess you like

Origin blog.csdn.net/weixin_45841831/article/details/130795682