程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python -- collections

編輯:Python

         The elements of the set are not repeated and unique .

Set creation :

        1. Use {} Create a collection object

>>> a = {3,5,7}
>>> a
{3, 5, 7}

        2. Use set(), Will list 、 Turn iteratable objects such as tuples into collections .

Be careful : De duplication if there are duplicate elements .

>>> a = ['a','b','c','b']
>>> b = set(a)
>>> b
{'b', 'a', 'c'}

Addition of collection elements :

        add() add to

>>> a=[3,5,7]
>>> a.add(9)
>>> a
{9, 3, 5, 7}

Deletion of collection elements :

        1.remove() Deletes the specified element

>>> a = {10,20,30,40,50}
>>> a.remove(20)
>>> a
{10, 50, 30}

         2.clear() Empty

>>> a={1,2,3,4,5}
>>> a.clear()
>>> a
{}

Set related operations :

>>> a = {1,3,'sxt'}
>>> b = {'he','it','sxt'}

        1. intersection

>>> a&b
{'sxt'}
>>> a.intersection(b)
{'sxt'}

        2. Combine

>>> a|b
{1, 3, 'sxt', 'he','it'}
>>> a.union(b)
{1, 3, 'sxt', 'he','it'}

        3. Difference set

>>> a-b
{1, 3}
>>> a.difference(b)
{1, 3}
>>> b-a
{'it', 'he'}


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved