Python入門筆記(11):集合 -----集合

1、目錄python

一、集合概述shell

二、關於集合的操做符、關係符號函數

三、集合的一系列操做(添加、更新、訪問、刪除)spa

四、關於集合的內建函數、內建方法code

五、小結對象

2、集合概述索引

集合(set):把不一樣的元素組成一塊兒造成集合,是python基本的數據類型。element

集合元素(set elements):組成集合的成員rem

1 >>> li=['a','b','c','a']2 >>> se =set(li)3 >>> se4 set(['a', 'c', 'b'])

集合對象是一組無序排列的可哈希的值:集合成員能夠作字典的鍵字符串

複製代碼

>>> li=[['a','b','c'],['a','c']]>>> se = set(li)Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    se = set(li)
TypeError: list objects are unhashable

複製代碼

集合分類:可變集合、不可變集合

可變集合(set):可添加和刪除元素,非可哈希的,不能用做字典的鍵,也不能作其餘集合的元素

不可變集合(frozenset):與上面偏偏相反

集合操做符與關係符號:(忘完了!)

3、集合的相關操做

一、建立集合

因爲集合沒有本身的語法格式,只能經過集合的工廠方法set()和frozenset()建立

複製代碼

>>> s = set('beginman')>>> s
set(['a', 'b', 'e', 'g', 'i', 'm', 'n'])>>> t = frozenset('pythonman')>>> t
frozenset(['a', 'h', 'm', 'o', 'n', 'p', 't', 'y'])>>> type(s),type(t)
(<type 'set'>, <type 'frozenset'>)>>> len(s),len(t)
(7, 8)>>> s==t
False>>> s=t>>> s==t
True>>>

複製代碼

二、訪問集合

因爲集合自己是無序的,因此不能爲集合建立索引或切片操做,只能循環遍歷或使用in、not in來訪問或判斷集合元素。

複製代碼

>>> 'a' in s
True>>> 'z' in s
False>>> for i in s:    print i

    
a
h
m
o
n
p
t
y>>>

複製代碼

三、更新集合

可以使用如下內建方法來更新:

s.add()
s.update()
s.remove()

注意只有可變集合才能更新:

複製代碼

>>> s.add(0)Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    s.add(0)AttributeError: 'frozenset' object has no attribute 'add'>>> type(s)<type 'frozenset'>
>>> se = set(s)>>> se
set(['a', 'h', 'm', 'o', 'n', 'p', 't', 'y'])>>> type(se)<type 'set'>
>>> se.add(0)>>> se
set(['a', 0, 'h', 'm', 'o', 'n', 'p', 't', 'y'])>>> se.update('MM')>>> se
set(['a', 0, 'h', 'm', 'o', 'n', 'p', 'M', 't', 'y'])>>> se.update('Django')>>> se
set(['a', 0, 'D', 'g', 'h', 'j', 'm', 'o', 'n', 'p', 'M', 't', 'y'])>>> se.remove('D')>>> se
set(['a', 0, 'g', 'h', 'j', 'm', 'o', 'n', 'p', 'M', 't', 'y'])>>>

複製代碼

del:刪除集合自己

 4、集合類型操做符

一、in ,not in
二、集合等價與不等價(==, !=)
三、子集、超集(見上表)

>>> set('shop')<set('cheeshop')
True>>> set('bookshop')>=set('shop')
True

四、聯合(|)

聯合(union)操做與集合的OR操做其實等價的,聯合符號有個等價的方法,union()。

>>> s1=set('begin')>>> s2=set('man')>>> s3=s1|s2>>> s3
set(['a', 'b', 'e', 'g', 'i', 'm', 'n'])
>>> s1.union(s2)
set(['a', 'b', 'e', 'g', 'i', 'm', 'n'])

但+ 運算則不適合:

複製代碼

>>> s3New = s1+s2Traceback (most recent call last):
  File "<pyshell#68>", line 1, in <module>
    s3New = s1+s2
TypeError: unsupported operand type(s) for +: 'set' and 'set'

複製代碼

五、交集(&)

與集合AND等價,交集符號的等價方法是intersection()

>>> s1&s2
set(['n'])>>> s1.intersection(s2)
set(['n'])

六、查補(-)
等價方法是difference()

>>> s1-s2
set(['i', 'b', 'e', 'g'])>>> s1.difference(s2)
set(['i', 'b', 'e', 'g'])

七、對稱差分(^)

對稱差分是集合的XOR(‘異或’),取得的元素屬於s1,s2但不一樣時屬於s1和s2.其等價方法symmetric_difference()

>>> s1^s2
set(['a', 'b', 'e', 'g', 'i', 'm'])>>> s1.symmetric_difference(s2)
set(['a', 'b', 'e', 'g', 'i', 'm'])

注意:集合之間and,or

>>> s1 and s2
set(['a', 'm', 'n'])   #取 s2>>> s1 or s2
set(['i', 'b', 'e', 'g', 'n']) #取 s1>>>

5、集合、列表、元組、字符串之間轉換

複製代碼

>>> list(s1)
['i', 'b', 'e', 'g', 'n']>>> str(s1)"set(['i', 'b', 'e', 'g', 'n'])">>> tuple(s1)
('i', 'b', 'e', 'g', 'n')

複製代碼

應用:

'''最簡單的去重方式'''lis = [1,2,3,4,1,2,3,4]print list(set(lis))    #[1, 2, 3, 4]

6、關於集合的內建函數、內建方法

一、len():返回集合元素個數

二、set()、frozenset()工廠函數

三、全部集合方法:

四、僅適合可變集合

 

博客搬家啦~:http://beginman.cn/

相關文章
相關標籤/搜索