编程语言
首页 > 编程语言> > Python容器数据类型之间的转换

Python容器数据类型之间的转换

作者:互联网

强制转换成字符串

str()

将列表(list)转为字符串

var1 = [1, 2, 3, 4]
var2 = str(var1)
print(type(var2))  # <class 'str'>
print(repr(var2))  # '[1, 2, 3, 4]'

注:repr() 函数将对象转化为供解释器读取的形式,返回一个对象的字符串格式。

将元组(tuple)转为字符串

var1 = (1, 2, 3, 4)
var2 = str(var1)
print(type(var2))  # <class 'str'>
print(repr(var2))  # '(1, 2, 3, 4)'s

将集合(set)转为字符串

var1 = {1, 2, 3}
var2 = str(var1)
print(type(var2))  # <class 'str'>
print(repr(var2))  # '{1, 2, 3}'

将字典(dict)转为字符串

var1 = {"name": "tom", "age":18}
var2 = str(var1)
print(type(var2))  # <class 'str'>
print(repr(var2))  # "{'name': 'tom', 'age': 18}"

将数字(number)转为字符串

var1 = 100
var2 = str(var1)
print(type(var2))  # <class 'str'>
print(repr(var2))  # '100'

将布尔(bool)转为字符串

var1 = True
var2 = str(var1)
print(type(var2))  # <class 'str'>
print(repr(var2))  # 'True'

总结:str()函数可以将所有的数据类型转为字符串,在当前的数据类型两边加上引号。

强制转换成列表

list()

将字符串(str)转为列表

var1 = "Hello"
var2 = list(var1)
print(var2)  # ['H', 'e', 'l', 'l', 'o']
print(type(var2))  # <class 'list'>

将元组(tuple)转为列表

var1 = (2, 3, 4)
var2 = list(var1)
print(var2)  # [2, 3, 4]
print(type(var2))  # <class 'list'>

将集合(set)转为列表

var1 = {"a", "b", "c"}
var2 = list(var1)
print(var2)  # ['a', 'b', 'c']
print(type(var2))  # <class 'list'>

将字典(dict)转为列表

var1 = {"aa": 1, "bb": 2, "cc": 3}
var2 = list(var1)
# 将字典中的键(key)转为列表,不包括值(value)
print(var2)  # ['aa', 'bb', 'cc']
print(type(var2))  # <class 'list'>

强制转换成元组

tuple()

将字符串(str)转为元组

var1 = "hello"
var2 = tuple(var1)
print(type(var2))  # <class 'tuple'>
print(repr(var2))  # ('h', 'e', 'l', 'l', 'o')

将列表(list)转为元组

var1 = ["a", "b", "c"]
var2 = tuple(var1)
print(type(var2))  # <class 'tuple'>
print(repr(var2))  # ('a', 'b', 'c')

将集合(set)转为元组

var1 = {1, 2, 3, 4}
var2 = tuple(var1)
print(type(var2))  # <class 'tuple'>
print(repr(var2))  # (1, 2, 3, 4)

将字典(dict)转为元组

var1 = {"a": 1, "b": 2, "c": 3}
var2 = tuple(var1)
print(type(var2))  # <class 'tuple'>
# 将字典中的键(key)转为元组,不包括值(value)
print(repr(var2))  # ('a', 'b', 'c')

强制转换成集合

set()

将字符串(str)转为集合

var1 = "你好啊,啊 "
var2 = set(var1)
print(type(var2))  # <class 'set'>
print(repr(var2))  # {'你', ',', '好', ' ', '啊'}

将列表(list)转为集合

var1 = [1, 2, 3, 1, 4, 2]
var2 = set(var1)
print(type(var2))  # <class 'set'>
print(repr(var2))  # {1, 2, 3, 4}

将元组转为(tuple)集合

var1 = (1, 2, 3, 1)
var2 = set(var1)
print(type(var2))  # <class 'set'>
print(repr(var2))  # {1, 2, 3}

将字典(dict)转为集合

var1 = {"name": "tom", "age": 20}
var2 = set(var1)
print(type(var2))  # <class 'set'>
# 将字典中的键(key)转为集合,不包括值(value)
print(repr(var2))  # {'name', 'age'}

强制转换成字典

要求: 必须是等长的二级容器,并且里面的元素个数只能是2个

dict()

list1 = [["a", 2], ["b", 3]]
dict1 = dict(list1)
print(dict1)  # {'a': 2, 'b': 3}
print(type(dict1))  # <class 'dict'>
tup1 = (("a", 2), ("b", 3))
dict1 = dict(tup1)
print(dict1)  # {'a': 2, 'b': 3}
print(type(dict1))  # <class 'dict'>
set1 = {("a", 2), ("b", 3)}
dict1 = dict(set1)
print(dict1)  # {'a': 2, 'b': 3}
print(type(dict1))  # <class 'dict'>

例外情况:

标签:容器,var1,var2,Python,数据类型,repr,元组,print,type
来源: https://www.cnblogs.com/bubbleboom/p/14725914.html