Python 各类型数据元素相互转化方法及实例

  • 数字类型:整型(int),浮点型(float),复数类型(complex)
  • 字符串类型:str
  • 逻辑类型:bool True False
  • 列表类型:list [] 有序 可修改
  • 元组类型:turple () 有序 不可修改
  • 集合类型:set {} 无序 不重复
  • 字典类型:dict {key:value} 无序

一.int整数类型

1.int—>float(整形—>浮点数)

方法:float()

a=100
print(type(a))
print(a)
b=float(a)
print(type(b))
print(b)

运行结果:

<class 'int'>
100
<class 'float'>
100.0

2.int—>str(整形—>字符串)

方法:str()

a=100
print(type(a))
b=str(a)
print(type(b))

运行结果:

<class 'int'>
<class 'str'>

3.int—>complex(整形—>复数)

方法:complex()

a=100
print(type(a))
print(a)
b=complex(a)
print(type(b))
print(b)

运行结果:

<class 'int'>
100
<class 'complex'>
(100+0j)

二.float浮点数类型

1.float—>int(浮点数—>整形)

方法:int()

b=float(100)
print(type(b))
print(b)
c=int(b)
print(type(c))
print(c)

运行结果:

<class 'float'>
100.0
<class 'int'>
100

2.float—>complex(浮点数—>复数)

方法:complex()

b=float(100)
print(type(b))
print(b)
c=complex(b)
print(type(c))
print(c)

运行结果:

<class 'float'>
100.0
<class 'complex'>
(100+0j)

3.float—>str(浮点数—>字符串)

方法:str()

b=float(100)
print(type(b))
print(b)
c=str(b)
print(type(c))
print(c)

运行结果:

<class 'float'>
100.0
<class 'str'>
100.0

三.complex复数类型

一般不转化


四.str字符串类型

1.str—>int(字符串—>整形)

方法:int()

a="123"
print(type(a))
print(a)
b=int(a)
print(type(b))
print(b)

运行结果:

<class 'str'>
123
<class 'int'>
123

2.str—>float(字符串—>浮点数)

方法:float()

a="123.456"
print(type(a))
print(a)
b=float(a)
print(type(b))
print(b)

运行结果:

<class 'str'>
123.456
<class 'float'>
123.456

3.str—>list(字符串—>列表)

1.方法:split()(如果想整体变成一个元素,split()内填任何字符串中没有的分割单位都可以)

str1 = 'hello world'
print(type(str1))
print(str1)
str2=str1.split(' ')
#注意split()内不能为空
print(type(str2))
print(str2)

运行结果:

<class 'str'>
hello world
<class 'list'>
['hello', 'world']

2.方法:list()(将字符串str中的每个字符都分割出来成为列表的一个元素)

str1 = 'hello world'
print(type(str1))
print(str1)
str2=list(str1)
print(type(str2))
print(str2)

运行结果:

<class 'str'>
hello world
<class 'list'>
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

五.list列表类型

1.list—>str(列表—>字符串)

1.方法:直接拼接

list1 = ['hello','world']
print(type(list1))
print(list1)
list2 = list1[0] + list1[1]
print(type(list2))
print(list2)

运行结果:

<class 'list'>
['hello', 'world']
<class 'str'>
helloworld

2.方法:join()

list1 = ['hello','world']
print(type(list1))
print(list1)
# 引号中的内容就是连接各个字符串的字符
list2="".join(list1)
list3=" ".join(list1)
print(list2)
print(list3)
print(type(list2))
print(type(list3))

运行结果:

<class 'list'>
['hello', 'world']
helloworld
hello world
<class 'str'>
<class 'str'>

3.不同类型列表元素转字符串:方法:map()函数 str()函数

lt=["1",2,"a"]
print(type(lt))
print(lt)
ls1=map(str,lt)
ls2="".join(ls1)
print(type(ls2))
print(ls2)

运行结果:

<class 'list'>
['1', 2, 'a']
<class 'str'>
12a

