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'>
例外情况:
外层是列表,里层是列表或元组
可以实现但是不推荐使用,可能达不到想要的结果,因为集合是无序的。
list1 = [{"a", 2}, {"b", 3}] dict1 = dict(list1) # 可能的情况 print(dict1) # {'a': 2, 3: 'b'} # {'a': 2, 'b': 3}
外层是列表,元组或集合,里层放字符串
字符串长度只能是2位,不推荐使用。
list1 = ["a1", "b2"] dict1 = dict(list1) print(dict1) # {'a': '1', 'b': '2'} list2 = ["a11", "b22"] # ValueError: dictionary update sequence element #0 has length 3; 2 is required dict2 = dict(list2)