1. 程式人生 > >python3_list and tuple

python3_list and tuple

清除 pop ext index iter nta value __new__ insert

>>>list=[a,1,1]
>>> dir(list)
[__add__, __class__, __contains__, __delattr__, __delitem__, __dir__, __doc__, __eq__, __format__, __ge__, __getattribute__, __getitem__, __gt__, __hash__, __iadd__, __imul__, __init__, __init_subclass__, __iter__
, __le__, __len__, __lt__, __mul__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __reversed__, __rmul__, __setattr__, __setitem__, __sizeof__, __str__, __subclasshook__, append, clear, copy, count, extend, index, insert, pop, remove, reverse
, sort] append, clear, copy, count, extend, index, insert, pop, remove, reverse, sort] “追加”、“清除”、“復制”、“計數”、“擴展”、“索引”、“插入”、“彈出”、“刪除”、“反向”、“排序”。 append(self, p_object): >>> list=[1] >>> list.append(2) >>> list [1, 2] >>> clear(self):
>>> list=[a,b] >>> list.clear() >>> list [] >>> copy(self): >>> list=[1] >>> list1=list.copy() >>> list1 [1] >>> count(self, value): >>> list=[1,2,3,4,5,1] >>> list.count(1) 2 >>> extend(self, iterable): >>> list=[1,2,3] >>> list.extend({1:2}) >>> list [1, 2, 3, 1] >>> index(self, value, start=None, stop=None): >>> list=[1,2,3,2] >>> list.index(2) 1 >>> 沒有該元素則報錯 insert(self, index, p_object): >>> list=[1,2,3] >>> list.insert(3,5) >>> list [1, 2, 3, 5] >>> pop(self, index=None): >>> list=[1,2,3] >>> list.pop(0) 1 >>> list [2,3] >>> list=[1,2,3] >>> list.pop() 3 >>> list [1, 2] remove(self, value): >>> list=[1,2,3] >>> list.remove(1) >>> list [2, 3] >>> reverse(self): >>> list=[1,3,2,3] >>> list.reverse() >>> list [3, 2, 3, 1] >>> sort(self, key=None, reverse=False): >>> list=[1,3,2,3] >>> list.sort() >>> list [1, 2, 3, 3] >>>
t=(1,2)
print(type(t))
print(dir(t))

運行結果:
<class tuple>
[__add__, __class__, __contains__, __delattr__, __dir__, __doc__, __eq__, __format__, __ge__, __getattribute__, __getitem__, __getnewargs__, __gt__, __hash__, __init__, __init_subclass__, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __rmul__, __setattr__, __sizeof__, __str__, __subclasshook__, count, index]




count(self, value): 
t=(1,4,4,2,3,4,3,5)
print(t.count(3))
運行結果:
2
  
index(self, value, start=None, stop=None): 
t=(1,4,4,2,3,4,3,5)
print(t.index(3))
運行結果:
4

python3_list and tuple