python_4(tuple_元组)
2015-10-17 22:26
726 查看
#元组(tuple) -------------------tuple_create.py---------------------- #!/usr/bin/python # -*- coding: UTF-8 -*- tuple = ("apple", "banana", "grape", "orange") #tuple[0] = "a" t = ("apple",) t = () print tuple[-1] print tuple[-2] #print t[0] -------------------tuple_use.py---------------------- #!/usr/bin/python # -*- coding: UTF-8 -*- tuple = ("apple", "banana", "grape", "orange") print tuple[-1] print tuple[-2] tuple2 = tuple[1:3] tuple3 = tuple[0:-2] tuple4 = tuple[2:-2] print tuple2 print tuple3 print tuple4 fruit1 = ("apple", "banana") fruit2 = ("grape", "orange") tuple = (fruit1, fruit2) print tuple print "tuple[0][1] =",tuple[0][1] print "tuple[1][1] =",tuple[1][1] #print tuple[1][2] #打包 tuple = ("apple", "banana", "grape", "orange") #解包 a, b, c, d = tuple print a, b, c, d -------------------tuple_loop.py---------------------- #!/usr/bin/python # -*- coding: UTF-8 -*- #使用range()循环遍历 tuple = (("apple", "banana"),("grape", "orange"),("watermelon",),("grapefruit",)) for i in range(len(tuple)): print "tuple[%d] :" % i, "" , for j in range(len(tuple[i])): print tuple[i][j], "" , print #使用map()循环遍历 k = 0 for a in map(None,tuple): print "tuple[%d] :" % k, "" , for x in a: print x, "" , print k += 1
相关文章推荐
- python_3(循环语句、if、switch、for、break、冒泡算法)
- Python3学习笔记——数据类型和变量
- python 输入函数raw_input和input以及eval函数
- python_2(编译,变量、引号、变量类型、算数表达式)
- 【Python】Learn Python the hard way, ex42 对、类及从属关系
- Python游戏引擎开发(四):TextField文本类
- python 文件输入和输出
- python_1(对象,变量名,函数,分隔符,换行)
- 4.Python对象
- 【Python】Learn Python the hard way, ex41 面向对象术语练习
- Python3学习笔记——基础的巩固
- <LeetCode><Easy> 100 Same Tree
- python例子-关于时间time模块
- python例子-urllib,urllib2练习题合集.
- <LeetCode><Easy> 88 Merge Sorted Array
- 内网ip字典生成 python实现
- <LeetCode><Easy> 83 Remove Duplicates from Sorted List
- 在arcgis使用python脚本进行字段计算时对中文的处理方案
- python学习记录一 --by mumu
- <LeetCode><Easy> 67 Add Binary