五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

Python數(shù)據(jù)類型轉(zhuǎn)換詳解

發(fā)布日期:2021-12-17 09:39 | 文章來源:站長之家

1. Python的數(shù)據(jù)類型

上一遍博文已經(jīng)詳細地介紹了Python的數(shù)據(jù)類型,詳見鏈接Python的變量命名及數(shù)據(jù)類型。

在這里總結(jié)一下Python的數(shù)據(jù)類型:

  • 字符串類型 String
  • 數(shù)字類型 Number:

整形 int

浮點型 float

復數(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ù)類型之間是不能進行運算的,所以我們需要數(shù)據(jù)類型轉(zhuǎn)換。Python中的數(shù)據(jù)類型轉(zhuǎn)換有兩種,一種是自動類型轉(zhuǎn)換,即Python在計算中會自動地將不同類型的數(shù)據(jù)轉(zhuǎn)換為同類型數(shù)據(jù)來進行計算;另一種是強制類型轉(zhuǎn)換,即需要我們基于不同的開發(fā)需求,強制地將一個數(shù)據(jù)類型轉(zhuǎn)換為另一個數(shù)據(jù)類型。

2.1 自動類型轉(zhuǎn)換

當兩個不同類型的數(shù)據(jù)進行運算時,結(jié)果會像更高精度進行計算,精度等級:布爾 < 整型 < 浮點型 < 復數(shù)。

a = 10
b = True
print(a + b) # 11
'''
在和數(shù)字運算時,True轉(zhuǎn)為1,F(xiàn)alse轉(zhuǎn)為0
'''
a = 10
b = 3.14
print(a + b) # 13.14
'''
整型與浮點型運算時,整型轉(zhuǎn)化為浮點型,結(jié)果也為浮點型
'''

2.2 強制類型轉(zhuǎn)換

  • str( ):可以把其他類型數(shù)據(jù)轉(zhuǎn)化為字符串類型
  • int( ):可以把其他類型數(shù)據(jù)轉(zhuǎn)化為整型
  • float( ):可以把其他類型數(shù)據(jù)轉(zhuǎ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)) # 此時python會報錯,報錯類型為TypeError
a = [1, 2, 3] # list
res = int(a)
print(res, type(res)) # 此時同樣會報錯,因為除了字符串以外的其他容器類型都不可以轉(zhuǎn)換成數(shù)字類型

其他類型轉(zhuǎn)數(shù)字類型中有一個特殊情況,就是其他類型轉(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)列表時,會把字符串中的每一個字符當作列表的元素

3.元組轉(zhuǎn)列表時,會把字符串中的每一個字符當作列表的元素

4.字典轉(zhuǎn)列表時,只保留字典中的鍵

5.集合轉(zhuǎn)列表時,結(jié)果是無序的,因為集合本身就是無序的

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)集合時,結(jié)果是無序的

3.列表轉(zhuǎn)集合時,結(jié)果是無序的

4.元組轉(zhuǎn)集合時,結(jié)果是無序的

5.字典轉(zhuǎn)集合時,只保字典中的鍵,結(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)字典類型,因為字符串不能生成二級容器

3.列表類型轉(zhuǎn)字典類型,列表必須為等長二級容器,子容器中的元素個數(shù)必須為2

4.元組類型轉(zhuǎn)字典類型,列表必須為等長二級容器,子容器中的元素個數(shù)必須為2集

5.合不能轉(zhuǎn)字典類型,因為集合不支持哈希

a = '123' # str
res = dict(a)
print(res, type(res)) # 此時python會報錯: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)) # 此時python會報錯:TypeError: unhashable type: 'set'

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注本站的更多內(nèi)容!

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關(guān)注
微信

關(guān)注官方微信
頂部