zoukankan      html  css  js  c++  java
  • Python集合的常用方法

    注释很详细

    collection={1,2,"apple","orange","cat"}
    # 查看一个数是否在集合中  in
    print("Tom 是否在集合中:","Tome" in collection)
    print("apple 是否在集合中:","apple" in collection)
    
    # 集合初始化时用set()   pets={}是代表初始化了一个字典集
    pets=set()
    
    # 集合添加元素用add()方法,删除元素用discard()
    pets.add("dog")
    pets.add("cat")
    pets.add("bird")
    pets.add("zebra")
    print(pets)
    pets.discard("zebra")
    print(pets)
    
    # 集合的 与  或  非
    print("并集:",pets | collection )
    print("交集:",pets & collection)
    print("异或:",pets - collection)
    # 将一个字典集的keys转为集合,用set(字典集名)
    dict1={"cat":1,"dog":2,"bird":3}
    print(set(dict1))
    # 向一个集合中添加另一个集合用update()方法
    collection.update([12,13,14])
    print(collection)
    for i in set("apple"):
        print(i,end=",")
    print()
    for j in "apple":
        print(j,end=",")

    输出结果”

    E:Program_FilesPythonAllworkspaceMyLearnProjectvenvScriptspython.exe E:/Program_Files/PythonAll/workspace/MyLearnProject/pythonCode/collection/collectionFunc.py
    Tom 是否在集合中: False
    apple 是否在集合中: True
    {'dog', 'cat', 'bird', 'zebra'}
    {'dog', 'cat', 'bird'}
    并集: {1, 2, 'bird', 'apple', 'orange', 'dog', 'cat'}
    交集: {'cat'}
    异或: {'dog', 'bird'}
    {'dog', 'cat', 'bird'}
    {1, 2, 12, 13, 14, 'apple', 'cat', 'orange'}
    l,p,e,a,
    a,p,p,l,e,
    Process finished with exit code 0
  • 相关阅读:
    复利计算- 结对
    《构建之法》第4章读后感
    复利计算--单元测试
    实验一 命令解释程序的编写实验
    Scrum 项目准备4.0
    Scrum 项目准备3.0
    scrum 项目准备2.0
    【操作系统】实验三 进程调度模拟程序
    scrum 项目准备1.0
    Scrum团队成立
  • 原文地址:https://www.cnblogs.com/lyxcode/p/10919036.html
Copyright © 2011-2022 走看看