日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

winform list集合怎么 in过滤_python3基础04字典(dict)和集合(set)

發布時間:2025/3/19 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 winform list集合怎么 in过滤_python3基础04字典(dict)和集合(set) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

"pythonic生物人"的第37篇分享。


原創不易,點個“贊“或"在看"鼓勵下唄

摘要

本節梳理python中另外兩種容器字典(dict)和集合(set)的基本用法。

目錄

1、字典
  • 字典創建

  • 字典使用

返回字典中元素個數判斷字典是否存在某個鍵取出鍵對應的值修改鍵對應的值刪除字典的某個鍵值對返回某個鍵對應的值,不存在設置默認值替代刪除鍵對應的值取出字典中所有鍵值對取出字典中所有鍵返回字典所有鍵組成的列表取出字典中所有值取出字典中的值并將刪除刪除字典中所有的鍵值對字典淺復制

2、集合(set)

  • 集合創建

  • 集合使用

兩個集合求并集:使用符號&或者union函數兩個集合求差集:使用符號-或者difference函數求兩個集合中不同時存在的元素:使用符號^或者symmetric_difference函數求集合元素個數:len(set)成員資格判斷a集合和b集合是否有共同元素(相交):使用isdisjoint判斷a集合是否屬于或等于b集合:使用<=符號,判斷b集合是否包含a集合:使用>=符號,>符號和issuperset函數元素x添加到a集合中:a.add(x)移除集合a中元素x:a.remove(x)移除集合a中元素x:a.discard(x)任意移除集合a中的一個元素:a.pop()清空集合a元素:a.clear()

正文開始啦


1、字典

字典(dict)是python中的映射容器。字典中存儲鍵(key)值(value)對,通過鍵調用值,鍵具有唯一性,值可以不唯一;每個鍵值對之間使用逗號分隔,鍵與值之間使用頓號分割;列表、集合、字典因為可修改所以不能作為字典的鍵;字符串、數值、元組不可修改可以作為字典的鍵。
  • 字典創建

#{}直接創建In 1: {"jack":"man", "Rose":"womman"}Out1: {'jack': 'man', 'Rose': 'womman'}#dict函數創建In 3: pre_dict =[("Jack","man"), ("Rose", "woman")]In 5: dict(pre_dict) Out5: {'Jack': 'man', 'Rose': 'woman'}#借助zip創建(zip 將可迭代對象組合成元組列表) In 7: dict(zip(["Jack","Rose"], ["man","woman"])) Out7: {'Jack': 'man', 'Rose': 'woman'}In?22:?{('Jack'):"man",?"Rose":"womman"}#元組('Jack')可以作為鍵Out22: {'Jack': 'man', 'Rose': 'womman'}In 23: {['Jack']:"man", "Rose":"womman"}#列表['Jack']不能作為鍵 TypeError Traceback (most recent call last) in ----> 1 {['Jack']:"man", "Rose":"womman"}TypeError: unhashable type: 'list'
  • 字典使用

主要介紹一下常見的字典方法。

In [2]: print(dir(dict))

['class', 'contains', 'delattr', 'delitem', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getitem', 'gt', 'hash', 'init', 'init_subclass', 'iter', 'le', 'len', 'lt', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'setitem', 'sizeof', 'str', 'subclasshook', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

  • 返回字典中元素個數
len(d),返回字典d中鍵值對(python中一個鍵值對稱為一個項)的個數。In [38]: len(a_dict)Out[38]: 2
  • 判斷字典是否存在某個鍵

key in d,存在返回True,不存在返回False。In [49]: "Jack1" in a_dictOut[49]: FalseIn [50]: "Rose" in a_dictOut[50]: True
  • 取出鍵對應的值

d[key],當key不存在d中時,報錯。In [41]: a_dict["Jack"]Out[41]: 'man'In?[42]:?a_dic["Lucy"]#字典中不存在Lucy,報錯。---------------------------------------------------------------------------NameError Traceback (most recent call last) in----> 1 a_dic["Lucy"]NameError: name 'a_dic' is not defined
  • 修改鍵對應的值

d[key] = value。

In [43]: a_dictOut[43]: {'Jack': 'man', 'Rose': 'woman'}In [44]: a_dict["Jack"] = "man1"In [45]: a_dictOut[45]: {'Jack': 'man1', 'Rose': 'woman'}
  • 刪除字典的某個鍵值對

d.pop(key,value),如果key存在,刪除字典d中key/value鍵值對;key不存在,返回指定的value;如果value沒指定,報錯。In [98]: a_dictOut[98]: {'Jack': 'man', 'Rose': 'woman'}In [99]: a_dict.pop("Jack")Out[99]: 'man'In [100]: a_dictOut[100]: {'Rose': 'woman'}In [101]: a_dict.pop("Jack","Not exit!")Out[101]: 'Not exit!'In [102]: a_dictOut[102]: {'Rose': 'woman'}
  • 返回某個鍵對應的值,不存在設置默認值替代

d.get(key,value),當d中存在key時,返回key對應的value;當d中不存在key時,返回指定的value,不指定value返回None。In [56]: a_dictOut[56]: {'Rose': 'woman'}In [58]: a_dict.get("Rose","haha")#Rose存在,返回對應值。Out[58]: 'woman'In [59]: a_dict.get("Lucy","haha")#Lucy不存在,返回指定value "haha"。Out[59]: 'haha'In [60]: print(a_dict.get("Lucy"))#Lucy不存在,返回None。Out[60]: None
  • 刪除鍵對應的值

del d[key],字典不存在key,報錯退出。In [43]: a_dictOut[43]: {'Jack': 'man', 'Rose': 'woman'}In [44]: a_dict["Jack"] = "man1"In [45]: a_dictOut[45]: {'Jack': 'man1', 'Rose': 'woman'}In [46]: del a_dict["Jack"]In [47]: a_dictOut[47]: {'Rose': 'woman'}In [48]: del a_dict["Jack1"]#不存在鍵Jack1,報錯退出。---------------------------------------------------------------------------KeyError Traceback (most recent call last) in----> 1 del a_dict["Jack1"]KeyError: 'Jack1'
  • 取出字典中所有鍵值對

d.items()In [74]: a_dictOut[74]: {'Rose': 'woman', 'Jack': 'man'}In [75]: a_dict.items()#items返回字典鍵值對視圖對象,支持迭代,通過list轉化為列表。Out[75]: dict_items([('Rose', 'woman'), ('Jack', 'man')])In [76]: list(a_dict.items())Out[76]: [('Rose', 'woman'), ('Jack', 'man')]
  • 取出字典中所有鍵

d.keys()In [77]: a_dict.keys()Out[77]: dict_keys(['Rose', 'Jack'])In [78]: list(a_dict.keys())Out[78]: ['Rose', 'Jack']
  • 返回字典所有鍵組成的列表

list(d)In [30]: a_dictOut[30]: {'Jack': 'man', 'Rose': 'woman'}In [31]: list(a_dict)Out[31]: ['Jack', 'Rose']
  • 取出字典中所有值

d.values()In [79]: a_dict.values()Out[79]: dict_values(['woman', 'man'])In [80]: list(a_dict.values())Out[80]: ['woman', 'man']
  • 取出字典中的值并將刪除

d.pop(key),返回key對應的鍵,同時刪除該鍵值對。In [81]: a_dictOut[81]: {'Rose': 'woman', 'Jack': 'man'}In [82]: a_dict.pop("Rose")Out[82]: 'woman'In [83]: a_dictOut[83]: {'Jack': 'man'}
  • 刪除字典中所有的鍵值對

d.clear(),刪除字典中所有的鍵值對,返回空字典{}。In [85]: a_dictOut[85]: {'Jack': 'man'}In [86]: a_dict.clear()In [87]: a_dictOut[87]: {}
  • 字典淺復制

d.copy()In [89]: a_dictOut[89]: {'Jack': 'man'}In [91]: b_dict = a_dict.copy()In [92]: b_dictOut[92]: {'Jack': 'man'}In [93]: b_dict["Jack"] = "man1"In [94]: b_dictOut[94]: {'Jack': 'man1'}In [95]: a_dictOut[95]: {'Jack': 'man'}

2、集合(set)

  • 集合是非序列和映射python容器,集合中元素無序,可以理解為只有鍵的字典(集合中不可能有重復元素);

  • 集合并不記錄元素索引,所以集合不支持索引、切片或其他序列類的操作;

  • python內置了兩種集合類型,set類型可修改,forezenset類型不可變,常見的用途

  • 成員檢測(in);序列容器去除重復項;求交集、并集、差集與對稱差集。
    • 集合創建

    In [116]: {'a', 'b', 'c', 'd', 'e'}#花括號{}包圍創建Out[116]: {'a', 'b', 'c', 'd', 'e'}In [117]: set("abcde")#set函數創建Out[117]: {'a', 'b', 'c', 'd', 'e'}**注意:**空的{}表示字典,而不是集合。

    集合使用

    • 兩個集合求交集:使用&號或者intersection函數。

    In [128]: a_setOut[128]: {'a', 'b', 'c', 'd'}In [129]: b_setOut[129]: {'a', 'b', 'c', 'd', 'e'}In [130]: a_set & b_set #使用&號求交集Out[130]: {'a', 'b', 'c', 'd'}In [131]: a_set.intersection(b_set)#intersection函數求交集Out[131]: {'a', 'b', 'c', 'd'}
    • 兩個集合求并集:使用符號&或者union函數

    In [132]: a_set | b_set#使用符號|Out[132]: {'a', 'b', 'c', 'd', 'e'}In [138]: a_set.union(b_set)#使用union函數Out[138]: {'a', 'b', 'c', 'd', 'e'}
    • 兩個集合求差集:使用符號-或者difference函數

    In [142]: a_set - b_set#使用-符號。返回a_set中存在,而b_set中不存在的一個新集合Out[142]: set()In [143]: b_set - a_set#返回b_set中存在,而a_set中不存在的一個新集合Out[143]: {'e'}In [144]: a_set.difference(b_set)#使用difference函數。返回a_set中存在,而b_set中不存在的一個新集合Out[144]: set()In [145]: b_set.difference(a_set)Out[145]: {'e'}
    • 求兩個集合中不同時存在的元素:使用符號^或者symmetric_difference函數

    In [146]: a_set ^ b_setOut[146]: {'e'}In [147]: a_set.symmetric_difference(b_set)Out[147]: {'e'}
    • 求集合元素個數:len(set)

    In [148]: len(a_set)Out[148]: 4
    • 成員資格

    In [148]: len(a_set)Out[148]: 4In [149]: "a" in a_setOut[149]: TrueIn [150]: "x" in a_setOut[150]: False
    • 判斷a集合和b集合是否有共同元素(相交):使用isdisjoint

    有共同元素返回False,否則返回True。In [151]: a_set.isdisjoint(b_set)Out[151]: FalseIn [152]: b_set.isdisjoint(a_set)Out[152]: FalseIn [153]: a_set.isdisjoint(["x"])Out[153]: True
    • 判斷a集合是否屬于或等于b集合:使用<=符號,

    In [155]: a_set.issubset(b_set)#使用函數issubset,a_set完全屬于b_setOut[155]: TrueIn [156]: a_set <= b_set#使用符號<=Out[156]: True
    • 判斷b集合是否包含a集合:使用>=符號,>符號和issuperset函數

    In [162]: b_set.issuperset(a_set)Out[162]: TrueIn [163]: b_set >= a_setOut[163]: True
    • 元素x添加到a集合中:a.add(x)

    如果x已經存在a中,沒任何效果;否則加入。In [171]: a_setOut[171]: {'a', 'b', 'c', 'd'}In [172]: a_set.add("a")#a已存在無任何效果In [173]: a_set.add("x")#添加xIn [174]: a_setOut[174]: {'a', 'b', 'c', 'd', 'x'}
    • 移除集合a中元素x:a.remove(x)

    如果 x 不存在于a中則報錯。In [175]: a_setOut[175]: {'a', 'b', 'c', 'd', 'x'}In [176]: a_set.remove("y")#y不存在集合a_set中---------------------------------------------------------------------------KeyError Traceback (most recent call last) in----> 1 a_set.remove("y")KeyError: 'y'In [177]: a_set.remove("x")In [178]: a_setOut[178]: {'a', 'b', 'c', 'd'}
    • 移除集合a中元素x:a.discard(x)

    如果元素x 存在于集合a中則將其移除;不存在沒任何效果,與remove有細微差別。In [178]: a_setOut[178]: {'a', 'b', 'c', 'd'}In [179]: a_set.discard("x")#a_set不存在x元素,沒任何效果,區別于remove函數In [180]: a_set.discard("d")In [181]: a_setOut[181]: {'a', 'b', 'c'}
    • 任意移除集合a中的一個元素:a.pop()

    如果集合為空則會報錯。In [183]: a_setOut[183]: {'a', 'b', 'c'}In [184]: a_set.pop()Out[184]: 'a'In [185]: a_set.pop()Out[185]: 'c'In [186]: a_set.pop()Out[186]: 'b'In [187]: a_set.pop()---------------------------------------------------------------------------KeyError Traceback (most recent call last) in----> 1 a_set.pop()KeyError: 'pop from an empty set'In [188]: a_set#集合此時已經為空Out[188]: set()
    • 清空集合a元素:a.clear()

    In [190]: a_setOut[190]: {'a', 'b', 'c', 'd'}In [191]: a_set.clear()In [192]: a_setOut[192]: set()
    同系列文章

    python3基礎01數值和字符串(一)

    python3基礎02數值和字符串(二)

    python3基礎03列表(list)和元組(tuple)

    原創不易"點贊"、"在看"勵下唄

    總結

    以上是生活随笔為你收集整理的winform list集合怎么 in过滤_python3基础04字典(dict)和集合(set)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。