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的元组与列表类似,同样可通过索引访问,支持异构,任意嵌套.不同之处在于元组的元素不能修改.元组使用小括号 ...
随机推荐
- centos mysql 实战 第一节课 安全加固 mysql安装
centos mysql 实战 第一节课 安全加固 mysql安装 percona名字的由来=consultation 顾问+performance 性能=per con a mysql ...
- 常见Chrome 插件
Chrome插件网:http://chromecj.com/downloadstart.html Chrome浏览器:http://chromecj.com/chrome/2014-09/177.ht ...
- 线程安全问题.md
# 线程安全问题: - 如果一个资源/变量,他对于多线程来讲,不用加锁也不会引起任何问题,则称为线程安全 - 线程不安全变量类型:list, set, dict - 线程安全变量类型: queue # ...
- NYOJ 最大和
#include<iostream> #include<algorithm> #include<string> using namespace std; ][]; ...
- Spark代码中设置appName在client模式和cluster模式中不一样问题
问题 Spark应用名在使用yarn-cluster模式提交时不生效,在使用yarn-client模式提交时生效,如图1所示,第一个应用是使用yarn-client模式提交的,正确显示我们代码里设置的 ...
- 安装OpenSSL中出现的问题及解决
1.报错:install-record.txt --single-version-externally-managed --compile" failed with error code 1 ...
- 【LeetCode每天一题】4Sum(4数之和)
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- [LeetCode] 367. Valid Perfect Square_Easy tag:Math
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- css--clearfix浮动
解读浮动闭合最佳方案:clearfix: http://www.daqianduan.com/3606.html clearfix清除浮动进化史:http://www.admin10000.com/d ...
- [pat]A1072 Gas Station
这道题的结点编号是字符串类型,处理的过程很有意思,用getID将house和GasStation进行区分 #include<bits/stdc++.h> using namespace s ...