Python 中三种重要的数据结构:tuple、list、dict
2,访问
3,修改
4,删除
5,运算
6,内置函数
7,模式函数
二,列表(List)
2,访问 如 元组,略
4,删除
5,运算 如 元组,略
7,模块函数与元组类似
键必须独一无二,但值则不必。
值可以取任何数据类型,但必须是不可变的,如字符串,数或元组。
3,更新
4,删除
注:
5,运算
6,内置函数
一,元组 (tuple)
1,实例
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = ("a", "b", "c", "d");
tup4 = ((1, 2), ('a', 'b', 'c'), [1, 2, 3], {'A': 1, 'b': 2})
2,访问
tup1[2] # 1997
tup2[3:] # (4,5)
tup3[:2] # ('a', 'b')
tup4[3]['A'] # 1
tup4[2] # [1, 2, 3]
3,修改
元组中的元素值是不允许修改的,但我们可以对元组进行连接组合
tup5 = tup1 + tup2;
tup5 # ('physics', 'chemistry', 1997, 2000, 1, 2, 3, 4, 5)
4,删除
元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组
del tup5;
5,运算
len(tup1) # 计算元素个数 4
len(tup4) # 计算元素个数 4
tup3*2 # 复制 ('a', 'b', 'c', 'd', 'a', 'b', 'c', 'd')
for x in tup4: print x # 迭代
6,内置函数
6.1 cmp(tuple1, tuple2) 用于比较两个元组元素
tuple1, tuple2 = (123, 'xyz'), (456, 'abc')
print cmp(tuple1, tuple2); # -1
print cmp(tuple2, tuple1); # 1
6.2 len(tuple1, tuple2) 计算元组元素个数
tuple1, tuple2 = (123, 'xyz', 'zara'), (456, 'abc')
print "First tuple length : ", len(tuple1); # 3
print "Second tuple length : ", len(tuple2); # 2
6.3 max(tuple)
tuple1, tuple2 = (123, 'xyz', 'zara', 'abc'), (456, 700, 200)
print "Max value element : ", max(tuple1); # 'zara'
print "Max value element : ", max(tuple2); # 700
6.4 tuple(seq) 将list转换为元组
tuple([1,2,3,4]) # (1,2,3,4)
tuple({1:2,3:4}) # (1,3) 返回字典键值
tuple((1,2,3,4)) # (1,2,3,4)
7,模式函数
tup1.count('1997') # 1
tup4[1].count('a') # a
tup5.index('chemistry') # 1
tup5.__sizeof__() # 96
tup5.__len__() # 9
tup5.__eq__(tup5) # TURE
tup1.__add__(tup2) # ('physics', 'chemistry', 1997, 2000, 1, 2, 3, 4, 5)
二,列表(List)
1,实例
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];
list4 = [(1, 2), ('a', 'b', 'c'), [1, 2, 3], {'A': 1, 'b': 2}]
2,访问 如 元组,略
3,更新
print list1[2];
list1[2] = 2001;
print list1; #['physics', 'chemistry', 2001, 2000]
4,删除
list1 = ['physics', 'chemistry', 1997, 2000];
del list1[2];
list1 # ['physics', 'chemistry', 2000];
5,运算 如 元组,略
6,内置函数
6.1 list.append(obj) 在列表末尾添加新的对象
aList = [123, 'xyz', 'zara', 'abc'];
aList.append( 2009 );
print "Updated List : ", aList; # [123, 'xyz', 'zara', 'abc', 2009]
6.2 list.count(obj) 统计某个元素在列表中出现的次数
6.3 list.extend(seq) 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
aList = [123, 'xyz', 'zara', 'abc', 123];
bList = [2009, 'manni'];
aList.extend(bList)
print "Extended List : ", aList ;
# Extended List : [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']
6.4 list.index(obj) 从列表中找出某个值第一个匹配项的索引位置
6.5 list.insert(index, obj) 用于将指定对象插入列表
aList = [123, 'xyz', 'zara', 'abc']
aList.insert( 3, 2009)
print "Final List : ", aList
# Final List : [123, 'xyz', 'zara', 2009, 'abc']
6.6 list.pop(obj=list[-1]) 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
aList = [123, 'xyz', 'zara', 'abc'];
print "A List : ", aList.pop();
print "B List : ", aList.pop(2);
# A List : abc
# B List : zara
6.7 list.remove(obj) 移除列表中某个值的第一个匹配项
aList = [123, 'xyz', 'zara', 'abc', 'xyz'];
aList.remove('xyz');
print "List : ", aList;
# List : [123, 'zara', 'abc', 'xyz']
aList.remove('abc');
print "List : ", aList;
# List : [123, 'zara', 'xyz']
6.8 list.reverse() 用于反向列表中元素
aList = [123, 'xyz', 'zara', 'abc', 'xyz'];
aList.reverse();
print "List : ", aList;
# List : ['xyz', 'abc', 'zara', 'xyz', 123]
6.9 list.sort([func]) 对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数
aList = [123, 'xyz', 'zara', 'abc', 'xyz'];
aList.sort();
print "List : ", aList;
# List : [123, 'abc', 'xyz', 'xyz', 'zara']
7,模块函数与元组类似
三,字典 (dict)
1,实例
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
dict1 = { 'abc': 456 };
dict2 = { 'abc': 123, 98.6: 37 };
每个键与值用冒号隔开(:),每对用逗号,每对用逗号分割,整体放在花括号中({})。
键必须独一无二,但值则不必。
值可以取任何数据类型,但必须是不可变的,如字符串,数或元组。
2,访问
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
dict['Name'] #Zara
dict['Age'] #7
3,更新
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry
dict['Age'] # 8
dict['School'] # DPS School
4,删除
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
del dict['Name']; # 删除键是'Name'的条目
dict.clear(); # 清空词典所有条目
del dict ; # 删除词典
注:
4.1 不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住
dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'};
dict['Name'] #Manni
4.2 键必须不可变,可用数,字符串或元组充当,List不可以
dict = {['Name']: 'Zara', 'Age': 7};
dict['Name']; # 报错
5,运算
5.1 str( ) #转化为字符串。
dict = {'Name': 'Zara', 'Age': 7};
print "Equivalent String : %s" % str (dict)
# Equivalent String : {'Age': 7, 'Name': 'Zara'}
6,内置函数
6.1 dict.clear()
#用于删除字典内所有元素
6.2 dict.copy()
# 返回一个字典的浅复制
dict1 = {'Name': 'Zara', 'Age': 7};
dict2 = dict1.copy()
print "New Dictinary : %s" % str(dict2)
# New Dictinary : {'Age': 7, 'Name': 'Zara'}
6.3 dict.fromkeys(seq[, value]))
# 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值
# seq -- 字典键值列表
# value -- 可选参数, 设置键序列(seq)的值
seq = ('name', 'age', 'sex')
dict = dict.fromkeys(seq)
print "New Dictionary : %s" % str(dict)
dict = dict.fromkeys(seq, 10)
print "New Dictionary : %s" % str(dict)
# New Dictionary : {'age': None, 'name': None, 'sex': None}
# New Dictionary : {'age': 10, 'name': 10, 'sex': 10}
6.4 dict.get(key, default=None)
# 函数返回指定键的值,如果值不在字典中返回默认值
# key -- 字典中要查找的键
# default -- 如果指定键的值不存在时,返回该默认值值
dict = {'Name': 'Zara', 'Age': 27}
print "Value : %s" % dict.get('Age')
print "Value : %s" % dict.get('Sex', "Never")
# Value : 27
# Value : Never
6.5 dict.has_key(key)
# 用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false
dict = {'Name': 'Zara', 'Age': 7}
print "Value : %s" % dict.has_key('Age')
print "Value : %s" % dict.has_key('Sex')
# Value : True
# Value : False
6.6 dict.items()
# 以列表返回可遍历的(键, 值) 元组数组
dict = {'Name': 'Zara', 'Age': 7}
print "Value : %s" % dict.items()
# Value : [('Age', 7), ('Name', 'Zara')]
6.7 dict.keys()
# 以列表返回一个字典所有的键
dict = {'Name': 'Zara', 'Age': 7}
print "Value : %s" % dict.keys()
# Value : ['Age', 'Name']
6.8 dict.setdefault(key, default=None)
# 与get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值
dict = {'Name': 'Zara', 'Age': 7}
print "Value : %s" % dict.setdefault('Age', None)
print "Value : %s" % dict.setdefault('Sex', None)
# Value : 7
# Value : None
6.9 dict.update(dict2)
# 把字典dict2的键/值对更新到dict里
dict = {'Name': 'Zara', 'Age': 7}
dict2 = {'Sex': 'female' }
dict.update(dict2)
print "Value : %s" % dict
# Value : {'Age': 7, 'Name': 'Zara', 'Sex': 'female'}
6.10 dict.values()
# 以列表返回字典中的所有值
dict = {'Name': 'Zara', 'Age': 7}
print "Value : %s" % dict.values()
# Value : [7, 'Zara']