一、元组的常用方法
①元组方法-------count(),元组中某个元素出现的次数
>>> t_tuple=('a','b',11,22) >>> t_tuple.count('a') 1 >>> t_tuple.count('b') 1 >>> t_tuple.count('11') 0 >>> t_tuple.count(11) 1 >>>
①元组方法-------index(),查找出元组中元素的下标位置,如果元素不存在,则会报错
>>> t_tuple.index('a')0>>> t_tuple.index('b')1>>> t_tuple.index(11)2>>> t_tuple.index('c')Traceback (most recent call last): File "", line 1, in ValueError: tuple.index(x): x not in tuple>>>
二、字典及常用方法
①字典常用方法-------clear(),清除字典内容
>>> name_dict={'alex':22,'eric':26,'tony':25} >>> name_dict {'tony': 25, 'alex': 22, 'eric': 26} >>> name_dict.clear() >>> name_dict {} >>>
浅拷贝
②字典常用方法-------copy(),字典浅拷贝,该拷贝纸拷贝第一层,如果子点的key或者value下还继续有字典,
>>> name_dict={'alex':22,'eric':26,'tony':25}>>> name_dict.copy(){'tony': 25, 'alex': 22, 'eric': 26}>>> name_dicts = name_dict.copy()>>> print name_dicts{'tony': 25, 'alex': 22, 'eric': 26}>>> id(name_dict)39525232>>> id(name_dicts)39524800>>>
深拷贝
import copy
test_dict = {'a':{'b':{'c':100}}}
test02_dict=copy.deepcopy(被拷贝的字典),拷贝多层,当被拷贝的字典中二层或者二层以上中的键对应的值发生变化,通过该方法拷贝得到的字典test002_dict中的键值是不会发生变化的
为什么要拷贝?
1当进行修改时,想要保留原来的数据和修改后的数据
数字字符串 和 集合 在修改时的差异? (深浅拷贝不同的终极原因)
1在修改数据时:
2数字字符串:在内存中新建一份数据
3集合:修改内存中的同一份数据
对于集合,如何保留其修改前和修改后的数据?
1在内存中拷贝一份
③字典常用方法-------get(),获取某个key对应的值,如果该key不存在,返回None,也可以指定返回其他结果,get方法可以避免在key不存在的时候返回报错
>>> name_dict {'tony': 25, 'alex': 22, 'eric': 26} >>> name_dict.get('alex') 22 >>> name_dict['alex'] 22 >>> name_dict.get('eric') 26 >>> name_dict.get('susan') >>> print name_dict.get('susan') None >>> print name_dict.get('susan','OK') OK >>> name_dict.get('susan','OK') 'OK' >>> print name_dict['susan'] Traceback (most recent call last): File "", line 1, in KeyError: 'susan' >>>
④字典一般用字符串,数字,类的实例来作为key
⑤判断一个字典是否为字典用如下方法:
>>> nu_dict = {'alex':18,'tony':22} >>> type(nu_dict)>>> type(nu_dict) is dictTrue >>>
⑥字典常用方法-------fromkeys(),可以将一个列表与定义的后面的一个值进行匹配,生成一个新的字典,该方法可以用于一个人名列表,如果要把人名列表对应每个人的个人信息,可以用到次方法
>>>a{'a': 1, 'c': 3, 'b': 2} >>>a.fromkeys([1,2,3,4,5],'t'){1: 't', 2: 't', 3: 't', 4: 't', 5: 't'} >>> >>>a.fromkeys(['alex','eric','tony','susan'],[]){'tony': [], 'alex': [], 'eric': [], 'susan': []}
⑦字典常用方法-------items(),取出字典中所有对应的key:value
>>>name_dict = {'alex':22,'eric':24,'tony':18} >>>name_dict {'tony': 18, 'alex': 22, 'eric': 24} >>>name_dict.items() [('tony', 18), ('alex', 22), ('eric', 24)] >>>
对于字典中的内容不是很多,即上百条或者上千条更甚至上万条可以用以上方法,如果上百万或千万条记录,则建议用如下方法:
>>> for k in name_dict:print k,name_dict[k] ... tony 18 alex 22 eric 24 >>>
这样好处是只读取了字典中的key,生成一个列表,然后再拿着key去字典中找对应的value,可以节约内存,而上面的方法是在内存中生成两个列表,这样就消耗了过多的内存资源。
⑧字典常用方法-------keys(),当前所有的key打印出来
>>> name_dict{'tony': 18, 'alex': 22, 'eric': 24}>>> name_dict.keys()['tony', 'alex', 'eric']>>>
⑨字典常用方法-------pop(),删除指定的键值对,只需要指定键即可
>>> name_dict {'tony': 18, 'alex': 22, 'eric': 24} >>> name_dict.pop('alex') 22 >>> name_dict {'tony': 18, 'eric': 24} >>>
⑩字典常用方法-------del,该方法是一个全局性的,可以删除一个字典,列表,变量,元组等
>>> a='alex' >>> del a >>> a Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined >>> b = {'ccc':111,'ddd':222} >>> del b >>> b Traceback (most recent call last): File " ", line 1, in NameError: name 'b' is not defined >>> c = ('a',2,'c','f',22,) >>> c ('a', 2, 'c', 'f', 22) >>> del c >>> c Traceback (most recent call last): File " ", line 1, in NameError: name 'c' is not defined >>>
字典常用方法-------setdefault()如果字典的键对应的值存在,则输出,不存在则定义一个默认值
>>> c{7: [], 8: [], 9: [], 10: [], 11: [], 12: [], 13: [], 14: [], 15: [], 16: [], 17: [], 18: [], 19: [], 20: [], 21: [], 22: [], 23: [], 24: [], 25: [], 26: [], 27: [], 28: [], 29: [], 30: [], 31: [], 32: [], 33: [], 34: [], 35: [], 36: [], 37: [], 38: [], 39: [], 40: [], 41: [], 42: [], 43: [], 44: [], 45: [], 46: [], 47: [], 48: [], 49: [], 50: [], 51: [], 52: [], 53: [], 54: [], 55: [], 56: [], 57: [], 58: [], 59: [], 60: [], 61: [], 62: [], 63: [], 64: [], 65: [], 66: [], 67: [], 68: [], 69: [], 70: [], 71: [], 72: [], 73: [], 74: [], 75: [], 76: [], 77: [], 78: [], 79: [], 80: [], 81: [], 82: [], 83: [], 84: [], 85: [], 86: [], 87: [], 88: [], 89: [], 90: [], 91: [], 92: [], 93: [], 94: [], 95: [], 96: [], 97: [], 98: [], 99: []}>>> c.setdefault(8)[]>>> c.setdefault(200)>>> c{7: [], 8: [], 9: [], 10: [], 11: [], 12: [], 13: [], 14: [], 15: [], 16: [], 17: [], 18: [], 19: [], 20: [], 21: [], 22: [], 23: [], 24: [], 25: [], 26: [], 27: [], 28: [], 29: [], 30: [], 31: [], 32: [], 33: [], 34: [], 35: [], 36: [], 37: [], 38: [], 39: [], 40: [], 41: [], 42: [], 43: [], 44: [], 45: [], 46: [], 47: [], 48: [], 49: [], 50: [], 51: [], 52: [], 53: [], 54: [], 55: [], 56: [], 57: [], 58: [], 59: [], 60: [], 61: [], 62: [], 63: [], 64: [], 65: [], 66: [], 67: [], 68: [], 69: [], 70: [], 71: [], 72: [], 73: [], 74: [], 75: [], 76: [], 77: [], 78: [], 79: [], 80: [], 81: [], 82: [], 83: [], 84: [], 85: [], 86: [], 87: [], 88: [], 89: [], 90: [], 91: [], 92: [], 93: [], 94: [], 95: [], 96: [], 97: [], 98: [], 99: [], 200: None}>>> c.setdefault(200,'cccc')>>> c{7: [], 8: [], 9: [], 10: [], 11: [], 12: [], 13: [], 14: [], 15: [], 16: [], 17: [], 18: [], 19: [], 20: [], 21: [], 22: [], 23: [], 24: [], 25: [], 26: [], 27: [], 28: [], 29: [], 30: [], 31: [], 32: [], 33: [], 34: [], 35: [], 36: [], 37: [], 38: [], 39: [], 40: [], 41: [], 42: [], 43: [], 44: [], 45: [], 46: [], 47: [], 48: [], 49: [], 50: [], 51: [], 52: [], 53: [], 54: [], 55: [], 56: [], 57: [], 58: [], 59: [], 60: [], 61: [], 62: [], 63: [], 64: [], 65: [], 66: [], 67: [], 68: [], 69: [], 70: [], 71: [], 72: [], 73: [], 74: [], 75: [], 76: [], 77: [], 78: [], 79: [], 80: [], 81: [], 82: [], 83: [], 84: [], 85: [], 86: [], 87: [], 88: [], 89: [], 90: [], 91: [], 92: [], 93: [], 94: [], 95: [], 96: [], 97: [], 98: [], 99: [], 200: None}>>>
字典常用方法-------update(),将两个字典进行整合
>>> ss = {'a':11,'bb':22,'cc':33} >>> tt = {'a':'kk','pp':23,'rt':33} >>> ss.update(tt) >>> ss {'a': 'kk', 'rt': 33, 'pp': 23, 'bb': 22, 'cc': 33} >>>
如果ss中的键和tt中的键有重复,则以tt中的键所对应的值替换掉ss中keys对应的值,如果tt中的键值对在ss中不存在,则在ss中进行创建
字典常用方法-------values(),打印字典中所有的值
>>> ss {'a': 'kk', 'rt': 33, 'pp': 23, 'bb': 22, 'cc': 33} >>> ss.values() ['kk', 33, 23, 22, 33] >>>
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。