Python數(shù)據(jù)類型轉(zhuǎn)換詳解
1. Python的數(shù)據(jù)類型
上一遍博文已經(jīng)詳細(xì)地介紹了Python的數(shù)據(jù)類型,詳見鏈接Python的變量命名及數(shù)據(jù)類型。
在這里總結(jié)一下Python的數(shù)據(jù)類型:
- 字符串類型 String
- 數(shù)字類型 Number:
整形 int
浮點(diǎn)型 float
復(fù)數(shù) complex
- 布爾類型 Bool列
- 表類型 List
- 元組類型 Tuple
- 字典類型 Dictionary
- 集合類型 Set
可變數(shù)據(jù)類型:列表,字典,集合
不可變數(shù)據(jù)類型:字符串,數(shù)字類型,布爾類型,元組
容器類型數(shù)據(jù):字符串,列表,元組,字典,集合
非容器類型數(shù)據(jù):數(shù)字類型,布爾類型
2. Python數(shù)據(jù)類型轉(zhuǎn)換
由于不同的數(shù)據(jù)類型之間是不能進(jìn)行運(yùn)算的,所以我們需要數(shù)據(jù)類型轉(zhuǎn)換。Python中的數(shù)據(jù)類型轉(zhuǎn)換有兩種,一種是自動(dòng)類型轉(zhuǎn)換,即Python在計(jì)算中會(huì)自動(dòng)地將不同類型的數(shù)據(jù)轉(zhuǎn)換為同類型數(shù)據(jù)來進(jìn)行計(jì)算;另一種是強(qiáng)制類型轉(zhuǎn)換,即需要我們基于不同的開發(fā)需求,強(qiáng)制地將一個(gè)數(shù)據(jù)類型轉(zhuǎn)換為另一個(gè)數(shù)據(jù)類型。
2.1 自動(dòng)類型轉(zhuǎn)換
當(dāng)兩個(gè)不同類型的數(shù)據(jù)進(jìn)行運(yùn)算時(shí),結(jié)果會(huì)像更高精度進(jìn)行計(jì)算,精度等級:布爾 < 整型 < 浮點(diǎn)型 < 復(fù)數(shù)。
a = 10 b = True print(a + b) # 11 ''' 在和數(shù)字運(yùn)算時(shí),True轉(zhuǎn)為1,F(xiàn)alse轉(zhuǎn)為0 ''' a = 10 b = 3.14 print(a + b) # 13.14 ''' 整型與浮點(diǎn)型運(yùn)算時(shí),整型轉(zhuǎn)化為浮點(diǎn)型,結(jié)果也為浮點(diǎn)型 '''
2.2 強(qiáng)制類型轉(zhuǎn)換
- str( ):可以把其他類型數(shù)據(jù)轉(zhuǎn)化為字符串類型
- int( ):可以把其他類型數(shù)據(jù)轉(zhuǎn)化為整型
- float( ):可以把其他類型數(shù)據(jù)轉(zhuǎn)化為浮點(diǎn)型
- bool( ):可以把其他類型數(shù)據(jù)轉(zhuǎn)化為布爾類型
- list( ):可以把其他類型數(shù)據(jù)轉(zhuǎn)化為列表類型
- tuple( ):可以把其他類型數(shù)據(jù)轉(zhuǎn)化為元組類型
- dict( ):可以把其他類型數(shù)據(jù)轉(zhuǎn)化為字典類型
- set( ):可以把其他類型數(shù)據(jù)轉(zhuǎn)化為集合類型
2.2.1 其他轉(zhuǎn)字符串
所有類型都可以轉(zhuǎn)化為字符串類型。
a = 123 # int res = str(a) print(res, type(res)) # 123 <class 'str'> a = True # bool res = str(a) print(res, type(res)) # True <class 'str'> a = [1, 2, 3] # list res = str(a) print(res, type(res)) # [1, 2, 3] <class 'str'> a = (1, 2, 3) # tuple res = str(a) print(res, type(res)) # (1, 2, 3) <class 'str'> a = {1, 2, 3} # set res = str(a) # {1, 2, 3} print(res, type(res)) # {1, 2, 3} <class 'str'> a = {1: 'a', 2: 'b'} # dict res = str(a) print(res, type(res)) # {1: 'a', 2: 'b'} <class 'str'>
2.2.2 其他轉(zhuǎn)數(shù)字類型
數(shù)字類型之間可以相互轉(zhuǎn)換,但容器類型中只有字符串可以轉(zhuǎn)換為數(shù)字類型,并且字符串中的元素必須為純數(shù)字,否則無法轉(zhuǎn)換。
''' 1. 數(shù)字類型之間相互轉(zhuǎn)換 ''' a = 123 # int res = float(a) print(res, type(res)) # 123.0 <class 'float'> a = True # bool res = float(a) print(res, type(res)) # 1.0 <class 'float'> ''' 2. 字符串類型轉(zhuǎn)數(shù)字類型 ''' a = '123' # str res = int(a) print(res, type(res)) # 123 <class 'int'> a = '123abc' # str res = int(a) print(res, type(res)) # 此時(shí)python會(huì)報(bào)錯(cuò),報(bào)錯(cuò)類型為TypeError a = [1, 2, 3] # list res = int(a) print(res, type(res)) # 此時(shí)同樣會(huì)報(bào)錯(cuò),因?yàn)槌俗址酝獾钠渌萜黝愋投疾豢梢赞D(zhuǎn)換成數(shù)字類型
其他類型轉(zhuǎn)數(shù)字類型中有一個(gè)特殊情況,就是其他類型轉(zhuǎn)布爾類型。 bool( ) 可以把其他類型轉(zhuǎn)為True或False。
''' 1. 容器類型轉(zhuǎn)布爾類型: 容器中為空 --> False 容器中有元素 --> True ''' a = '' # 空字符串 res = bool(a) print(res, type(res)) # False <class 'bool'> a = '0' # 字符串中有元素 res = bool(a) print(res, type(res)) # True <class 'bool'> a = [] # 空列表 res = bool(a) print(res, type(res)) # False <class 'bool'> a = [1, 2, 3] # 列表中有元素 res = bool(a) print(res, type(res)) # True <class 'bool'> a = tuple() # 空元組 res = bool(a) print(res, type(res)) # False <class 'bool'> a = dict() # 空字典 res = bool(a) print(res, type(res)) # False <class 'bool'> a = set() # 空集合 res = bool(a) print(res, type(res)) # False <class 'bool'> ''' 2. 數(shù)字類型轉(zhuǎn)布爾類型: int類型中,0為False,其他為真 float類型中,0.0為False,其他為真 ''' a = 0 # int res = bool(a) print(res, type(res)) # False <class 'bool'> a = 0.0 # float res = bool(a) print(res, type(res)) # False <class 'bool'> a = 0.345 # float res = bool(a) print(res, type(res)) # True <class 'bool'>
2.2.3 其他轉(zhuǎn)列表類型
1.數(shù)字類型是非容器類型,不能轉(zhuǎn)換為列表
2.字符串轉(zhuǎn)列表時(shí),會(huì)把字符串中的每一個(gè)字符當(dāng)作列表的元素
3.元組轉(zhuǎn)列表時(shí),會(huì)把字符串中的每一個(gè)字符當(dāng)作列表的元素
4.字典轉(zhuǎn)列表時(shí),只保留字典中的鍵
5.集合轉(zhuǎn)列表時(shí),結(jié)果是無序的,因?yàn)榧媳旧砭褪菬o序的
a = '123' # str res = list(a) print(res, type(res)) # ['1', '2', '3'] <class 'list'> a = (1, 2, 3) # tuple res = list(a) print(res, type(res)) # ['1', '2', '3'] <class 'list'> a = {'name': 'Alice', 'Age': 5, 'Sex': 'Female'} # dict res = list(a) print(res, type(res)) # ['name', 'Age, 'Sex'] <class 'list'> a = {'a', 'b', 1, 2, 'c'} # set res = print(a) print(res, type(res)) # ['b', 2, 1, 'a', 'c'] <class 'list>
2.2.4 其他轉(zhuǎn)元組類型
其他類型數(shù)據(jù)轉(zhuǎn)元組類型與其他類型數(shù)據(jù)轉(zhuǎn)列表類型的規(guī)則相同。
a = 'abc' # str res = tuple(a) print(res, type(res)) # ('a', 'b', 'c') <class 'tuple> a = [1, 2, 3] # list res = tuple(a) print(res, type(res)) # (1, 2, 3) <class 'tuple> a = {'name': 'Alice', 'Age': 5, 'Sex': 'Female'} # dict res = tuple(a) print(res, type(res)) # ('name', 'Age, 'Sex') <class 'tuple> a = {'a', 'b', 1, 2, 'c'} # set res = tuple(a) print(res, type(res)) # ('b', 2, 1, 'a', 'c') <class 'tuple>
2.2.5 其他轉(zhuǎn)集合類型
1.數(shù)字類型是非容器類型,不能轉(zhuǎn)換為集合
2.字符串轉(zhuǎn)集合時(shí),結(jié)果是無序的
3.列表轉(zhuǎn)集合時(shí),結(jié)果是無序的
4.元組轉(zhuǎn)集合時(shí),結(jié)果是無序的
5.字典轉(zhuǎn)集合時(shí),只保字典中的鍵,結(jié)果是無序的
a = '123' # str res = set(a) print(res, type(res)) # {'3', '2', '1'} <class 'set'> a = ['a', 'b', 2, 1] # list res = set(a) print(res, type(res)) # {2, 1, 'b', 'a'} <class 'set'> a = ('a', 'b', 2, 1) # tuple res = set(a) print(res, type(res)) # {2, 1, 'b', 'a'} <class 'set'> a = {'name': 'Alice', 'Age': 5, 'Sex': 'Female'} # dict res = set(a) print(res, type(res)) # {'Age', 'name', 'Sex'} <class 'tuple>
2.2.6 其他轉(zhuǎn)字典類型
1.數(shù)字類型是非容器類型,不能轉(zhuǎn)換為字典
2.字符串不能轉(zhuǎn)字典類型,因?yàn)樽址荒苌啥壢萜?/p>
3.列表類型轉(zhuǎn)字典類型,列表必須為等長二級容器,子容器中的元素個(gè)數(shù)必須為2
4.元組類型轉(zhuǎn)字典類型,列表必須為等長二級容器,子容器中的元素個(gè)數(shù)必須為2集
5.合不能轉(zhuǎn)字典類型,因?yàn)榧喜恢С止?/p>
a = '123' # str res = dict(a) print(res, type(res)) # 此時(shí)python會(huì)報(bào)錯(cuò):ValueError: dictionary update sequence element #0 has length 1; 2 is required a = [[1, 2], ['a','b']] # 等長二級列表 res = dict(a) print(res, type(res)) # {1: 2, 'a': 'b'} <class 'dict'> a = ((1, 2), (3, 4), (5, 6)) # 等長二級元組 res = dict(a) print(res, type(res)) # {1: 2, 3: 4, 5: 6} <class 'tuple'> a = {{1, 2}, {3, 4}} # 等長二級集合 res = dict(a) print(res,type(res)) # 此時(shí)python會(huì)報(bào)錯(cuò):TypeError: unhashable type: 'set'
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注本站的更多內(nèi)容!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。