栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python-001-数据类型转换

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python-001-数据类型转换

1 其他类型→字符串类型,使用str()方法
  • int 转 str
>>> a = 12
>>> type(a)

>>> str(a)
'12'
  • bool 转 str
>>> b = True
>>> type(b)

>>> str(b)
'True'
  • list 转 str
>>> c = [1, 2]
>>> type(c)

>>> str(c)
'[1, 2]'
  • tuple 转 str
>>> d = (1, 2)
>>> type(d)

>>> str(d)
'(1, 2)'
  • set 转 str
>>> e = {1, 2}
>>> type(e)

>>> str(e)
'{1, 2}'
  • dict 转 str
>>> f = {'k1':1, 'k2':2}
>>> type(f)

>>> str(f)
"{'k1': 1, 'k2': 2}"

2 int/ bool→float,使用float()方法
  • int 转 float
>>> m = 32
>>> type(m)

>>> float(m)
32.0
  • bool 转 float
>>> float(True)
1.0

3 str→int,使用int()方法
  • 纯数字的str 转 int,直接去掉字符串中引号
>>> int('13')
13
  • 表示十六进制的str 转 十进制的int
>>> a = '0A'
>>> int(a, 16)
10
  • 表示八进制的str 转 十进制的int
>>> int('011', 8)
9
  • 表示二进制的str 转 十进制的int
>>> int('1000', 2)
8

4 十进制int →十六进制/八进制/二进制的str,使用format()方法
  • 十进制int 转 十六进制的str
# 方法一
>>> hex(10)  # 带0x
'0xa'
# 方法二
>>> format(10, 'x')  # 不带0x
'a'
>>> format(10, 'X')
'A'
>>> format(10, '#x') # 带0x
'0xa'
  • 十进制int 转 八进制的str
>>> format(10, 'o')
'12'
>>> format(10, '#o') # 带0o
'0o12'
  • 十进制int 转 二进制的str
>>> format(10, 'b')
'1010'
>>> format(10, '#b') # 带0b
'0b1010

5 其他类型→bool,使用bool()方法
  • str 转 bool
>>> bool('')  # 空字符串
False
>>> bool('2a')
True
  • list 转 bool
>>> bool([])  # 空列表
False
>>> bool([1,2])
True
  • tuple 转 bool
>>> bool(())  # 空元组
False
>>> bool((1,))
True
  • set 转 bool
>>> bool(set())  # 空集合
False
>>> bool({1})
True
  • dict 转 bool
>>> bool({})  # 空字典
False
>>> bool({1})
True
  • int/ float 转 bool
>>> bool(1)  # 非零数
True
>>> bool(1.2)  # 非零数
True
>>> bool(0)
False
>>> bool(0.000000)
False

6 其他类型 → list,使用list()方法
  • str 转 list
>>> list('5983')
['5', '9', '8', '3']
  • tuple 转 list
>>> list((1,5,6,7))
[1, 5, 6, 7]
  • dict 转 list,仅保留字典中的键
>>> list({'k1':1, 'k2':2,'k3':3})
['k1', 'k2', 'k3']
  • set 转 list, 结果是无序的
>>> list({5, 8, 3})
[8, 3, 5]
  • 注:数字无法转list

7 其他类型 →tuple,使用tuple()方法
  • str 转 tuple
>>> tuple('esfsafe')
('e', 's', 'f', 's', 'a', 'f', 'e')
  • list 转 tuple
>>> tuple([1,2,3,3,3])
(1, 2, 3, 3, 3)
  • dict 转 tuple,仅保留关键字
>>> tuple({'k1':1, 'k2':2,'k3':3})
('k1', 'k2', 'k3')
  • set 转 tuple,是无序的
>>> tuple({5, 9, 3, 6})
(9, 3, 5, 6)
  • 注:数字无法转tuple

8 其他类型 → set, 使用set()方法
  • str 转 set
>>> set('esfsafe')
{'a', 'f', 's', 'e'}
  • list 转 set
>>> set([1,1,1,3,5,9, 2, 3])
{1, 2, 3, 5, 9}
>>> set(['q', '2', '6'])
{'2', 'q', '6'}
  • tuple 转 set
>>> set((1,5,6,7,1,5,6,7))
{1, 5, 6, 7}
>>> set((4, 5, 1,1, 2, 'd', 'f'))
{1, 2, 4, 5, 'f', 'd'}
  • dict 转 set
>>> set({'k1':1, 'k2':2,'k3':3})
{'k3', 'k1', 'k2'}
  • 注:数字无法转set

9 其他类型 → dict, 使用dict()方法
  • 数字和字符串不能转为dict
  • 单个列表若转为dict,则需要满足条件:列表必须为等长二级容器,子容器中的元素个数必须为2
>>> dict([[1, 2], ['a', 'b']])
{1: 2, 'a': 'b'}

>>> dict([[1,2,3,4], ['a', 'd', 'c', 'f']])
Traceback (most recent call last):
  File "", line 1, in 
    dict([[1,2,3,4], ['a', 'd', 'c', 'f']])
ValueError: dictionary update sequence element #0 has length 4; 2 is required
  • 单个元组若转为dict,则需要满足条件:元组必须为等长二级容器,子容器中的元素个数必须为2
>>> dict(((1,2), ('a', 'c')))
{1: 2, 'a': 'c'}

>>> dict(((1,2, 3), ('a', 'c', 'f')))
Traceback (most recent call last):
  File "", line 1, in 
    dict(((1,2, 3), ('a', 'c', 'f')))
ValueError: dictionary update sequence element #0 has length 3; 2 is required
  • 集合不能转为dict,因为集合不支持哈希
  • 两个列表,可先通过zip()和list()方法组合成一个等长二级列表,再通过dict()方法转为一个字典
>>> list1 = [1, 2, 3, 4]
>>> list2 = ['a', 'c', 'f', 'g']
>>> list(zip(list1, list2))
[(1, 'a'), (2, 'c'), (3, 'f'), (4, 'g')]
>>> dict(list(zip(list1, list2)))
{1: 'a', 2: 'c', 3: 'f', 4: 'g'}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/989730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号