# 定义一个tuple
tuple1 = ('bosco','ricky','pinky')
tuple1

# 一个项目的 tuple
tuple2 = (5,)
tuple2

# 一次赋多值
x,y,z = tuple1
print(x,y,z)

# 用in判断
'bosco' in tuple1

# 索引
tuple1[0]

# string
tuple("money")

"""
Explanation: <font color="seagreen">[Python Notes]<br><b>lamHo</b><br>septembre 2017</font>  
Introduction (1) 数据结构
<font color='tomato'>tuple</font> vs <font color='tomato'>list</font> vs <font color='tomato'>array</font> vs <font color='tomato'>dictionary</font>
Python中几种主要的数据结构——容器（container）： 
|容器 container|特点|
|:-:|:-|
|元组 <font color='seagreen'>tuple</font>    |有序<br>不可修改  |
|列表 <font color='seagreen'>list</font>      |有序  |
|数组 <font color='seagreen'>array ( numpy )</font>     |有序  |
|字典 <font color='seagreen'>dictionary</font>|无序<br>查找速度快<br>key不可以重复且不可变<br>同一个dict的key/value都可以取任意类型的项目<br>浪费空间|  
本篇通过比较它们的存储方式和用法，分清这几种数据结构的异同。  


list vs tuple  

list 可修改：增减项目，修改项目
tuple <font color='tomato'>不可修改</font>：可用于被保护、不需要修改的数据



list vs array

内存地址
list 所有项目的内存地址可以不连续
array 连续的内存空间


数据类型
list 任意类型的对象
array <font color='tomato'>对对象有限制</font>：一旦声明数据类型，放入的每个项目都是同此类型的对象







tuple 元组      
list 列表       
array 数组      
dictionary 字典   
互相之间转换

1. tuple<a name="1tuple"></a>
End of explanation
"""


list1 = [16,2,53,24,5,36,67,80]
list1

# 索引 indexing
list1[5]

# 分片 slicing
list1[:5]

list1[3:]

list1[-3:]

list1[1:6]

list1[-4:-1]

list1[0:6:2]

list1[6:0:-2]

list1[::-3]

# more functions
len(list1)

max(list1)

min(list1)

# 改变元素
list1[1]=30
list1

# 增加元素
list1.append(4)
list1

list1.sort()
list1

"""
Explanation: 2. list<a name="2list"></a>
list1
End of explanation
"""


# 算数
list2 = ['alpha','beta','gamma','gamma','alpha','alpha']
list2.count('alpha')

list2 = [2,56,8,2,9,9,47,2,2]
list2.count(2)

"""
Explanation: list2
End of explanation
"""


# 联合list
list3 = [56,8,7,91]
list3_add = [15,32,5]
list3.extend(list3_add)
list3

"""
Explanation: list3
End of explanation
"""


list4 = ['apple','banana','orange','peach']
list4.index('orange')            # 找出该元素位置

list4.insert(2,'cherry')         # 在2号位插入新元素
list4

list4.pop(1)         # 删除1号位元素（并显示被删除的元素）

list4

list4.remove('orange')        # 删除该元素（不显示被删除元素）
list4

"""
Explanation: list4
End of explanation
"""


list('hey')

sorted('hey')

sentence = "Take it away"
sentence.find('it')       # i:5

sen1 = ['hello','world','welcome','so','good']
sen2 = 'miao'
sen2.join(sen1)       # join函数（str）：在sen1的每个元素之间插入sen2

sentence = "I toOK a Photo"
sentence.lower()          # 大写字母转小写

sentence.upper()          # 小写转大写

sentence = "it was my bro"
sentence.replace('bro','sis')         # 替换字符string

"""
Explanation: string
End of explanation
"""


import numpy as np

a = np.array([[18., 17., 16.], [14., 19., 18.]])         
a

"""
Explanation: 3. array<a name="3array"></a>
creat an array by a list
End of explanation
"""


a.ndim      # rank of an array

a.shape       # dimension or shape of an array (result is a tuple)

a.size       # total number of items

a.dtype       # type of item (rq. array里面所有item都是同样的类型)

"""
Explanation: some functions about array
End of explanation
"""


np.arange(15)        

np.arange(3,9)

np.arange(3,9,2)

"""
Explanation: some functions to creat special array with <font color='seagreen'>NumPy</font>
<font color='seagreen'>np.arange</font>
<font color='seagreen'>np.linspace</font>
<font color='seagreen'>np.zeros</font>
<font color='seagreen'>np.ones</font>
<font color='seagreen'>np.eye</font>
<font color='seagreen'>np.diag</font>
<font color='seagreen'>np.arange (</font> <font color='DarkSlateGray'>a=0,b,step=1</font> <font color='seagreen'>)</font><a name="arange"></a>
从a开始取值，直到b之前
<font color='crimson'>注</font>：如果step不是整数，用<font color='seagreen'>linspace</font>更好。
End of explanation
"""


np.linspace(3,9,5)

"""
Explanation: <font color='seagreen'>np.linspace (</font> <font color='DarkSlateGray'>a,b,num=50</font> <font color='seagreen'>)</font><a name="linspace"></a>
从a开始取num个值，直到b之前
End of explanation
"""


np.zeros(3)

np.zeros((3,2))

"""
Explanation: <font color='seagreen'>np.zeros(</font> <font color='DarkSlateGray'>shape</font> <font color='seagreen'>)</font><a name="zeros"></a>
End of explanation
"""


np.ones(3)

np.ones((3,2))

"""
Explanation: <font color='seagreen'>np.ones(</font> <font color='DarkSlateGray'>shape</font> <font color='seagreen'>)</font><a name="ones"></a>
End of explanation
"""


np.eye(3)

"""
Explanation: <font color='seagreen'>np.eye(</font> <font color='DarkSlateGray'>N</font> <font color='seagreen'>)</font><a name="eye"></a>
End of explanation
"""


np.diag([5,3,6])

"""
Explanation: <font color='seagreen'>np.diag(</font> <font color='DarkSlateGray'>v</font> <font color='seagreen'>)</font><a name="diag"></a>
End of explanation
"""


family = {'Dad':'Dickson','Mom':'Sara','Bro':'Bosco'}
family

family['Dad']

"""
Explanation: 4. dictionary<a name="4dictionary"></a>
creat a dictionary
key: value
字典键: 字典值
End of explanation
"""


dict1 = {'a':5,666:'yoooo',(12,'b'):('c','d')}
dict1

"""
Explanation: 键不可变，可以是数字、字符串、元组。
End of explanation
"""


home = family.copy()
home

"""
Explanation: copy a dictionary
End of explanation
"""


'Mom' in home

'Sis' in home

"""
Explanation: check key in dictionary
End of explanation
"""


'Bosco' in home.values()

"""
Explanation: check values in dictionary
End of explanation
"""


family.clear()
family

"""
Explanation: clear a dictionary
End of explanation
"""


user = {'name':'Bosco','age':40,'sex':'male'}
user

user['age'] = 32      # 修改一个value
user

user['city'] = 'New York'      # 添加一个项目
user

del user['sex']      # 删除一个项目
user

"""
Explanation: modify a dictionary
End of explanation
"""


list1 = [5,9,88,26,19]
type(list1)

tuple1 = [75,31,4,60]
type(tuple1)

array1 = np.array([62,8,99,6])
type(array1)

dict1 = {'a':5,666:'yoooo',(12,'b'):('c','d')}
type(dict1)

"""
Explanation: 5. transform<a name="5transform"></a>

转 tuple：<font color='seagreen'>tuple</font> 函数 ( list / array / dictionary )  
转 list：<font color='seagreen'>list</font> 函数 ( tuple / array / dictionary )  
转 array：<font color='seagreen'>np.array</font> 函数 ( tuple / list )
End of explanation
"""


l_to_t = tuple(list1)
type(l_to_t)

a_to_t = tuple(array1)
type(a_to_t)

# key to tuple
d_to_t = tuple(dict1)
d_to_t

# value to tuple
dv_to_t = tuple(dict1.values())
dv_to_t

"""
Explanation: list / array / dictionary 转 tuple
End of explanation
"""


t_to_l = list(tuple1)
type(t_to_l)

a_to_l = list(array1)
type(a_to_l)

# key to list
d_to_l = list(dict1)
d_to_l

# value to list
dv_to_l = list(dict1.values())
dv_to_l

"""
Explanation: tuple / array / dictionary 转 list
End of explanation
"""


l_to_a = np.array(list1)
type(l_to_a)

t_to_a = np.array(tuple1)
type(t_to_a)

"""
Explanation: list / tuple 转 array
End of explanation
"""


