python對象及運算 序列小結 集合

對象 序列 運算 回顧

[注]所有的序列都是一個可迭代對象。
類: 數據+方法
         實例的調用接口,或支持的操作。
         3+5
         3.add()
         列表和字典
         變量名:沒有類型,存儲在一個位置,指向對象的引用。
         對象:存儲在另一個位置。
         python是動態語言,在python中引用是可以臨時修改的。因此一個變量名引用的是整形對象,下一步可以任意指向其他一種對象,如列表對象,字典對象等等。如果某個對象不被引用,或引用計數爲零時,那麼這個對象就可被稱爲垃圾回收器所回收的對象。但事實上,python虛擬機可能在內部使用時可能不會立即回收,因爲後續的引用有可能還會指向它。
         a=3
         a={}
         x=3

點號運算符:
        1屬性:調用對象裏面變量名 數據 返回一個數據,顯示則要使用print語句
        2方法:調用操作() 執行方法內部的一段代碼
可調用對象: callable()
         1 函數
         2方法
         3類
字串:‘ ’, “” ,‘’‘ ’‘‘,”“” “”“
列表:[]
元組:()
操作:

list

list.append()
list.insert()
list.count()
list.index()
list.extend()
list.pop()
list.remove()
list.sort()
list.reverse()

dict.has_key() 字典中的鍵是否存在
dict.get()
dict.iteritems() 返回一個迭代器對象 可以使用next 返回元素的對象使用next()返回元素的值
dict.keys()
dict.items()
python3中所有的dict的內置方法
在這裏插入圖片描述

如何獲取使用幫助:

        獲取對象支持使用的屬性和方法:dir()
        某方法的具體使用幫助: help()
        獲取可調用對象的文檔字符串:print(obj.doc

                                                

集合

集合:是一組無序排列、可哈希對象的值。
       支持數學中的關係測試
       也支持成員關係測試: in not in 迭代
       不支持:索引、元素獲取、切片
       集合的類型:set(),frozenset() 這兩個就是工廠函數
       沒有特定語法格式,只能通過工廠函數創建

                                          
python3 help(set)
class set(object)
| set() -> new empty set object
| set(iterable) -> new set object
|
| Build an unordered collection of unique elements.
|
| Methods defined here:
|
| and(self, value, /)
| Return self&value.
|
| contains(…)
| x.contains(y) <> y in x.
|
| eq(self, value, /)
| Return self
value.
|
| ge(self, value, /)
| Return self>=value.
|
| getattribute(self, name, /)
| Return getattr(self, name).
|
| gt(self, value, /)
| Return self>value.
|
| iand(self, value, /)
| Return self&=value.
|
| init(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| ior(self, value, /)
| Return self|=value.
|
| isub(self, value, /)
| Return self-=value.
|
| iter(self, /)
| Implement iter(self).
|
| ixor(self, value, /)
| Return self^=value.
|
| le(self, value, /)
| Return self<=value.
|
| len(self, /)
| Return len(self).
|
| lt(self, value, /)
| Return self<value.
|
| ne(self, value, /)
| Return self!=value.
|
| new(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| or(self, value, /)
| Return self|value.
|
| rand(self, value, /)
| Return value&self.
|
| reduce(…)
| Return state information for pickling.
|
| repr(self, /)
| Return repr(self).
|
| ror(self, value, /)
| Return value|self.
|
| rsub(self, value, /)
| Return value-self.
|
| rxor(self, value, /)
| Return value^self.
|
| sizeof(…)
| S.sizeof() -> size of S in memory, in bytes
|
| sub(self, value, /)
| Return self-value.
|
| xor(self, value, /)
| Return self^value.
|
| add(…)
| Add an element to a set.
|
| This has no effect if the element is already present.
|
| clear(…)
| Remove all elements from this set.
|
| copy(…)
| Return a shallow copy of a set.
|
| difference(…)
| Return the difference of two or more sets as a new set.
|
| (i.e. all elements that are in this set but not the others.)
|
| difference_update(…)
| Remove all elements of another set from this set.
|
| discard(…)
| Remove an element from a set if it is a member.
|
| If the element is not a member, do nothing.
|
| intersection(…)
| Return the intersection of two sets as a new set.
|
| (i.e. all elements that are in both sets.)
|
| intersection_update(…)
| Update a set with the intersection of itself and another.
|
| isdisjoint(…)
| Return True if two sets have a null intersection.
|
| issubset(…)
| Report whether another set contains this set.
|
| issuperset(…)
| Report whether this set contains another set.
|
| pop(…)
| Remove and return an arbitrary set element.
| Raises KeyError if the set is empty.
|
| remove(…)
| Remove an element from a set; it must be a member.
|
| If the element is not a member, raise a KeyError.
|
| symmetric_difference(…)
| Return the symmetric difference of two sets as a new set.
|
| (i.e. all elements that are in exactly one of the sets.)
|
| symmetric_difference_update(…)
| Update a set with the symmetric difference of itself and another.
|
| union(…)
| Return the union of sets as a new set.
|
| (i.e. all elements that are in either set.)
|
| update(…)
| Update a set with the union of itself and others.

Data and other attributes defined here:
hash = None

python3中set支持的方法:
在這裏插入圖片描述

在這裏插入圖片描述

#創建set集合      參數必須是可迭代的對象 Iterator
>>> s1=set([1,2,3,4])
>>> print(s1)
{1, 2, 3, 4}
>>> s2=set([2,3,4,5])
#取集合的交集的兩種方法    &      內置方法:  intersection()
>>> s1&s2
{2, 3, 4}
>>> s1.intersection(s2)
{2, 3, 4}
#取兩個序列的並集 兩種方法     |     內置方法:symmetric_difference()
>>> s1.symmetric_difference(s2)
{1, 5}
>>> s1|s2
{1, 2, 3, 4, 5}
>>> s3=set("xyz")
>>> print(s3)
{'y', 'z', 'x'}
#取出集合中的值並在集合中刪除掉
>>> s3.pop()
'y'
>>> print(s3)
{'z', 'x'}

`>>> id(s1)
49969416
>>> print(s1,s3)
{1, 2, 3, 4} {'z', 'x'}
#在原集合的基礎初上加上一個集合  對象的引用沒有變  相當於合併集合
>>> s1.update(s3)
>>> id(s1)
49969416
>>> print(s1)
{1, 2, 3, 4, 'z', 'x'}
#添加新的元素到集合當中去
>>> s1.add(7)
>>> s1.add("jerry")
>>> print(s1)
{1, 2, 3, 4, 'z', 'x', 7, 'jerry'}
>>> s4=set(["xiaopang","like","home"])
>>> print(s4)
{'home', 'like', 'xiaopang'}

容器、類型、對象 小結

        1:列表、元組,字典可以在不使用換行符的情況下直接分佈的寫於多個行上,另外最後一個元素後面允許使用逗號,如果沒有元素時不允許使用逗號的。

        2:所有對象都有引用計數,爲某個對象指定一個新的變量名稱或者將某個對象放入一個容器當中時,都會導致其引用計數增加,當引用計數爲0時,都會導致垃圾回收機制的回收此對象。另外使用del()語句執行刪除操作或者是某次引用的修改超出當前對象的作用域時,那麼對象的引用計數就會減少。sys.getrefcount(a)可以計數當前變量的引用次數。
在這裏插入圖片描述
        3.列表和字典都支持兩種類型的複製操作;潛複製和深複製。潛複製會創建一個新對象,但是它是對原始對象中中所包含對象的引用,因此它們指向的是同樣的內存地址。深複製則創建一個新對象,並且遞歸複製它所包含的所有對象。深複製就是原來的對象的修改並不會導致複製後的新的對象的修改。深複製的方法可使用 copy模塊中deepcopy()來使用。
        4:python中的所有對象都是"第一類的”,這意味着使用標示符命名的所有對象都具有相同狀態,於是能夠命名所有對象都可以直接當數據進行處理。
        5:序列表示爲索引爲非負整數的有序對象集合,包括字符串、列表、元組,字符串是字符的序列,而列表和元組則是任意python對象的序列。字符串和元組不可變,列表和字典則是可變的。列表支持元素的插入,替換等操作。所有序列都支持迭代。
        6:所有序列都支持的操作方法:
               1:s[i] 索引
                2: s[i:j] 切片
                3:s[i:j:stride] 擴展切片
                4:len()求長度
                5:max()最大值
                6:min()最小值
                7:sum() 數值序列的求和
                8:all()判斷序列中是否存在False
                9:any() 判斷任意爲True
                10:s1+s2 連接
                11:s1*N 重複
                12:obj in s1:成員關係判斷
                13:obj not in s1 :非成員關係判斷

        7.可變序列的操作:
               1.s1[index]=value 元素賦值
               2.s1[i:j]=t 切片賦值
               3.del s1[index] 刪除序列中指定索引位置的元素
               4.del s1[i:j] 刪除序列中的某個切片
               5:del s1[i:j:stride] 刪除序列中的擴展切片

完結

下一篇 python表達式和語句

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章