当前位置:网站首页>Python基础数据类型——tuple浅析
Python基础数据类型——tuple浅析
2020-11-06 20:53:39 【Python进阶者】
Python中tuple的使用
一、什么是元组?
有序列表叫元组:tuple。tuple和list非常类似,但是tuple一旦初始化就不能修改。
二、用法
1. tuple元组的定义
Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。示例如下:
tup1 = ('361way', 'com', 1997, 2000)
print(type(tup1))
tup2 = (1, 2, 3, 4, 5)
tup3 = "a", "b", "c", "d"
print(type(tup3))
这里通过tup3可以看出,其并没有用小括号进行包括,但其也是元组。所以需要记住:任意无符号的对象,以逗号隔开,默认为元组 。另外需要特别注意只有一个元素时元组的创建:
tup1 = (111)
print(type(tup1))
tup1 = ("abc")
print(type(tup1))
tup1 = ("abc",)
print(type(tup1))
元组中只包含一个元素时,需要在元素后面添加逗号,否则就会是int 或 string 等其他数据类型。如果只是创建一个空元组时,则不受逗号的影响:
tup1 = ()
print(type(tup1))
2. 元组的索引与切片
同字符串、列表类型一样,元组也支持索引与切片 。而且用法也相同,
下面结合示例查看下:
tup1 = ('361way', 'com', 2013, 2014)
print(tup1[0])
print(tup1[4]) #取值超出其索引范围时报错
tup2 = (1, 2, 3, 4, 5, 6, 7)
print(tup2[1:5])
由上面的结果可以看出,取出元组的单个元素数据时,得到的是该数据原来的类型 ; 取出其一段元素值时得到的仍是元组。
3. 修改元组的值
元组中的元素值是不允许删除的,但可以使用del语句来删除整个元组,如下实例:
tup1 = ('361way', 'com', 2013, 2014)
del tup1[3] # 删除单个元素,报错
tup1[3] = 'abc' # 更改一个元素的值,报错
print(tup1[3])
del tup1 # 删除整个元组,正常
print(tup1)
在实际应用中,有时候会遇到需要修改元组的值,那怎么办呢?可以通过一个变通的方法实现。
示例如下:
tup1 = ('361way', 'com', 2013, 2014)
list1 = list(tup1)
print(list1)
list1[3] = 'change'
print(list1)
tup1 = tuple(list1)
print(tup1)
可以将tuple元组的值先通过list转化为列表,再对列表内的值进行修改,修改为再将list转化为tuple 。
不过这里需要注意的是此时的tup1已经非彼tup1,具体可以通过id函数进行查看,发现其内存地址已经发生了变化。
三、总结
本文基于Python基础,主要介绍了Python基础中tuple元组的使用,对于tuple的用法做了详细的讲解,用丰富的案例 ,代码效果图的展示帮助大家更好理解 。
tuple是Python内置的有序集合,一个可变,一个不可变。根据需要来选择使用它们。
最后,希望可以帮助大家更好的学习Python。 想学习更多Python网络爬虫与数据挖掘知识,可前往专业网站:http://pdcfighting.com/
想学习更多Python网络爬虫与数据挖掘知识,可前往专业网站:http://pdcfighting.com/
版权声明
本文为[Python进阶者]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4521128/blog/4672820
边栏推荐
- C++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World