元组的元组是python中内置的一种数据结构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了元组的元组是python中内置的一种数据结构相关的知识,希望对你有一定的参考价值。

参考技术A

元组和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组。元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。
1、Python中元组的书面形式和规范:
tuplename=(tupleitem1,tupleitem2,tupleitem3,tupleitem4)
tuplename=tupleitem1,tupleitem2,tupleitem3,tupleitem4
注意:定义元组的是逗号,而非括号。
zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)
new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]
一个空的元组由一对空的圆括号组成,如 myempty = ()。然而,含有单个元素的元组必须在第一个(唯一一个)项目后跟一个逗号,这样Python才能区分元组和表达式中一个带圆括号的对象。

以上是关于元组的元组是python中内置的一种数据结构的主要内容,如果未能解决你的问题,请参考以下文章

编程里面元组和数组的区别是啥?

Python入门教程第29篇 元组

Python中的元组(Tuple)

Python中的基本数据类型之元组类型

python的元组和列表的区别

利用Python进行数据分析笔记:3.1数据结构