腾讯课堂——基础数据类型(tuple元祖)
初识元祖
标准类型 |
数字 |
字符串 |
列表 |
字典 |
元组 |
定义:与列表类似,只不过[]改成()
特性:
1.可存放多个值
2.不可变
3.按照从左到右的顺序定义元组元素,下标从0开始顺序访问,有序
元祖的创建
ages = (11, 22, 33, 44, 55)
或
ages = tuple((11, 22, 33, 44, 55))
顺序及下标图示
元祖的常用操作
索引
切片
循环
长度
包含
元祖的特性详解
1.可存放多个值
如果元祖中只有一个值
t = (1,) t = (1) #<==>t = 1
元祖中不仅可以存放数字、字符串,还可以存放更加复杂的数据类型
2.不可变
元祖本身不可变,如果元祖中还包含其他可变元素,这些可变元素可以改变
元祖相关知识拾遗
字典中遇到的tuple
1.字典的定义
元祖可以作为字典的key:
dic = {('alex','male'):'v1',('wusir','male'):'v2'} print(dic)
字典定义可以用到元祖:
person = dict((['name','苑昊'],['文周',18])) person = dict((('name','苑昊'),('文周',18))) print(person)
2.字典的items方法,将字典中的每一项作为元祖返回
dic = {'k1':'v1','k2':'v2'} print(dic.items())
#dict_items([('k1', 'v1'), ('k2', 'v2')])
再讲循环
dic = {'key1':'value1','key2':'value2'} for key,value in dic.items(): print(key,value)
元祖的工厂函数
class tuple(object): """ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable's itemsIf the argument is a tuple, the return value is the same object. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">def</span> count(self, value): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> T.count(value) -> integer -- return number of occurrences of value </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> 0 </span><span style="color: #0000ff;">def</span> index(self, value, start=None, stop=None): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> 0 </span><span style="color: #0000ff;">def</span> <span style="color: #800080;">__add__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self+value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__contains__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return key in self. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__eq__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self==value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__getattribute__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return getattr(self, name). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__getitem__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self[key]. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__getnewargs__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__ge__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self>=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__gt__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self>value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__hash__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return hash(self). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__init__</span>(self, seq=()): <span style="color: #008000;">#</span><span style="color: #008000;"> known special case of tuple.__init__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object. # (copied from class doc) </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__iter__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Implement iter(self). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__len__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return len(self). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__le__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self<=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__lt__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self<value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__mul__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self*value.n </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span><span style="color: #000000;"> @staticmethod </span><span style="color: #008000;">#</span><span style="color: #008000;"> known case of __new__</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__new__</span>(*args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Create and return a new object. See help(type) for accurate signature. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__ne__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self!=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__repr__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return repr(self). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__rmul__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self*value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span></pre>