非容器类型数据不能转换为除str外的其他容器类型数据
str可以转换为list、tuple、set,每个字符为一个元素,但不能转换为dict
list、tuple、set、dict之间可以互相转换
dict转换为list、tuple、set时,只保留键,每个键为一个元素
A = '&&&' B = [1, 2, 'a', 'b'] C = (3, 4, 'c', 'd') D = {5, 6, 'e', 'f'} E = {7: 'g', '8': 'h'} # list print(list(A),type(list(A))) print(list(C),type(list(C))) print(list(E),type(list(E))) # tuple print(tuple(A),type(tuple(A))) print(tuple(D),type(tuple(D))) print(tuple(E),type(tuple(E))) # set print(set(A),type(set(A))) print(set(B),type(set(B))) print(set(E),type(set(E))) # dict # str不能转换为dict # list、tuple、set转换为dict时,必须是二级容器,且每个元素中只有两个二级元素 F = [(1,['a','A']),[2,{'b':'B'}],{3:'c',4:'d'},{('e','E'),5}] G = ((1,{'a','A'}),[2,{'b':'B'}],{3:'c',4:'d'},{5,('e','E')}) H = {(6,'f'),(7,'g')} print(dict(F),type(dict(F))) print(dict(G),type(dict(G))) print(dict(H),type(dict(H))) ['&', '&', '&'] <class 'list'> [3, 4, 'c', 'd'] <class 'list'> [7, '8'] <class 'list'> ------------------------------ ('&', '&', '&') <class 'tuple'> ('f', 5, 6, 'e') <class 'tuple'> (7, '8') <class 'tuple'> ------------------------------ {'&'} <class 'set'> {'b', 1, 2, 'a'} <class 'set'> {'8', 7} <class 'set'> ------------------------------ {1: ['a', 'A'], 2: {'b': 'B'}, 3: 4, ('e', 'E'): 5} <class 'dict'> {1: {'a', 'A'}, 2: {'b': 'B'}, 3: 4, ('e', 'E'): 5} <class 'dict'> {7: 'g', 6: 'f'} <class 'dict'>