2.list—>turple(列表—>元组)

方法:turple()

list = [1, 2, 3, 4, 5]
print(type(list))
print(list)
tuple = tuple(list)
print(type(tuple))
print(tuple)

运行结果:

<class 'list'>
[1, 2, 3, 4, 5]
<class 'tuple'>
(1, 2, 3, 4, 5)

3.list—>dict(列表—>字典)

方法:zip()

list1 = ['k1','k2','k3']
list2 = [1,2,3]
dt=dict(zip(list1,list2))
print(type(dt))
print(dt)

运行结果:

<class 'dict'>
{'k1': 1, 'k2': 2, 'k3': 3}

4.list—>set(列表—>集合)(去重)

方法:set()

list = [1,2,3,3,3,4]
set1=set(list)
print(type(set1))
print(set1)

运行结果:

<class 'set'>
{1, 2, 3, 4}

六.turple元组类型

1.turple—>str(元组—>字符串)

方法:str()

turple = (1, 2, 3, 4, 5)
print(type(turple))
print(turple)
print(turple[0])
str = str(turple)
print(type(str))
print(str)
print(str[0])

运行结果:

<class 'tuple'>
(1, 2, 3, 4, 5)
1
<class 'str'>
(1, 2, 3, 4, 5)
(

2.turple—>list(元组—>列表)

方法:list()

turple = (1, 2, 3, 4, 5)
print(type(turple))
print(turple)
list = list(turple)
print(type(list))
print(list)

运行结果:

<class 'tuple'>
(1, 2, 3, 4, 5)
<class 'list'>
[1, 2, 3, 4, 5]

七.set集合类型

1.set—>str(集合—>字符串)

方法:join()

set = {'a','b','c'}
print(type(set))
print(set)
str=''.join(set)
print(type(str))
print(str)

运行结果:

<class 'set'>
{'b', 'a', 'c'}
<class 'str'>
bac

2.set—>list(集合—>列表)

方法:list()

set = {'a','b','c'}
print(type(set))
print(set)
list=list(set)
print(type(list))
print(list)

运行结果:

<class 'set'>
{'b', 'a', 'c'}
<class 'list'>
['b', 'a', 'c']

八.dict字典类型

1.dict—>list(字典—>列表)

方法:items()

aa={"name":"wy","age":12,"amount":27}
print(type(aa))
print(aa)
bb=[(x,y) for x,y in aa.items()]
print(type(bb))
print(bb)

运行结果:

<class 'dict'>
{'name': 'wy', 'age': 12, 'amount': 27}
<class 'list'>
[('name', 'wy'), ('age', 12), ('amount', 27)]

2.dict—>str(字典—>字符串)(同上先转列表再转字符串)

1.方法:__getitem__()

aa={"name":"wy","age":12,"amount":27}
print(type(aa))
print(aa)
bb=[(x,y) for x,y in aa.items()]
print(type(bb))
print(bb)
print(type(bb[0].__getitem__(0)))
print(bb[0].__getitem__(0))
print(bb[0].__getitem__(1))

运行结果:

<class 'dict'>
{'name': 'wy', 'age': 12, 'amount': 27}
<class 'list'>
[('name', 'wy'), ('age', 12), ('amount', 27)]
<class 'str'>
name
wy

2.方法:json.dumps()

import json
//需要导入json库
dict_1 = {'name':'linux','age':18}
dict_string = json.dumps(dict_1)
print(type(dict_string))
print(dict_string)
print(dict_string[0])
print(dict_string[7])

运行结果:

<class 'str'>
{"name": "linux", "age": 18}
{
:

2.方法:str()(强制转换)

dict_2 = {'name':'linux','age':18}
dict_string = str(dict_2)
print(type(dict_string))
print(dict_string)
print(dict_string[0])
print(dict_string[7])

运行结果:

<class 'str'>
{'name': 'linux', 'age': 18}
{
:
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偶尔躲躲乌云_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值