Python之路,第七篇:Python入门与基础7

python3 元组 (tuple)

           元组是不可改变的序列, 同list 一样, 元组可以存放任意的值;

           表示方法:

                  用小括号()括起来;  单个元素括起来后加逗号(,)区分单个对象还是元组;

           创建空元组:

                 t = ()   #空元组

                 t= tuple() #空元组

            创建一个非空元组:

                  t = (10,) # 含有一个元素的元组;

                  t= 10,

                  t=(1,2,3)

                 t= 10 ,20, 30

                 t = ‘hello’ ,

                 t = (‘1’,‘2’, ‘tree’)

           创建元组错误示例:

                   t = (20)

                   #序列赋值

                   x, y = 100, 200  # x绑定100, y绑定200 

                   x, y = (100, 200)  # x绑定100, y绑定200 

                   x, y = [100, 200]  # x绑定100, y绑定200 

                   x, y = "AB"  #序列赋值

           元组的构造(生成)函数tuple

                  tuple()               #生成一个空元组, 等同于()

                  tuple(iterable)   #用可迭代对象生成一个元组 

            元组的运算:

                  算术运算:   +   、  +=   、  *  、 *=

                   +     拼接

                  +=    拼接后对变量赋值

                   *      生成重复的元组   

                   *=     生成重复的元组并赋值给变量

 1 >>> (1,3) + (4,5)
 2 (1, 3, 4, 5)
 3 >>> x = (1,2,3)
 4 >>> x += (4,)
 5 >>> x
 6 (1, 2, 3, 4)
 7 >>> 
 8 >>> x
 9 (1, 2, 3, 4)
10 >>> x * 3
11 (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
12 >>> x
13 (1, 2, 3, 4)
14 >>> x *= 3
15 >>> x
16 (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
17 >>> 
View Code

 

                   比较运算符:  <    <=    >   >=    ==  !=

 1 >>> x =(1,2,3)
 2 >>> y = (1,3,2)
 3 >>> x < y
 4 True
 5 >>> x > y
 6 False
 7 >>> x == y
 8 False
 9 >>> x != y
10 True
11 >>> 
View Code

 

           元组的 in  / not in  运算符

                    同字符串和列表的in 运算符相同,用于判断一个值是否存在于元组中,存在返回True,不存在返回False。

1 >>> x
2 (1, 2, 3)
3 >>> 1 in x
4 True
5 >>> 5 in  x
6 False
7 >>> 5 not in x
8 True
View Code

 

            元组的索引  indes

                   等同于字符串的索引规则; 但是元组不能切片赋值;

 1 >>> x
 2 (1, 2, 3)
 3 >>> x[0]
 4 1
 5 >>> x[0] = 5
 6 Traceback (most recent call last):
 7   File "<pyshell#51>", line 1, in <module>
 8     x[0] = 5
 9 TypeError: 'tuple' object does not support item assignment
10 >>> x[::2] = 5,6
11 Traceback (most recent call last):
12   File "<pyshell#52>", line 1, in <module>
13     x[::2] = 5,6
14 TypeError: 'tuple' object does not support item assignment
15 >>>
View Code

 

           可用于序列的函数总结:

len(x)

max(x)

min(x)

sum(x)

any(x)

all(x)

reversed(seq)   # 返回反向序列顺序的迭代器

sorted(iterable)      #返回已排序的列表 

 1 >>> s = 'ABCDEF'
 2 >>> reversed(s)
 3 <reversed object at 0x00000000034CA710>
 4 >>> r = reversed(s)
 5 >>> r
 6 <reversed object at 0x0000000003711BA8>
 7 >>> list(r)
 8 ['F', 'E', 'D', 'C', 'B', 'A']
 9 >>> list(r)
10 []     #在迭代器里再去拿数据时已经没有了,拿不了了。
View Code

s='ABC' 

for  x in reversed(s):

        print(x)

 1 >>> s = 'ABCDEF'
 2 >>> r = reversed(s)
 3 >>> for x in r:
 4     print(x)
 5 
 6     
 7 F
 8 E
 9 D
10 C
11 B
12 A
13 >>>
14 >>> s = 'AGBDCEF'
15 >>> sorted(s)   #正向
16 ['A', 'B', 'C', 'D', 'E', 'F', 'G']
17 >>> sorted(s, reverse=True)   #反向
18 ['G', 'F', 'E', 'D', 'C', 'B', 'A']
19 >>> ''.join(sorted(s))   #可迭代对象合并成列表
20 'ABCDEFG'
21 >>>
22 >>> help(''.join)
23 Help on built-in function join:
24 
25 join(...) method of builtins.str instance
26     S.join(iterable) -> str
27     
28     Return a string which is the concatenation of the strings in the
29     iterable.  The separator between elements is S.
30 
31 >>> 
View Code

 

          元组的方法:

                   T.index( v [, begin [,end]])  返回对应元素索引的下标,begin为开始索引,end为结束索引(不包含end);

                   T.count(x)  返回元组中对应元素的个数;

          练习:

 

 

 

 

 

                       

 

posted on 2018-05-02 21:30  微子天明  阅读(97)  评论(0编辑  收藏  举报

导航