• python之集合的创建与使用,遍历,集合常见的操作函数,集合与列表,元组,字典的嵌套


    集合的创建:使用内置函数set()进行转化或者使用{}包括起来的,集合中的元素:无序性,互异性,确定性。
    举例:

    numbers=set(range(0,7))//使用内置函数进行转化
    print(numbers)
    print(type(numbers))
    
    • 1
    • 2
    • 3
    {0, 1, 2, 3, 4, 5, 6}
    <class 'set'>
    
    • 1
    • 2

    互异性:

    fruit={'apple','orange','banana',"apple",'apple','orange','banana',"apple"}
    print(fruit)
    print(type(fruit))
    
    • 1
    • 2
    • 3
    {'apple', 'banana', 'orange'}
    <class 'set'>
    
    • 1
    • 2

    无序性:集合中的元素不能通过下标访问。
    举例:

    fruit =set({'apple',9,"axn","dbu",12})
    print(fruit[2])
    
    • 1
    • 2

    在这里插入图片描述

    集合中的操作函数:

    在集合中添加元素:add() 函数
    举例:

    fruit =set({'apple',9,"axn","dbu",12})
    fruit.add("bc")
    print(fruit)
    
    • 1
    • 2
    • 3
    {'bc', 'apple', 9, 12, 'dbu', 'axn'}
    
    • 1

    删除集合中的第一个元素:pop()函数
    举例:

    fruit =set({'apple',9,"axn","dbu",12})
    fruit.pop()
    print(fruit)
    
    • 1
    • 2
    • 3
    {'apple', 9, 12, 'axn'}
    
    • 1

    删除集合中的指定元素:
    1:remove()函数,若该元素不存在则会报错
    举例:

    fruit =set({'apple',9,"axn","dbu",12})
    fruit.remove("banana")
    print(fruit)
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    fruit =set({'apple',9,"axn","dbu",12,"apple"})
    fruit.remove("apple")
    print(fruit)
    
    • 1
    • 2
    • 3
    {'dbu', 'axn', 9, 12}
    
    • 1

    2:discard()函数,若指定元素不存在不会报错。
    举例:

    fruit =set({'apple',9,"axn","dbu",12,"apple"})
    fruit.discard("banana")
    print(fruit)
    
    • 1
    • 2
    • 3
    {'dbu', 'apple', 9, 'axn', 12}
    
    • 1
    fruit =set({'apple',9,"axn","dbu",12,"apple"})
    fruit.discard("apple")
    print(fruit)
    
    • 1
    • 2
    • 3
    {'dbu', 'axn', 9, 12}
    
    • 1

    判断元素是否在集合里面:if in/not in语句
    举例:

    fruit =set({'apple',9,"axn","dbu",12,"apple"})
    if "apple" in fruit:
        print("yes")
    else:
        print("NO")
    if "banana" not in fruit:
        print("YES")
    else:
        print("NO")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    yes
    YES
    
    • 1
    • 2

    集合的遍历:for循环

    fruit =set({'apple',9,"axn","dbu",12,"apple"})
    for i in fruit:
        print(i,end=' ')
    
    • 1
    • 2
    • 3
    axn 9 apple 12 dbu 
    
    • 1

    集合元素个数的计算:len()函数
    举例:

    fruit =set({'apple',9,"axn","dbu",12,"apple"})
    print(len(fruit))
    
    • 1
    • 2
    5//注意集合元素的唯一性特征
    
    • 1

    集合与字典,列表,元组的嵌套:

    集合与字典:

    s1=set({"name":"jason","age":19,"地址":"北京市"})
    print(s1)
    print(type(s1))
    
    • 1
    • 2
    • 3
    {'地址', 'name', 'age'}//只输出键名
    <class 'set'>
    
    • 1
    • 2

    集合与元组:
    举例:

    s1={("name","jason","age",19,"地址","北京市"),12,34,0}
    print(s1)
    print(type(s1))
    
    • 1
    • 2
    • 3
    {0, 34, ('name', 'jason', 'age', 19, '地址', '北京市'), 12}
    <class 'set'>
    
    • 1
    • 2

    使用内置函数进行转化:

    s1=set({"name","jason","age",19,"地址","北京市"})
    print(s1)
    print(type(s1))
    
    • 1
    • 2
    • 3
    {'age', 'jason', 19, '地址', '北京市', 'name'}
    <class 'set'>
    
    • 1
    • 2

    集合与列表:
    举例:

    s2=set(["name","jason","age",19,"地址","北京市"])
    print(s2)
    print(type(s2))
    
    • 1
    • 2
    • 3
    {'北京市', 'age', 'jason', 19, 'name', '地址'}
    <class 'set'>
    
    • 1
    • 2
  • 相关阅读:
    Oracle数据库简介
    基于ssm+html的小区物业管理系统
    【Vue】学习笔记-Vue中的Ajax配置代理
    数组与链表
    WPF 用户控件依赖注入赋值
    Mybatis面试题(三十三道)
    SQL面试题练习 —— 查询每个产品每年总销售额
    Linux Shell bash脚本中的 $! $$ $- $0 $1~$n ${n} $# $@ $* $?
    【Visual Studio Code----Shader编程环境配置】
    Oracle数据库设置归档模式(超级简单)
  • 原文地址:https://blog.csdn.net/m0_64365419/article/details/125420103