Python tuple】的更多相关文章

特点:不可改(与字符串一样.不允许删除和修改) 操作:1.print 可使用跟%的元祖  print( '%s is %d years old' % (name, age)) 2.像列表一样有索引 3.定义一个元素,(1,),(1)算是int数 4.tuple 里的list可修改(tuple的每个元素,指向永远不变) 八.元组内置函数Python元组包含了以下内置函数1.cmp(tuple1, tuple2):比较两个元组元素.2.len(tuple):计算元组元素个数.3.max(tuple)…
描述 Python 元组 tuple() 函数将列表转换为元组. 语法 以下是 tuple 的语法: tuple( seq ) 参数 seq -- 要转换为元组的序列. 返回值 返回元组. 实例 以下展示了使用 tuple 的实例: >>>tuple([1,2,3,4]) (1, 2, 3, 4) >>> tuple({1:2,3:4}) #针对字典 会返回字典的key组成的tuple (1, 3) >>> tuple((1,2,3,4)) #元组会返…
1.tuple和list非常类似,但是tuple一旦初始化就不能修改 classmates = ('Michael', 'Bob', 'Tracy') 现在,classmates这个tuple不能变了,它也没有append(),insert()这样的方法.其他获取元素的方法和list是一样的, 你可以正常地使用classmates[0],classmates[-1],但不能赋值成另外的元素. 不可变的tuple有什么意义?因为tuple不可变,所以代码更安全.如果可能,能用tuple代替list…
Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5 ) tup3 = "a", "b", "c", "d" 创建空元组 tup1 = () 元组中只包含一个元…
背景 看到有同学很执着的用 tuple,想起自己刚学 python 时,也是很喜欢 tuple,为啥?因为以前从来没见过这种样子的数据 (1,2), 感觉很特别,用起来也挺好用 i,j=(1,2), 一下子就得到两个变量了: 而且如果函数返回值超过 1 个的话, 用 tuple 挺好,直接就返回了,解析起来也方便. 但 tuple 为啥这么好?是真的这么好吗?真的这么好,为啥比如 json 什么的很少用 tuple 呢?没有细想过. 探索 所以就挺想搞明白,为啥设计了 tuple,应该怎么用?…
描述 Python 元组 tuple() 函数将列表转换为元组.每组词 www.cgewang.com 语法 tuple()方法语法: tuple( iterable ) 参数 iterable -- 要转换为元组的可迭代序列. 返回值 返回元组. 实例 以下实例展示了 tuple()函数的使用方法: 实例 1 >>>tuple([1,2,3,4]) (1, 2, 3, 4) >>> tuple({1:2,3:4}) #针对字典 会返回字典的key组成的tuple (1…
描述 Python 元组 min() 函数返回元组中元素最小值.高佣联盟 www.cgewang.com 语法 min()方法语法: min(tuple) 参数 tuple -- 指定的元组. 返回值 返回元组中元素最小值. 实例 以下实例展示了 min()函数的使用方法: #!/usr/bin/python tuple1, tuple2 = (123, 'xyz', 'zara', 'abc'), (456, 700, 200) print "min value element : "…
描述 Python 元组 max() 函数返回元组中元素最大值.高佣联盟 www.cgewang.com 语法 max()方法语法: max(tuple) 参数 tuple -- 指定的元组. 返回值 返回元组中元素最大值. 实例 以下实例展示了 max()函数的使用方法: #!/usr/bin/python tuple1, tuple2 = (123, 'xyz', 'zara', 'abc'), (456, 700, 200) print "Max value element : "…
描述 Python 元组 len() 函数计算元组元素个数.高佣联盟 www.cgewang.com 语法 len()方法语法: len(tuple) 参数 tuple -- 要计算的元组. 返回值 函数返回元组元素个数. 实例 以下实例展示了 len()函数的使用方法: #!/usr/bin/python tuple1, tuple2 = (123, 'xyz', 'zara'), (456, 'abc') print "First tuple length : ", len(tup…
>>> dir(tuple) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__le…