python 元组和方法

来源:互联网 发布:牛股选股器源码 编辑:程序博客网 时间:2024/05/16 18:24

一元组是什么?

tuple  是不可变的List ,区别由[ ] 变为( ),另外,tuple 不能增加和减少。

1. tuple 定义:

str1='abdcccccccc;cccc'print(tuple(str1))print(type(tuple(str1)))
('a', 'b', 'd', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', ';', 'c', 'c', 'c', 'c')<type 'tuple'>
创建空元组

b = ();print (type(b))
<type 'tuple'>
只包含一个元素时,需要在元素后面添加逗号。

b = (123,);print (b)print (type(b))
(123,)<type 'tuple'>
方法:

1.count  /统计tuple 中某个元素的个数

t1=('a','b','c','a','d','c')print (t1.count('a'))
2
2.index  返回某个元素的下标

a=(1,2,3,'a',b'')print (a.index('a'))

3




原创粉丝点击