元组(tuple)

In [15]: t1 = tuple(x for x in range(1,10))                                     

In [16]: t1                                                                     
Out[16]: (1, 2, 3, 4, 5, 6, 7, 8, 9)

 

 

元组连接

In [1]: t1 = (1,2,3)                                                            

In [2]: t2=(4,5,6)                                                              

In [3]: t1+t2                                                                   
Out[3]: (1, 2, 3, 4, 5, 6)

 

元组的重复

In [4]: t1*3                                                                    
Out[4]: (1, 2, 3, 1, 2, 3, 1, 2, 3)

 

最大值最小值

In [5]: max(t1)                                                                 
Out[5]: 3

In [6]: min(t1)                                                                 
Out[6]: 1

 

列表和元组的转换

In [7]: t1                                                                      
Out[7]: (1, 2, 3)

In [8]: list(t1)                                                                
Out[8]: [1, 2, 3]

In [10]: l1                                                                     
Out[10]: [4, 5, 6]

In [11]: tuple(l1)                                                              
Out[11]: (4, 5, 6)

 

index与count

In [16]: t1                                                                     
Out[16]: (1, 2, 3, 4, 5, 6, 7, 8, 9)

In [17]: t1.index(5)                     # 元素5的索引值                                                
Out[17]: 4

In [18]: t1.count(9)                      # 元素9的个数                           
Out[18]: 1

 

元组拼接

In [37]: city                                                                   
Out[37]: ['bj', 'sh', 'sz']

In [38]: code                                                                   
Out[38]: [1, 2, 3]

In [39]: dict(zip(city,code))                                                   
Out[39]: {'bj': 1, 'sh': 2, 'sz': 3}

 

posted @ 2019-02-18 21:01  Ray_chen  阅读(204)  评论(0编辑  收藏  举报