系统
Windows 10 专业工作站版22H2
软件
python-3.9.6-amd64.exe
拓展库:
jupyter==1.0.0notebook==7.0.61.元组嵌套1.1 元组嵌套元组
try: tuple0 = ((1,2,3),(1,2,3),(1,2,3)) print(tuple0,type(tuple0))except: print('error')
((1, 2, 3), (1, 2, 3), (1, 2, 3))
1.2 元组嵌套列表
try: tuple0 = ([1,2,3],[1,2,3],[1,2,3]) print(tuple0,type(tuple0))except: print('error')
([1, 2, 3], [1, 2, 3], [1, 2, 3])
1.3 元组嵌套集合
try: tuple0 = ({1,2,3},{1,2,3},{1,2,3}) print(tuple0,type(tuple0))except: print('error')
({1, 2, 3}, {1, 2, 3}, {1, 2, 3})
1.4 元组嵌套字典
try: tuple0 = ({'a':1,'b':2,'c':3},{'a':1,'b':2,'c':3},{'a':1,'b':2,'c':3}) print(tuple0,type(tuple0))except: print('error')
({‘a’: 1, ‘b’: 2, ‘c’: 3}, {‘a’: 1, ‘b’: 2, ‘c’: 3}, {‘a’: 1, ‘b’: 2, ‘c’: 3})
2 列表嵌套2.1 列表嵌套元组
try: list0 = [(1,2,3),(1,2,3),(1,2,3)] print(list0,type(list0))except: print('error')
[(1, 2, 3), (1, 2, 3), (1, 2, 3)]
2.2 列表嵌套列表
try: list0 = [[1,2,3],[1,2,3],[1,2,3]] print(list0,type(list0))except: print('error')
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
2.3 列表嵌套集合
try: list0 = [{1,2,3},{1,2,3},{1,2,3}] print(list0,type(list0))except: print('error')
[{1, 2, 3}, {1, 2, 3}, {1, 2, 3}]
2.4 列表嵌套字典
try: list0 = [{'a':1,'b':2,'c':3},{'a':1,'b':2,'c':3},{'a':1,'b':2,'c':3}] print(list0,type(list0))except: print('error')
[{‘a’: 1, ‘b’: 2, ‘c’: 3}, {‘a’: 1, ‘b’: 2, ‘c’: 3}, {‘a’: 1, ‘b’: 2, ‘c’: 3}]
3 集合嵌套3.1 集合嵌套元组
try: set0 = {(1,2,3),(1,2,3),(1,2,3)} print(set0,type(set0))except: print('error')
{(1, 2, 3)}
3.2 集合嵌套列表
try: set0 = {[1,2,3],[1,2,3],[1,2,3]} print(set0,type(set0))except: print('error')
error
3.3 集合嵌套集合
try: set0 = {{1,2,3},{1,2,3},{1,2,3}} print(set0,type(set0))except: print('error')
error
3.4 集合嵌套字典
try: set0 = {{'a':1,'b':2,'c':3},{'a':1,'b':2,'c':3},{'a':1,'b':2,'c':3}} print(set0,type(set0))except: print('error')
error
4 字典嵌套4.1 字典的键嵌套4.1.1 字典的键嵌套元组
try: dict0 = {(1,2,3):(1,2,3),(1,2,3):(2,3,4),(1,2,3):(3,4,5)} print(dict0,type(dict0)) dict0 = {(1,2,3):(3,4,5),(1,2,3):(2,3,4),(2,3,4):(1,2,3)} print(dict0,type(dict0))except: print('error')
{(1, 2, 3): (3, 4, 5)}
{(1, 2, 3): (2, 3, 4), (2, 3, 4): (1, 2, 3)}
4.1.2 字典的键嵌套列表
try: dict0 = {[1,2,3]:(1,2,3),[1,2,3]:(1,2,3),[1,2,3]:(1,2,3)} print(dict0,type(dict0))except: print('error')
error
4.1.3 字典的键嵌套集合
try: dict0 = {{1,2,3}:(1,2,3),{1,2,3}:(1,2,3),{1,2,3}:(1,2,3)} print(dict0,type(dict0))except: print('error')
error
4.1.4 字典的键嵌套字典
try: dict0 = { {'a': 1, 'b': 2, 'c': 3}:(1,2,3), {'a': 1, 'b': 2, 'c': 3}:(1,2,3), {'a': 1, 'b': 2, 'c': 3}:(1,2,3)} print(dict0,type(dict0))except: print('error')
error
4.2 字典的值嵌套4.2.1 字典的值嵌套元组
try: dict0 = {'a':(1,2,3),'b':(1,2,3),'c':(1,2,3)} print(dict0,type(dict0))except: print('error')
{‘a’: (1, 2, 3), ‘b’: (1, 2, 3), ‘c’: (1, 2, 3)}
4.2.2 字典的值嵌套列表
try: dict0 = {'a':[1,2,3],'b':[1,2,3],'c':[1,2,3]} print(dict0,type(dict0))except: print('error')
{‘a’: [1, 2, 3], ‘b’: [1, 2, 3], ‘c’: [1, 2, 3]}
4.2.3 字典的值嵌套集合
try: dict0 = {'a':{1,2,3},'b':{1,2,3},'c':{1,2,3}} print(dict0,type(dict0))except: print('error')
{‘a’: {1, 2, 3}, ‘b’: {1, 2, 3}, ‘c’: {1, 2, 3}}
4.2.4 字典的值嵌套字典
try: dict0 = { 'a':{'a':1,'b':2,'c':3}, 'b':{'a':1,'b':2,'c':3}, 'c':{'a':1,'b':2,'c':3}} print(dict0,type(dict0))except: print('error')
{‘a’: {‘a’: 1, ‘b’: 2, ‘c’: 3}, ‘b’: {‘a’: 1, ‘b’: 2, ‘c’: 3}, ‘c’: {‘a’: 1, ‘b’: 2, ‘c’: 3}}
5 总结5.1 文字总结
1.可变类型数据不能作为集合的元素和字典的键。
2.集合和字典自身的去重特性导致嵌套时的输入值可能不等于输出值。
5.2 表格总结
被嵌套的数据类型 | ||||
---|---|---|---|---|
元组 | 列表 | 集合 | 字典 | |
元组 | 保留输入 | 保留输入 | 保留输入 | 保留输入 |
列表 | 保留输入 | 保留输入 | 保留输入 | 保留输入 |
集合 | 去重输入 | error | error | error |
字典的键 | 去重输入 | error | error | error |
字典的值 | 保留输入 | 保留输入 | 保留输入 | 保留输入 |
元组 | ||||
集合 | 相同元素仅保留一个 | |||
字典的键 | 键相同时,后键值重写前键值。 |