標籤:python methods list
__author__ = 'hualong_zhang'# -*- coding:utf-8 -*-import sysreload(sys)sys.setdefaultencoding('utf-8')init_list_1 = [1, 4, 9, 'cat', 'dog', 'dog', 'bird', ['fish']]init_list_2 = [1, 4, 9]print 'the origin list 1 and 2:\n', init_list_1, '\n', init_list_2init_list_1.append(16) # 尾加一個元素print init_list_1print init_list_1.count('dog') # 返回某元素出現個數init_list_1.extend(init_list_2) # 尾加一個listprint init_list_1print init_list_1.index('cat') # 返回某元素位置init_list_1.insert(3, 'bird') # 插入print init_list_1for i in range(4): init_list_1.pop() # 從後彈出print init_list_1init_list_1.remove('dog') # 移除init_list_1.remove('dog')print init_list_1init_list_1.reverse() # 可能是歸類print init_list_1init_list_1.sort() # 元素排序print init_list_1#下面這些__xxx__形式的,是所謂Python __Underscore__ Methodsprint init_list_1.__add__(init_list_2) # 串連起來,但不賦給左值print init_list_1.__class__ # 類所有的方法,擷取類型print init_list_1.__contains__(init_list_2) # 是否包含print init_list_1print init_list_1.__eq__(init_list_2) # 相當於==print init_list_1.__ge__(init_list_2) # 相當於>=print init_list_1.__ne__(init_list_2) # 相當於!=print init_list_1.__lt__(init_list_2) # 相當於<print init_list_1.__le__(init_list_2) # 相當於<=print init_list_1.__gt__(init_list_2) # 相當於>print init_list_1.__mul__(2) # 相當於*,就是連續幾個init_list_1print init_list_1.__rmul__(2) # 與mul效果一樣,他說x.__rmul__(other) <==> other * x ,Only called if other.__mul__ is not defined or other.__mul__(x) returns NotImplemented.init_list_1.__iadd__(init_list_2) # x.__iadd__(other) <==> x += other,說明以i開頭這些會作用於自身print init_list_1init_list_1.__imul__(2) # 自身變為原來自身的重複print init_list_1print init_list_1.__iter__() # 我猜是返回一個自身迭代器print init_list_1.__sizeof__() # 返回尺寸print init_list_1.__len__() # 返回元素個數print init_list_1.__reversed__() # Should return an iterator suitable for iterating over all the items contained in the instance in reverse order.print init_list_1.__getitem__(2) # 擷取第幾個元素print init_list_1.__getslice__(2, 5) # 擷取i到j區間元素init_list_1.__delitem__(2) # 刪除元素print init_list_1init_list_1.__delslice__(0, 2) # 刪除切片print init_list_1init_list_1.__setitem__(2, 'dog') # 設定第幾號元素print init_list_1init_list_1.__setslice__(2, 6, 'human') # 把i到j替換為某個序列,human在裡頭會變成...h,u,m,a,n...print init_list_1print init_list_1.__str__() # 這是object的方法,變為strprint init_list_1.__repr__() # 他是這樣說的(然後我這裡直接輸出了init_list_1本身) x.__repr__() <==> repr(x) <==> `x`# Should return a string representation of the class or instance with as much information as possible, preferably something that can be passed to eval to recreate the object. Return value must be a string.# If not present, returns something like '<class __main__.Class at 0x2e6d50>' for classes and '<__main__.Class instance at 0xbfb70>' for instances.init_list_1.__new__(type(init_list_2)) # 大概是在參數的位置放一個list的子類型,然後類型就轉過去了print init_list_1print init_list_1.__hash__ # 輸出時none,不是雜湊函數嗎,暫時不理解
<span style="font-size:24px;color:#000099;"><strong>運行出來是這樣</strong></span>
<p>D:\Python27\python.exe C:/Users/hualong_zhang/PycharmProjects/method_in_list_and_tuple/method_in_list_and_tuple.pythe origin list 1 and 2:[1, 4, 9, 'cat', 'dog', 'dog', 'bird', ['fish']] [1, 4, 9][1, 4, 9, 'cat', 'dog', 'dog', 'bird', ['fish'], 16]2[1, 4, 9, 'cat', 'dog', 'dog', 'bird', ['fish'], 16, 1, 4, 9]3[1, 4, 9, 'bird', 'cat', 'dog', 'dog', 'bird', ['fish'], 16, 1, 4, 9][1, 4, 9, 'bird', 'cat', 'dog', 'dog', 'bird', ['fish']][1, 4, 9, 'bird', 'cat', 'bird', ['fish']][['fish'], 'bird', 'cat', 'bird', 9, 4, 1][1, 4, 9, ['fish'], 'bird', 'bird', 'cat'][1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]<type 'list'>False[1, 4, 9, ['fish'], 'bird', 'bird', 'cat']FalseTrueTrueFalseFalseTrue[1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9, ['fish'], 'bird', 'bird', 'cat'][1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9, ['fish'], 'bird', 'bird', 'cat'][1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9][1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]<listiterator object at 0x019B79B0>13220<listreverseiterator object at 0x019B79B0>9[9, ['fish'], 'bird'][1, 4, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9][['fish'], 'bird', 'bird', 'cat', 1, 4, 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9][['fish'], 'bird', 'dog', 'cat', 1, 4, 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9][['fish'], 'bird', 'h', 'u', 'm', 'a', 'n', 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9][['fish'], 'bird', 'h', 'u', 'm', 'a', 'n', 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9][['fish'], 'bird', 'h', 'u', 'm', 'a', 'n', 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9][['fish'], 'bird', 'h', 'u', 'm', 'a', 'n', 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]None</p><p>Process finished with exit code 0</p>
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Python裡list的那些method學習筆記