首页 > 编程语言 >python数据类型元组、列表、集合、字典相互嵌套

python数据类型元组、列表、集合、字典相互嵌套

时间:2023-12-11 15:01:01浏览次数:37  
标签:dict0 python 数据类型 except 元组 嵌套 error print type

系统
Windows 10 专业工作站版22H2
软件
python-3.9.6-amd64.exe
拓展库:

jupyter==1.0.0 notebook==7.0.6

1.元组嵌套

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)) <class 'tuple'>

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]) <class 'tuple'>

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}) <class 'tuple'>

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}) <class 'tuple'>

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)] <class 'list'>

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]] <class 'list'>

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}] <class 'list'>

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}] <class 'list'>

3 集合嵌套

3.1 集合嵌套元组

try:
    set0 = {(1,2,3),(1,2,3),(1,2,3)}
    print(set0,type(set0))
except:
    print('error')

{(1, 2, 3)} <class 'set'>

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)} <class 'dict'>
{(1, 2, 3): (2, 3, 4), (2, 3, 4): (1, 2, 3)} <class 'dict'>

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)} <class 'dict'>

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]} <class 'dict'>

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}} <class 'dict'>

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}} <class 'dict'>

5 总结

5.1 文字总结

1.可变类型数据不能作为集合的元素和字典的键。
2.集合和字典自身的去重特性导致嵌套时的输入值可能不等于输出值。

5.2 表格总结

被嵌套的数据类型
元组 列表 集合 字典
元组 保留输入 保留输入 保留输入 保留输入
列表 保留输入 保留输入 保留输入 保留输入
集合 去重输入 error error error
字典的键 去重输入 error error error
字典的值 保留输入 保留输入 保留输入 保留输入
元组
集合 相同元素仅保留一个
字典的键 键相同时,后键值重写前键值。

标签:dict0,python,数据类型,except,元组,嵌套,error,print,type
From: https://www.cnblogs.com/mlcode/p/nest.html

相关文章

  • map(python中的字典)
    //创建一个空的map,键是字符串类型,值是整数类型myMap:=make(map[string]int)//创建有值的map//初始化并赋值myMap:=map[string]int{"apple":1,"banana":2,"orange":3,}//添加修改元素myMap["grape"]=4//添加元素myMa......
  • 【python基础之函数】--- 函数入门
    title:【python基础之函数】---函数入门date:2023-12-0818:50:06updated:2023-12-1114:30:00description:cover:https://home.cnblogs.com/u/dream-ze/函数的基本使用目前为止,借助之前的学习内容,是已经能开发一些功能简单的小程序了但随着程序功能......
  • 在自动化测试时,Python常用的几个加密算法,你有用到吗
    本文分享自华为云社区《『加密算法』|自动化测试时基于Python常用的几个加密算法实现,你有用到吗?》,作者:虫无涯。写在前边这几天做自动化测试,遇到一个问题,那就是接口的请求的密码是加密的;产品的要求是不能使用其他特殊手段,他给提供加密算法,需要在接口请求的时候,使用加密算法处......
  • Python语言合并列表元素常用的方法!
    众所周知,列表是Python中常见的数据类型,它可以存储多个元素。但由于某种需求,我们有时候需要将多个元素进行合并,那么Python语言如何合并列表中的元素?以下是常用方法介绍。1、使用+运算符在Python中,可以使用+运算符将两个列表的元素合并成一个新的列表。例如,假设有两个列......
  • python 怎么组织代码?
    参考:https://www.liaoxuefeng.com/wiki/1016959663602400/10174541450141761.为什么不能把代码写到一个.py中?实际开发中,我们不可能把所有的代码都写到一个.py文件中,看起来太累了,并且难以修改,修改后难免要考虑会不会影响别的。解决方法:把很多函数分组,分别放到不同的文件里,......
  • [Python急救站]文件管理工具
    对于一个程序员,有时候文件太多,忘记放哪里了,那有没有一个可以帮你定位到文件的文件管理工具呢,抱着这样的想法,我做了以下这个代码,可以快速定位找到文件所在位置。importosimporttkinterastkimporttimeimportsubprocess#函数用于搜索文件defsearch_files():file......
  • Linux学习36- python3.9出现ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+
    遇到问题python3.9上安装requests库,requests包引入了urllib3,而新版本v2.x的urllib3需要OpenSSL1.1.1+以上版本所以就出现了报错File"/root/python39/lib/python3.9/site-packages/_pytest/assertion/rewrite.py",line186,inexec_moduleexec(co,module.__dict__......
  • Linux学习35- python3.9出现ModuleNotFoundError: No module named '_ctypes'的解决
    遇到问题pip安装第三方库的时候报错ModuleNotFoundError:Nomodulenamed'_ctypes'File"/usr/local/python3/lib/python3.9/ctypes/__init__.py",line7,in<module>from_ctypesimportUnion,Structure,ArrayModuleNotFoundError:Nomodulen......
  • 10行Python代码能做出哪些酷炫的事情?
    Python凭借其简洁的代码,赢得了许多开发者的喜爱。因此也就促使了更多开发者用Python开发新的模块,从而形成良性循环,Python可以凭借更加简短的代码实现许多有趣的操作。下面我们来看看,我们用不超过10行代码能实现些什么有趣的功能。一、生成二维码二维码又称二维条码,常见的二维码为QR......
  • spring boot 项目实现调用python工程的方法
    在SpringBoot中调用Python脚本或工程,主要有以下几种方式:1.使用ProcessBuilder或Runtime执行Python脚本这是最直接的方法,使用Java的ProcessBuilder或Runtime.getRuntime().exec()来执行Python脚本。优点:实现简单,无需额外依赖。缺点:处理输出和错误流可能较为繁琐......