[Python Notes]
lamHo
septembre 2017
Python中几种主要的数据结构——容器(container):
容器 container | 特点 |
---|---|
元组 tuple | 有序 不可修改 |
列表 list | 有序 |
数组 array ( numpy ) | 有序 |
字典 dictionary | 无序 查找速度快 key不可以重复且不可变 同一个dict的key/value都可以取任意类型的项目 浪费空间 |
本篇通过比较它们的存储方式和用法,分清这几种数据结构的异同。
list vs tuple
list vs array
In [1]:
# 定义一个tuple
tuple1 = ('bosco','ricky','pinky')
tuple1
Out[1]:
In [2]:
# 一个项目的 tuple
tuple2 = (5,)
tuple2
Out[2]:
In [3]:
# 一次赋多值
x,y,z = tuple1
print(x,y,z)
In [4]:
# 用in判断
'bosco' in tuple1
Out[4]:
In [5]:
# 索引
tuple1[0]
Out[5]:
In [6]:
# string
tuple("money")
Out[6]:
In [7]:
list1 = [16,2,53,24,5,36,67,80]
list1
Out[7]:
In [8]:
# 索引 indexing
list1[5]
Out[8]:
In [9]:
# 分片 slicing
list1[:5]
Out[9]:
In [10]:
list1[3:]
Out[10]:
In [11]:
list1[-3:]
Out[11]:
In [12]:
list1[1:6]
Out[12]:
In [13]:
list1[-4:-1]
Out[13]:
In [14]:
list1[0:6:2]
Out[14]:
In [15]:
list1[6:0:-2]
Out[15]:
In [16]:
list1[::-3]
Out[16]:
In [17]:
# more functions
len(list1)
Out[17]:
In [18]:
max(list1)
Out[18]:
In [19]:
min(list1)
Out[19]:
In [20]:
# 改变元素
list1[1]=30
list1
Out[20]:
In [21]:
# 增加元素
list1.append(4)
list1
Out[21]:
In [22]:
list1.sort()
list1
Out[22]:
In [23]:
# 算数
list2 = ['alpha','beta','gamma','gamma','alpha','alpha']
list2.count('alpha')
Out[23]:
In [24]:
list2 = [2,56,8,2,9,9,47,2,2]
list2.count(2)
Out[24]:
In [25]:
# 联合list
list3 = [56,8,7,91]
list3_add = [15,32,5]
list3.extend(list3_add)
list3
Out[25]:
In [26]:
list4 = ['apple','banana','orange','peach']
list4.index('orange') # 找出该元素位置
Out[26]:
In [27]:
list4.insert(2,'cherry') # 在2号位插入新元素
list4
Out[27]:
In [28]:
list4.pop(1) # 删除1号位元素(并显示被删除的元素)
Out[28]:
In [29]:
list4
Out[29]:
In [30]:
list4.remove('orange') # 删除该元素(不显示被删除元素)
list4
Out[30]:
In [31]:
list('hey')
Out[31]:
In [32]:
sorted('hey')
Out[32]:
In [33]:
sentence = "Take it away"
sentence.find('it') # i:5
Out[33]:
In [34]:
sen1 = ['hello','world','welcome','so','good']
sen2 = 'miao'
sen2.join(sen1) # join函数(str):在sen1的每个元素之间插入sen2
Out[34]:
In [35]:
sentence = "I toOK a Photo"
sentence.lower() # 大写字母转小写
Out[35]:
In [36]:
sentence.upper() # 小写转大写
Out[36]:
In [37]:
sentence = "it was my bro"
sentence.replace('bro','sis') # 替换字符string
Out[37]:
In [38]:
import numpy as np
a = np.array([[18., 17., 16.], [14., 19., 18.]])
a
Out[38]:
In [39]:
a.ndim # rank of an array
Out[39]:
In [40]:
a.shape # dimension or shape of an array (result is a tuple)
Out[40]:
In [41]:
a.size # total number of items
Out[41]:
In [42]:
a.dtype # type of item (rq. array里面所有item都是同样的类型)
Out[42]:
In [43]:
np.arange(15)
Out[43]:
In [44]:
np.arange(3,9)
Out[44]:
In [45]:
np.arange(3,9,2)
Out[45]:
In [46]:
np.linspace(3,9,5)
Out[46]:
In [47]:
np.zeros(3)
Out[47]:
In [48]:
np.zeros((3,2))
Out[48]:
In [49]:
np.ones(3)
Out[49]:
In [50]:
np.ones((3,2))
Out[50]:
In [51]:
np.eye(3)
Out[51]:
In [52]:
np.diag([5,3,6])
Out[52]:
In [53]:
family = {'Dad':'Dickson','Mom':'Sara','Bro':'Bosco'}
family
Out[53]:
In [54]:
family['Dad']
Out[54]:
键不可变,可以是数字、字符串、元组。
In [55]:
dict1 = {'a':5,666:'yoooo',(12,'b'):('c','d')}
dict1
Out[55]:
In [56]:
home = family.copy()
home
Out[56]:
In [57]:
'Mom' in home
Out[57]:
In [58]:
'Sis' in home
Out[58]:
In [59]:
'Bosco' in home.values()
Out[59]:
In [60]:
family.clear()
family
Out[60]:
In [61]:
user = {'name':'Bosco','age':40,'sex':'male'}
user
Out[61]:
In [62]:
user['age'] = 32 # 修改一个value
user
Out[62]:
In [63]:
user['city'] = 'New York' # 添加一个项目
user
Out[63]:
In [64]:
del user['sex'] # 删除一个项目
user
Out[64]:
In [65]:
list1 = [5,9,88,26,19]
type(list1)
Out[65]:
In [66]:
tuple1 = [75,31,4,60]
type(tuple1)
Out[66]:
In [67]:
array1 = np.array([62,8,99,6])
type(array1)
Out[67]:
In [68]:
dict1 = {'a':5,666:'yoooo',(12,'b'):('c','d')}
type(dict1)
Out[68]:
In [69]:
l_to_t = tuple(list1)
type(l_to_t)
Out[69]:
In [70]:
a_to_t = tuple(array1)
type(a_to_t)
Out[70]:
In [71]:
# key to tuple
d_to_t = tuple(dict1)
d_to_t
Out[71]:
In [72]:
# value to tuple
dv_to_t = tuple(dict1.values())
dv_to_t
Out[72]:
In [73]:
t_to_l = list(tuple1)
type(t_to_l)
Out[73]:
In [74]:
a_to_l = list(array1)
type(a_to_l)
Out[74]:
In [75]:
# key to list
d_to_l = list(dict1)
d_to_l
Out[75]:
In [76]:
# value to list
dv_to_l = list(dict1.values())
dv_to_l
Out[76]:
In [77]:
l_to_a = np.array(list1)
type(l_to_a)
Out[77]:
In [78]:
t_to_a = np.array(tuple1)
type(t_to_a)
Out[78]: