首頁  >  文章  >  後端開發  >  python-set集合類別方法的詳細介紹

python-set集合類別方法的詳細介紹

高洛峰
高洛峰原創
2017-03-20 11:11:451271瀏覽

  s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])回傳的是{11,22,33,44, 'Tom','tony',77,2.5}(注意:返回的並不是一個字典,只是告訴你這個集合中含有這些元素,所以每次返回的結果元素的順序可能是不一樣的)

  s2=set([11,22,33,44,'Tom','tony',11,55,66,])回傳的是{11,22,33,44,'Tom','tony ',55,66}(注意:回傳的並不是字典,只是告訴你這個集合含有這些元素,所以每次回傳的結果元素的順序可能是不一樣的)

add(. ..)
  Add an element to a set.
  This has no effect if the element is already present.(如果元素已存在,則添加無效。就是說明set是一個沒有重複元素的集合。)

  例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

#       s1.add('jimi##       s1.add('jimi')

     print(s1)

  結果:{33, 'tony', 2.5, 'jimi', 11, 44, 77, 22, 'Tom'}(注意:set是一個無序的集合,可能每次運行的結果的順序不一樣。 sets as a

new

set.(求主差集,並會產生一個新的集合)
  (i.e. all elements that are in this set but not the others.) (註:s1.difference(s2)表示的是找出s1中有而s2中沒有的元素,並產生一個新的集合。s2.difference(s1)表示的是找出s2中有而s1中沒有的元素,並產生一個新的集合。      s2=set([11,22,33,44,'Tom','tony',11,55,66,])
     s3=s1.er##    iffdiffence#s1.diffd#s1. ##     s4=s2.difference(s1)

     print(s3)

     print(s4)

     print(s4)

     print#s4)

    { 66, 55}

difference_update(...)

  Remove all elements of another set from this set.(從這個集合中移除另一組的所有元素。注意:s1.difference_update(s2)表示的是移除s1中兩者共有的元素,保留不共有的元素,是s1的修改而不是產生一個新列表。    s2.difference_update(s1)表示的是移除s2中兩者共有的元素,保留不共有的元素,是對s2的修改而不是產生一個新列表。 )

  例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

     s2=set([1111 ,22,33,44,'Tom','tony',11,55,66,])

     s1.difference_update(s2)

  s1.difference_update(s2)

      #  結果:{2.5, 77}

discard(...)

  Remove an element from a set if it is a member.(移除集合中的成員(元素))

  If  If the element is not a member, do nothing.(如果集合中沒有這個成員,則不進行任何操作,也不會報錯,這與Remove有區別。)

  例如:s1=set([11 ,22,33,44,'Tom','tony',11,77,2.5,])

     s1.discard(2.5)


  結果:{33, 'Tom', 11, 44, 77, 'tony', 22}

intersection(...)

  Return the intersection of two sets as a new set.(產生兩個集合的交集,並產生一個新的列表)

  (i.e. all elements that are in both sets.)

  例如:s1=set([11,22,33,44,'Tom','tony',11,77, 2.5,])

     s2=set([11,22,33,44,'Tom','tony',11,55,66,])


        s5. (s2)

     s6=s2.intersection(s1)

     print(s5)

    print(s5)

  ', 11, 44, 22, 'tony'}

     {33, 11, 44, 'tony', 'Tom', 22}

intersection_update(...)

Update a set with the intersection of itself and another.(功能和intersection(...)是一樣的,但是s1.intersection(s2)在執行的時候是對原來的集合s1的修改,並不會產生一個新的集合)

  例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

     s2=set()

     s2=set( [11,22,33,44,'Tom','tony',11,55,66,])

     s1.intersection(s2)

   s1.intersection(s2)

     s2.

##     print(s1)

     print(s2)

  結果:{33, 'Tom', 11, 44,##  結果:{33, 'Tom', 11, 44, 22, '#y'}# {33, 11, 44, 'tony', 'Tom', 22}

isdisjoint(...)
  Return True if two sets have a null intersection.(判斷兩個集合是否有交集,如果有則回傳False,如果沒有則回傳True)

  例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

     s2=set([100,##     s2=set([100,##     s2=set([100,500,500,500 ,])

     print(s1.isdisjoint(s2))

  結果:True

issubset(...)
 otherReport whether

issubset(...)

 other set whether an an contains an set. (判斷集合中的所有元素是否在另一個集合中,s1.issubset(s2) 表示的是s1中的每個元素都在s2中,注意與s1.issuperset(s2)的區別。 )

  例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

     s2=set([11,##     s2=set([11,#2,33 ,44,'Tom','tony',11,])

     s3=([11,22,33,44,'Tom','tony',11,55,66])

     s1.issubset(s2)

     s1.issubset(s3)

     print(s3)

     print(s3)#sub_

     print(s1.is#12)#12 月。 set(s3 ))

  結果:True

     False

issuperset(...)

  Report whether this set contains another set.

  Report whether this set contains another set.(判斷一個集合中的所有元素是否在另一個集合中,s1.issubset(s2)表示的是s2中的每個元素都在s1中,注意與s1.issubset(s2)的區別。 [11,22,33,44,'Tom','tony',11,77,2.5,])

     s2=set([11,22,33,44,'Tom','tony ',11,])

     s3=([11,22,33,44,'Tom','tony',11,55,66])

      s124)。 )

     s1.issuperset(s3)

     print(s1.issuperset(s2))

    :True

     False


pop(...)
  Remove and return an arbitrary set element.(隨機刪除一個元素)#」set#   Raises KeyError if set#.若集合為空,執行pop時會提醒KeyError)

  例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

     s1.pop()

#  結果:{33, 'Tom', 2.5, 11, 44, 77, 22}

#remove(...)
Remove an element from a set; it must be a member.(移除集合中的成員(元素),這個元素必須在這個集合中)
  If the element is not a member, raise a KeyError.(如果集合中沒有這個成員,則會提示keyError,這與discard有區別。 )

  例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

     s1.remove(2.#5)

     s1.remove('jimi')

  結果:{33, 'Tom', 'tony', 11, 44, 77, 22}

 Key E jimi'

symmetric_difference(...)
  Return the symmetric difference of two sets as a new set.(產生一個新的列表,這個列表是兩個列表中不重複元素的集合,s1 .symmetric_difference(s2)表示s1中有s2中沒有的元素和s2中有s1中沒有的元素的集合)

  (i.e. all elements that are in exactly one of the sets.)

  例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

     s2=set([11,22,33, 44,'Tom','tony',11,55,66,])

     s4=s2.symmetric_difference(s1)

     {2.5, 66, 77, 55}

symmetric_difference_update(...)

  Update a set with the symmetric difference of itself and another.(對一個列表進行修改,兩個列表中不重複元素的集合,s1.symmetric_difference(s2)表示s1中有s2中沒有的元素和s2中有s1中沒有的元素的集合)


  例如:s1=set([11,22,33,44 ,'Tom','tony',11,77,2.5,])

     s2=set([11,22,33,44,'Tom','tony',11,55,66, ])

     s1.symmetric_difference_update(s2)

     print(s1)

     print(s1)

  ..)

  Return the union of sets as a new set.(產生一個新集合,改列表是兩個列表的所以成員(元素)的集合,s1.union(s2)表示的是包含s1,s2所有元素的新集合)

  (i.e. all elements that are in either set.)

  例如:s1=set([11,22,33,44,'Tom','tony',111 ,77,2.5,])

     s2=set([11,22,33,44,'Tom','tony',11,55,66,])

[               s1.union(s2)

     print(s3)

  結果:{33, 2.5, 66, 'Tom', 11, 44, 77, 55, 22, 'tony'Tom', 11, 44, 77, 55, 22, 'tony'Tom', 11, 44, 77, 55, 22, 'tony'Tom', 11, 44, 77, 55, 22, 'tony'Tom',}#}#}#}#}#}#}#}#}#}#}#}#}#}

##update(...)

  Update a set with the union of itself and others.(將一個集合更新到另一個集合,s1.update(s2)表示將s2中的所有元素放到在s1中,完成s1的更新修改)

  例如:s1=set([100,50,])

      s2=set([11,22,33,44,'Tom ','tony',11,55,66,])

     s1.update(s2)

     print(s1)

  結果:{'tony', 33, 66, 1

  結果:{'tony', 33, 66, 100, ' 55}###

以上是python-set集合類別方法的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn