Python set方法總結

來源:互聯網
上載者:User

標籤:python集合   集合方法   python set   

1.在尾部添加一項

    add(...)

    This has no effect if the element is already present

       >>> a = set([1,2,3,4,5])

       >>> a.add(‘shaw‘)

       >>> a

       set([1, 2, 3, 4, 5, ‘shaw‘])

2.清空集合中所有元素

    clear(...)

    Remove all elements from this set

        >>>a = set([1, 2, 3, 4, 5, ‘shaw‘])

        >>>a.clear()

        >>>a

        set([])

3.返回一個新集合,但是新集合中的元素,a中有,b中沒有

    difference(...)

       >>> a = set([1, 2, 3, 4, 5,‘shaw‘])

       >>> b = set([2,3,5,‘sam‘])

       >>> a.difference(b)

       set([1, 4, ‘shaw‘])

4.從set a中刪除,集合a和集合b同時擁有的元素

    difference_update(...)

       >>> a = set([1, 2, 3, 4, 5,‘shaw‘])

       >>> b = set([2,3,5,‘sam‘])

       >>> a.difference_update(b)

       >>> a

       set([1, 4, ‘shaw‘])

       >>> b

       set([2, 3, 5, ‘sam‘])

5.刪除set中指定元素(即使這個元素不存在與這個集合中,不會報錯)

    discard(...)

    Remove an element from a set if it is amember.f the element is not a member, do nothing.

       >>> a = set([1, 2, 3, 4, 5,‘shaw‘])

       >>> a.discard(‘5‘)

       >>> a

       set([1, 2, 3, 4, 5, ‘shaw‘])

       >>> a.discard(‘sam‘)

       >>> a

       set([1, 2, 3, 4, 5, ‘shaw‘])

 

6.返回一個新集合,新集合元素由set a和set b的交集組成

    intersection(...)

    Return the intersection of two or more setsas a new set.

       >>> a = set([1, 2, 3, 4, 5,‘shaw‘])

       >>> b = set([2,3,5,‘sam‘])

       >>> a.intersection(b) 等同於  a & b

       set([2, 3, 5])

7.從set a中刪除,a和b交集之外的元素

    intersection_update(...)

       >>> a = set([1, 2, 3, 4, 5,‘shaw‘])

       >>> b = set([2,3,5,‘sam‘])

       >>> a.intersection_update(b)

       >>> a

       set([2, 3, 5])

8.判斷兩個集合是否有交集,如果有,則返回True,否則返回False

    isdisjoint(...)

       >>> a = set([1, 2, 3, 4, 5,‘shaw‘])

       >>> b = set([2,3,5,‘sam‘])

       >>> a.isdisjoint(b)

       False

       >>> c = set([9,‘alices‘])

       >>> a.isdisjoint(c)

       True

9.測試一個集合中的元素是否都在另一個元素中(即測試集合b是否是集合a的子集)

    issubset(...)

    Report whether another set contains this set

    註:

    issuperset(...)

    Report whether this set contains another set

       >>> a = set([1, 2, 3, 4, 5,‘shaw‘])

       >>> b = set([2,3,5,‘sam‘])

       >>> c = set([5,‘shaw‘])

       >>> b.issubset(a)

       False

       >>> c.issubset(a)  等同於 c <= a

       True

10.刪除一個元素(預設刪除第一項),如果要刪除的元素不存在,則報keyerror

    pop(...)

        >>>a = set([1, 2, 3, 4, 5, ‘shaw‘])

        >>>a.pop()

        1

        >>>a

        set([2,3, 4, 5, ‘shaw‘])

 

11. 刪除指定元素,如果要刪除的元素不存在,則報keyerror

    remove(...)

       >>> a = set([1, 2, 3, 4, 5,‘shaw‘])

       >>> a.remove(1)

       >>> a

       set([2, 3, 4, 5, ‘shaw‘])

       >>> a.remove(‘shaw‘)

       >>> a

       set([2, 3, 4, 5])

12.對稱差集(此元素在a中有且b中沒有,和b中有且a中沒有的元素)

symmetric_difference(...)

       >>> a = set(‘abcdef‘)

       >>> b = set(‘abcklyt‘)

       >>> a.symmetric_difference(b)

       set([‘e‘, ‘t‘, ‘f‘, ‘y‘, ‘k‘, ‘l‘, ‘d‘])

       >>> a ^ b

       set([‘e‘, ‘t‘, ‘f‘, ‘y‘, ‘k‘, ‘l‘, ‘d‘])

13.取並集(返回一個新的 set 包含 s 和t 中的每一個元素)

    union(...)

       >>> a = set(‘acdf‘)

       >>> b = set(‘adkh‘)

       >>> a.union(b)

       set([‘a‘, ‘c‘, ‘d‘, ‘f‘, ‘h‘, ‘k‘])

       >>> a | b

       set([‘a‘, ‘c‘, ‘d‘, ‘f‘, ‘h‘, ‘k‘])

14.向集合a中添加元素(添加另一個集合時,會去除重複的元素)

    update(...)

       >>> a = set(‘acdf‘)

       >>> b = set(‘adkh‘)

       >>> a.update(b)

       >>> a

       set([‘a‘, ‘c‘, ‘d‘, ‘f‘, ‘h‘, ‘k‘])

 

       >>> a = set(‘acdf‘)

       >>> b = set(‘adkh‘)

       >>> a |= b

       >>> a

       set([‘a‘, ‘c‘, ‘d‘, ‘f‘, ‘h‘, ‘k‘])

 

       >>> a= set(‘shaw‘)

       >>> a.update(‘bnga‘)

       >>> a

       set([‘a‘, ‘b‘, ‘g‘, ‘h‘, ‘n‘, ‘s‘, ‘w‘])


本文出自 “Shaw Blog” 部落格,請務必保留此出處http://opsedu.blog.51cto.com/9265055/1762866

Python set方法總結

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.