python--元组tuple
元组与列表一样,都是序列。但元组不能修改内容(列表允许)
默认的,元组通过圆括号括起来
1. 使用type函数查看类型
numbers = (1,2,3,4,5,6,7,8,9,0)
print(type(numbers))
2. tuple 函数
tuple 函数的功能与list函数基本一样:以一个序列作为参数并把它转换为元组。
AAA = [1,2,3,4,5]
print (type(AAA))
BBB = tuple(AAA)
print (type(BBB))
元组的基本操作
同其他序列(如:索引,分片,相加,相乘)
1.索引
CCC = (11,22,33,44,55,66,77,88)
print (CCC[0])
print (CCC[4])
print (CCC[-1])
注:使用负数索引,python会从右边开始计算。最后一个元素的位置编号是-1
2.分片
DDDD = (12,222,112,333,44,1234,11111,1,33,455,66667,87787)
print (DDDD[3:6]) # 获取第4个到第6个元素
print (DDDD[-3:]) # 获取最好三个元素
print (DDDD[:3]) # 获取前三个元素
print (DDDD[0:10:2]) # 步长为2分片
print (DDDD[::4]) # 步长为4分片
print (DDDD[::-1]) # 从右到左提取元素
3.序列相加
EE = (2,3,4,5,6)
FF = (11,33,32,88,90)
print (EE+FF)
4.序列相乘
GG = ("I","LOVE","PYTHON","~")
print (GG*5)
HH = (12,23,34,56,78)
print (HH*5)
注:元组的内容不能修改
5.统计元素出现次数count函数
HH = (12,22,33,33,44,44,44,55,12)
print (HH.count(12))
print (HH.count(44))
元组tuple官方文档解析
class tuple(object):
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items
If the argument is a tuple, the return value is the same object.
"""
def count(self, value): # real signature unknown; restored from __doc__
""" T.count(value) -> integer -- return number of occurrences of value """
return 0
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
def __add__(self, *args, **kwargs): # real signature unknown
""" Return self+value. """
pass
def __contains__(self, *args, **kwargs): # real signature unknown
""" Return key in self. """
pass
def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass
def __getitem__(self, *args, **kwargs): # real signature unknown
""" Return self[key]. """
pass
def __getnewargs__(self, *args, **kwargs): # real signature unknown
pass
def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass
def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass
def __hash__(self, *args, **kwargs): # real signature unknown
""" Return hash(self). """
pass
def __init__(self, seq=()): # known special case of tuple.__init__
"""
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)
"""
pass
def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass
def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass
def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass
def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass
def __mul__(self, *args, **kwargs): # real signature unknown
""" Return self*value.n """
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass
def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass
def __rmul__(self, *args, **kwargs): # real signature unknown
""" Return self*value. """
pass
python--元组tuple的更多相关文章
- Python 元组 tuple() 方法
描述 Python 元组 tuple() 方法用于将可迭代对象(字符串.列表.元祖.字典)转换为元组. 语法 tuple() 方法语法: tuple(iterable) 参数 iterable -- ...
- Python元组tuple(不可变)
Python元组Tuple(不可变): 元组的特点: 1.元组的初始化: tuple = (1, ) #元组只有一个元素的话,初始化时要加,否则当做元素的普通变量类型处理 tuple = (1, 2 ...
- python 元组tuple - python基础入门(14)
在上一篇文章中我们讲解了关于python列表List的相关内容,今天给大家解释一下列表List的兄弟 – 元组,俗称: tuple. 元组tuple和列表List类似,元组有如下特点: 1.由一个或者 ...
- python 元组tuple介绍,使用。
原文 https://blog.csdn.net/ruanxingzi123/article/details/83184909 一 是什么? # python 元组tuple? ''' 元祖tupl ...
- Python—元组tuple
列表的知识其实就类似于c语言中的数组,可插入.修改.list=[a,b,c,d] 而元组tuple,一旦初始化即不可修改.好处与绝对安全. 定义一个空的元组:t=() 定义只有一个元素的元组:t=(1 ...
- Python - 元组(tuple) 详解 及 代码
元组(tuple) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17290967 元组是存放任意元素集合,不能修 ...
- Python元组(tuple)
元组(tuple)是Python中另一个重要的序列结构,与列表类型,也是由一系列按特定顺序排列的元素组成,但是他是不可变序列.在形式上元组的所有元素都放在"()"中,两个元素使用& ...
- Python 元组(Tuple)操作详解
Python的元组与列表类似,不同之处在于元组的元素不能修改,元组使用小括号, 列表使用方括号,元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可 一.创建元组 代码如下: tup1 = (' ...
- Python 元组Tuple概念和操作
# 元组概念:有序的不可变的元素集合 # 和列表的区别就是, 元组元素不能修改 # 定义 # 一个元素的写法 # (666,) t = (666,) #正确写法 t = (666) #错误写法,括号当 ...
- Python 元组 (tuple)
作者博文地址:https://www.cnblogs.com/liu-shuai/ Python的元组与列表类似,同样可通过索引访问,支持异构,任意嵌套.不同之处在于元组的元素不能修改.元组使用小括号 ...
随机推荐
- SQL 流水账余额查询
创建表 CREATE TABLE [dbo].[test]( ,) NOT NULL, [RQ] [date] NULL, [SR] [int] NULL, [ZC] [int] NULL ) ON ...
- Python统计日志中每个IP出现次数
介绍了Python统计日志中每个IP出现次数的方法,实例分析了Python基于正则表达式解析日志文件的相关技巧,需要的朋友可以参考下 本脚本可用于多种日志类型 #-*- coding:utf-8 -* ...
- LVS基础知识
LVS介绍(Linux Virtual Server) 负载调度器,已经集成到内核 工作原理:VS根据请求报文的目标IP和目标协议及端口将其调度转发至某RS,根据调度算法来挑选RS iptables/ ...
- 配置gVim使之不自动生成备份文件
设置 _vimrc set nobacku 1 set nobacku 或指定一个其备份的地方: set backupdir=D:/Program/ Files/Vim /tmp 1 1 1 ...
- C 函数声明及求最大值
#include <stdio.h> int main() { int a,b,c,max(int x,int y,int z); scanf("%d,%d,%d",& ...
- PHP策略模式2
<?php /** PHP 策略模式 * 策略模式是对象的行为模式,用意是对一组算法的封装.动态的选择需要的算法并使用. * 策略模式指的是程序中涉及决策控制的一种模式.策略模式功能非常强大,因 ...
- (转)Springboot定时任务
在我们的项目开发过程中,经常需要定时任务来帮助我们来做一些内容,springboot默认已经帮我们实行了,只需要添加相应的注解就可以实现 1.pom包配置 pom包里面只需要引入springboot ...
- iOS UI基础-13.0 数据存储
应用沙盒 每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离.应用必须待在自己的沙盒里,其他应用不能访问该沙盒 应用沙盒的文件系统目录,如下图所示(假设应用的名称叫Lay ...
- vue打包后404,webpack配置问题
首先声明这是基于vue2.x的 1. 将其中build的配置项assetsPublicPath进行修改,改为上图-->目的是将资源文件的引入路径,改为相对地址(相对index.html) 2.h ...
- RF基础(一) RF内建函数库BuiltIn
Robot framework做为一个测试框架,并不是只能做selenium测试,是支持扩展的, 比如说,你引用requests库就可以做接口测试, 那么无论你用什么库 首先要了解, RF本身提供的内 ...