Python 中星號(*)的用法小結(jié)
乘法和冪運(yùn)算符
最簡單的用法是利用星號作為基本的運(yùn)算符:
單個 ??*??? 用于乘法運(yùn)算;如果是列表,則是復(fù)制n次
兩個 ???** ?? 表示冪運(yùn)算
>>> 2*3 >>> 6 >>> 2**3 >>> 8
重復(fù)容器內(nèi)容
Python也支持類列表的容器類對象(即序列)與整數(shù)相乘,即為按照整數(shù)實(shí)現(xiàn)重復(fù)其中的元素數(shù)量。
>>> 'star' * 2 starstar >>> ['star'] * 2 ['star', 'star']
zeros_list = [0] * 10 print(zeros_list) #[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] zeros_tuple = (0,) * 10 print(zeros_tuple) #(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) vector_list = [[1, 2, 3]] * 3 print(vector_list) #[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
函數(shù)變參聲明
一般來說,函數(shù)往往接收固定數(shù)量的參數(shù);但是如果我們需要更大的靈活性,比如當(dāng)不確定將傳遞多少個參數(shù)時,此時將是星號??*?? 發(fā)揮作用的時候。
在Python中有兩類參數(shù),一類是位置參數(shù),另外一類是關(guān)鍵詞參數(shù),前者根據(jù)位置確定相應(yīng)值,后者則是依據(jù)參數(shù)名稱確定。
位置變參
*args可以收集任意個數(shù)的位置參數(shù),星號是必須要的,args可以是任意的變量名:
def fun(*args):
print(isinstance(args, tuple)) #返回的是True
for i in args:
print(i)
fun(1, 2, 3)上面的例子表示,我們雖然傳遞了3個參數(shù)1, 2, 3,但是fun函數(shù)把他們放到了一個元組。并且參數(shù)的個數(shù)不限。
關(guān)鍵字變參
**kwargs可以收集任意數(shù)量的按名參數(shù)/關(guān)鍵字參數(shù),**是必須要的,kwargs可以是任意變量名:
def fun2(**kwargs):
print(isinstance(kwargs, dict)) #返回的是True
pass
fun2(a=1, b=2, c=3)上面的例子表明,使用兩個星號,我們傳遞的參數(shù)是被當(dāng)作一個字典來進(jìn)行傳遞的,參數(shù)的個數(shù)不限。其實(shí)我們看kwargs這個名字就可以推出,這個參數(shù)是字典類型的。
def print_genius(*names):
print(type(names))
for n in names:
print(n)
print_genius('Elon Mask', 'Du Fu ', 'Li Bai')
# <class 'tuple'>
# Elon Mask
# Du Fu
# Li Bai
def top_genius(**names):
print(type(names))
for k, v in names.items():
print(k, v)
top_genius(Top1="Elon Mask", Top2="Du Fu", Top3="Li Bai")
# <class 'dict'>
# Top1 Elon Mask
# Top2 Du Fu
# Top3 Li Bai如上例所示,在定義函數(shù)時,我們可以定義一個以一個或兩個星號為前綴的參數(shù),以捕獲不限制數(shù)量的參數(shù)輸入。
總結(jié)如下:
- 以 一個 ??* ?? 為前綴的參數(shù)可以將任意數(shù)量的參數(shù)以元組形式傳入
- 以兩個 ??**?? 為前綴的參數(shù)可以將任意數(shù)量的參數(shù)以字典形式傳入
任意變參
按照慣例,當(dāng)我們定義的函數(shù)接收不定數(shù)量的參數(shù)時,我們一般采用以下函數(shù)定義形式:
def foo(*args, **kwargs):
pass注意:位置參數(shù)一定要放在關(guān)鍵字參數(shù)之前,下面這個聲明是錯誤的:
def save_ranking(**kwargs, *args):
...限制僅為關(guān)鍵字變參
星號??* ??的一個非常酷的用法是使函數(shù)只能接收關(guān)鍵字參數(shù)。
def genius(*, first_name, last_name):
print(first_name, last_name)
# genius('Li','Bai')
# TypeError: genius() takes 0 positional arguments but 2 were given
genius(first_name='Li', last_name='Bai')
# Li Bai上述代碼采用了星號??* ??限制了星號之后的參數(shù)必須采用關(guān)鍵字形式來調(diào)用上述函數(shù)。 實(shí)際上,如果我們只是想將一些參數(shù)限制為僅以關(guān)鍵字形式輸入同時另一部分參數(shù)依舊按照位置形式輸入,此時我們可以將位置參數(shù)放置在星號之前。
def genius(age, *, first_name, last_name):
print(first_name, last_name, 'is', age)
genius(28, first_name='Li', last_name='Bai')
# Li Bai is 28如果我們想強(qiáng)制只使用位置參數(shù),使用 / 號來實(shí)現(xiàn)
def only_positional_arguments(arg1, arg2, /):
pass如果你傳遞關(guān)鍵字參數(shù),會發(fā)生報錯
only_positional_arguments(arg1=1, arg2=2) """ TypeError: only_positional_arguments() got some positional-only arguments passed as keyword arguments: 'arg1, arg2' """
函數(shù)參數(shù)拆解
我們可以使用星號??* ??來解包可迭代對象,可以很優(yōu)雅的進(jìn)行參數(shù)傳遞。
位置參數(shù)拆解
調(diào)用函數(shù)時,在輸入?yún)?shù)前添加星號 * 可以對參數(shù)執(zhí)行提取操作,比如對列表、元組、字符串等迭代類型的輸入?yún)?shù)做提取之后,迭代類型的數(shù)據(jù)元素會被逐個取出。
print('list', sep=',')
print(*'list', sep=',')
print(['hello', 'world', '!'], sep=',')
print(*['hello', 'world', '!'], sep=',')
‘''
list
l,i,s,t
['hello', 'world', '!']
hello,world,!
‘''*號將字符串、列表、元組等進(jìn)行拆解,作為位置參數(shù)傳遞給函數(shù),函數(shù)要求能接收這些位置參數(shù)(函數(shù)的參數(shù)個數(shù)與拆解后的元素個數(shù)相等,或者可以接受位置變參)
def fun(a, b, c):
return a+b+c
test = [1, 2, 3]
print(fun(*test))
#把序列test中的每個元素,當(dāng)作位置參數(shù)傳遞到函數(shù)中去,就不用test[0],test[1]這樣了再例如:
from functools import reduce
primes = [2, 3, 5, 7, 11, 13]
def product(*numbers):
p = reduce(lambda x, y: x * y, numbers)
return p
product(*primes)
# 30030
product(primes)
# [2, 3, 5, 7, 11, 13]因?yàn)閜roduct()能接收任意參數(shù),我們本來需要將列表中的元素取出來,然后傳給此函數(shù)。但在這里,如果以*primes的方式向函數(shù)提供primes列表數(shù)據(jù),則primes所引用的列表會被解包,其中的每個素數(shù)都被傳給函數(shù),并被收集后用變量numbers引用。如果傳該列表primes給函數(shù),就不能解包,numbers所引用的元組中只有一個primes列表。
關(guān)鍵字參數(shù)拆解
在字典類型數(shù)據(jù)前添加兩個星號 **,對字典數(shù)據(jù)執(zhí)行提取,然后以關(guān)鍵字參數(shù)的形式輸入函數(shù)。
def fun(c, b, a):#注意順序
return a==1 and b==2 and c==3
test = {'a':1, 'b':2, 'c':3}
print(fun(**test))def foobar(param1=None, param4=None):
return "{}{}".format(param4, param1)
values = {"param1": "foo", "param4": "bar"}
print(foobar(**values)) #barfoo雖然字典中的定義的數(shù)據(jù)和函數(shù)定義的順序不一致,但是我們是按照關(guān)鍵字來進(jìn)行函數(shù)賦值的,所以這個函數(shù)返回的結(jié)構(gòu)是True
使用兩個星號實(shí)際是對字典進(jìn)行解包操作。
headers = {
'Accept': 'text/plain',
'Content-Length': 348,
'Host': 'http://mingrammer.com'
}
def pre_process(**headers):
content_length = headers['Content-Length']
print('content length: ', content_length)
host = headers['Host']
if 'https' not in host:
raise ValueError('You must use SSL for http communication')
pre_process(**headers)
# content length: 348
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "<stdin>", line 7, in pre_process
# ValueError: You must use SSL for http communication賦值變量解包
部分列表賦值
在給*valuename進(jìn)行賦值時,會自動將一個列表、元組、字符串賦值給valuename,對于前后的其他變量,解釋器將給它們單獨(dú)賦值,特別注意的,如果只有一個*變量,需要寫上逗號,表示它是一個列表在接收賦值。
無論原來的類型是什么類型,valuename賦值后都是列表類型
numbers = [1, 2, 3, 4, 5, 6]
# The left side of unpacking should be list or tuple.
*a, = numbers #直接寫成*a會提示語法錯誤
print(a) ## a = [1, 2, 3, 4, 5, 6]
*a, b = numbers
print(a) # a = [1, 2, 3, 4, 5]
print(b) # b = 6
a, *b, = numbers
print(a) # a = 1
print(b) # b = [2, 3, 4, 5, 6]
a, *b, c = numbers
print(a) # a = 1
print(b) # b = [2, 3, 4, 5]
print(c) # c = 6
str1 = 'python'
s1,*s2,s3 = str1
print(s1) #p
print(s2) #['y', 't', 'h', 'o']
print(s3) #n
t = ('a', 'b', 'c', 'd', 'e')
t1,*t2,t3 = t
print(t1) #a
print(t2) #['b', 'c', 'd']
print(t3) #c列表解包連接
可以通過*號先將列表解包,然后在通過括號將元素重新組織起來:
A = [1, 2, 3]
B = (4, 5, 6)
C = {7, 8, 9}
L = [*A, *B, *C]
print(L)
# [1, 2, 3, 4, 5, 6, 8, 9, 7]
A = ('1', '2', '3')
B = ('a', 'b', 'c')
C = {*A, *B}
print(C) #{'a', '1', 'c', 'b', '2', '3'}甚至在連接的過程中還可以加入其他的值:
my_list_1 = [1, 2, 3] my_list_2 = [10, 20, 30] num= 'union' merged_list = [*my_list_1, num, *my_list_2] print(merged_list) #[1, 2, 3, 'union', 10, 20, 30]
字典解包連接
兩個星號可以針對字典類型進(jìn)行解包
dict1 = {
'age': '22',
'country': 'BEIJING'
}
dict2 = {
'email': 'blog@example.com'
}
user_dict = {'username': 'kanye', **dict1, **dict2}
print(user_dict)
#{'username': 'kanye', 'age': '22', 'country': 'BEIJING', 'email': 'blog@example.com'}可以通過多種方式合并兩個字典
>>> x = {'a': 1, 'b': 2}
>>> y = {'a': 10, 'c': 30}
>>> yy = {'aa': 10, 'c': 30}
>>> z = x | y # Union operator introduced recently
>>> z
{'a': 10, 'b': 2, 'c': 30}
>>> x.update(y) # in place update
# as `y` value getting updated in `x`, `a` value getting overwritten from 1 to 10.
>>> x
{'a': 10, 'b': 2, 'c': 30}
>>> z = dict(**x, **yy)
>>> z
{'a': 1, 'b': 2, 'aa': 10, 'c': 30}
# same key in two dictionaries will throw error
>>> z = dict(**x, **y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: dict() got multiple values for keyword argument 'a'如果我們利用一個??*???作為??dict??? 的前綴,它的??key??? 將被解包;如果我們使用雙星號??**??? 作為前綴,其??value??? 將被解包;此時我們必須顯示使用??key??? 來接收解包后的??value?? 。
D = {'first': 1, 'second': 2, 'third': 3}
print(*D)
# first second third
# print(**D)
# TypeError: 'first' is an invalid keyword argument for print()
print('{first},{second},{third}'.format(**D))
# 1,2,3以上就是Python 中星號(*)的用法小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Python星號(*)的用法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python持續(xù)監(jiān)聽文件變化代碼實(shí)例
這篇文章主要介紹了Python持續(xù)監(jiān)聽文件變化代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
Python使用multiprocessing實(shí)現(xiàn)多進(jìn)程的詳細(xì)步驟記錄
multiprocessing包是Python中的多進(jìn)程管理包,與threading.Thread類似,它可以利用multiprocessing.Process對象來創(chuàng)建一個進(jìn)程,下面這篇文章主要給大家介紹了關(guān)于Python使用multiprocessing實(shí)現(xiàn)多進(jìn)程的詳細(xì)步驟,需要的朋友可以參考下2024-08-08
python 使用sys.stdin和fileinput讀入標(biāo)準(zhǔn)輸入的方法
今天小編就為大家分享一篇python 使用sys.stdin和fileinput讀入標(biāo)準(zhǔn)輸入的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
python中圖形庫turtle庫詳解(適用于計算機(jī)二級)
Turtle庫是Python語言中的一個圖形庫,可以用來繪制各種形狀,如線條、矩形、圓形等等,下面這篇文章主要給大家介紹了關(guān)于python中圖形庫turtle庫的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
Pytorch結(jié)合PyG實(shí)現(xiàn)MLP過程詳解
這篇文章主要為大家介紹了Pytorch結(jié)合PyG實(shí)現(xiàn)MLP過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
NumPy數(shù)組創(chuàng)建方法與索引訪問詳解
這篇文章主要介紹了NumPy數(shù)組創(chuàng)建方法與索引訪問,NumPy 中的核心數(shù)據(jù)結(jié)構(gòu)是 ndarray,它代表多維數(shù)組,NumPy 提供了多種方法來創(chuàng)建 ndarray 對象,文中通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-05-05

