python3_list and tuple

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3_list and tuple相关的知识,希望对你有一定的参考价值。

>>>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的主要内容,如果未能解决你的问题,请参考以下文章

[Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript(代码片

[原创][Synth 8-2543] port connections cannot be mixed ordered and named ["*_Top.v":1151](代码片

Week 3: Structured Types 5. Tuples and Lists Exercise: odd tuples

LeetCode-面试算法经典-Java实现106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)(示例(代码片

python 两个tuple元组之间连接判断是否有一个为空, and 和 & ,只能用and 不能用&

tuple and list 练习