The reason why people give up so quickly is because they look at how far they still have to go, instead of how far they have come
第二章
1.列表的操作 2.元组的操作
3.字符串操作 4.字典的操作
♣列表的操作:
列表:列表用于存储数据、操作数据的最常见的一种数据类型,可以支持数据的增、删、改、查
定义列表:
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys 4 some=["cat","dog","monkey"]
操作列表:
>追加zoo.append
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys 4 zoo=["cat","dog","monkey"] 5 zoo.append("donkey") 6 print(zoo)
#结果是:['cat', 'dog', 'monkey', 'donkey'],追加到列表的最后
>切片zoo[:]
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 zoo=["cat","dog","monkey","donkey","chiken"] 5 print(zoo[0])#只获取第一个值,精确的值,非列表 6 print(zoo[1:])#获取第二个及以后的全部值,以列表的显示展示 7 print(zoo[-1])#获取最后一个值,精确的值,非列表 8 print(zoo[1:-1])#获取下标1到-1的值,不包括-1,俗称顾头不顾尾 9 print(zoo[0::2])#2的意思是每隔2个取一次值 10 print(zoo[::2])#同zoo[0::2] ,凡是带有:的都是切取的列表 11 12 '''结果是: 13 cat 14 ['dog', 'monkey', 'donkey', 'chiken'] 15 chiken 16 ['dog', 'monkey', 'donkey'] 17 ['cat', 'monkey', 'chiken'] 18 ['cat', 'monkey', 'chiken'] 19 20 '''
>插入zoo.insert(i,x)
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 zoo=["cat","dog","monkey","donkey","chiken"] 5 zoo.insert(2,"Tiger")#2表示的是插入的位置 6 zoo.insert(10,"Tiger")#如果下脚标不存在,则在最后一个位置插入 10不存在,则在接近10 即最大脚标位置插入 7 print(zoo) 8 ''' 9 ['cat', 'dog', 'Tiger', 'monkey', 'donkey', 'chiken', 'Tiger'] 10 '''
>修改zoo[0]
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 5 zoo=["cat","dog","monkey","donkey","chiken"] 6 zoo[0]='dog'#修改下脚标为0的值,可见列表中可存储相同的值 7 print(zoo) 8 '''结果是: 9 ['dog', 'dog', 'monkey', 'donkey', 'chiken'] 10 '''
>删除del、pop、remove
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 zoo=["cat","dog","monkey","donkey","chiken"] 5 del zoo[2] #删除下脚标为2的值,monkey没了 6 print(zoo) 7 zoo.remove("dog")#删除指定的值,dog又没了 8 print(zoo) 9 zoo.pop()#删除最后一个值,参数默认是-1,chiken没了 10 print(zoo) 11 zoo.pop(-2)#参数是-2,删除的是倒数第二个值 12 print(zoo) 13 del zoo 14 print(zoo)#会报错,提示zoo未被定义,其实是删除了整个列表 15 '''结果是: 16 ['cat', 'dog', 'donkey', 'chiken'] 17 ['cat', 'donkey', 'chiken'] 18 ['cat', 'donkey'] 19 ['donkey'] 20 '''
>扩展 extend()
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 zoo=["cat","dog","monkey","donkey","chiken"] 5 bird=["Smew","Cuckoo","Partridge","Peafowl"] 6 zoo.extend(bird)#将bird中全部值扩展到zoo中 7 print(zoo) 8 '''结果: 9 ['cat', 'dog', 'monkey', 'donkey', 'chiken', 'Smew', 'Cuckoo', 'Partridge', 'Peafowl'] 10 '''
>统计zoo.count("")
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 zoo=["cat","dog","monkey","donkey","chiken","cat"] 5 count=zoo.count("cat")#统计元素cat在zoo列表中的个数 6 print(count)
'''结果:
2
'''
>排序sort()、翻转reverse
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 zoo=["cat","dog","monkey","donkey","chiken","Cat","1","!#@","陈"] 5 zoo.sort()#排序,默认排序规则是特殊字符,数字,大写字母A-Z,小写字母a-z等开头的排序,即ASICLL编码表顺序排列 6 print(zoo) 7 zoo.reverse()#翻转列表 8 print(zoo) 9 '''结果: 10 ['!#@', '1', 'Cat', 'cat', 'chiken', 'dog', 'donkey', 'monkey', 'xe9x99x88'] 11 '''
>下脚标查询index()
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 zoo=["cat","dog","monkey","donkey","chiken","cat","1","!#@","陈"] 5 lic0=zoo.index("cat")#查找cat的下脚标,只能显示第一个查到的下脚标 6 lic1=zoo.index("cat",1)#从下脚标1这个位置查找cat的下脚标 7 lic2=zoo.index("cat",1,6)#从下脚标1到6之间查找cat的下脚标 8 #lic3=zoo.index("cat",1,4)#报错,提示ValueError: 'cat' is not in list 9 print(lic0,lic1,lic2) 10 '''结果: 11 (0, 5, 5) 12 '''
>拷贝copy()
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass,copy 4 a=[1,2] 5 b=[a,3] 6 print(b) 7 c=copy.copy(b)#浅浅的copy 8 d=copy.deepcopy(b)#俗称深copy 9 print(c)#[[1, 2], 3] 10 print(d)#[[1, 2], 3] 11 a[1]=10 12 print(c)#通过copy()过来的值变化了 [[1, 10], 3] 13 print(d)#通过deepcopy()的值没有变化 [[1, 2], 3]
♣元组
元组:以小括号显示,与列表类似的类型,元组一旦创建,值便不可被修改,又叫只读列表。有两个属性:count()、index()
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 zoo=("dog","cat","monkey")#元组是只读列表,只有count、index属性
♣字典
字典一种key - value 的数据类型,字典的特点是:1.key是唯一的,即天生去重 2.无序
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 zoo_dict={"1":["cat","dog"], 5 "2":["donkey","monkey"], 6 "3":["bird","tiger"] 7 } 8 print(zoo_dict)
>增加
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 zoo_dict={"1":["cat","dog"], 5 "2":["donkey","monkey"], 6 "3":["bird","tiger"] 7 } 8 print(zoo_dict) 9 zoo_dict["3"]=["chiken","Smew"]#增加key=3的值,如果存在key=3,那么更新value值。 10 zoo_dict["4"]=["chiken","Smew"]#增加key=4的值 11 print(zoo_dict) 12 '''结果无序 13 {'1': ['cat', 'dog'], '3': ['bird', 'tiger'], '2': ['donkey', 'monkey']} 14 {'1': ['cat', 'dog'], '3': ['bird', 'tiger'], '2': ['donkey', 'monkey'], '4': ['chiken', 'Smew']} 15 '''
>删除
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 zoo_dict={"1":["cat","dog"], 5 "2":["donkey","monkey"], 6 "3":["bird","tiger"], 7 "4":["bird","tiger"] 8 } 9 print(zoo_dict) 10 zoo_dict.pop("1")#删除了key=1的值,标准的删除 11 print(zoo_dict) 12 del zoo_dict["2"]#另一种删除方式 13 print(zoo_dict) 14 zoo_dict.popitem()#随机删除一组数字 15 #del zoo_dict["5"]#删除不存在的key值报错 16 print(zoo_dict) 17 '''结果: 18 {'1': ['cat', 'dog'], '3': ['bird', 'tiger'], '2': ['donkey', 'monkey'], '4': ['bird', 'tiger']} 19 {'3': ['bird', 'tiger'], '2': ['donkey', 'monkey'], '4': ['bird', 'tiger']} 20 {'3': ['bird', 'tiger'], '4': ['bird', 'tiger']} 21 {'4': ['bird', 'tiger']} 22 '''
>修改
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 zoo_dict={"1":["cat","dog"], 5 "2":["donkey","monkey"], 6 "3":["bird","tiger"], 7 "4":["bird","tiger"] 8 } 9 zoo_dict["4"]=["tiger","bird"]#有则修改 10 zoo_dict["5"]=["tiger","bird"]#无则增加 11 print(zoo_dict) 12 ''' 13 {'1': ['cat', 'dog'], '3': ['bird', 'tiger'], '2': ['donkey', 'monkey'], '5': ['tiger', 'bird'], '4': ['tiger', 'bird']} 14 '''
>查找
#! /user/bin/env python #_-_coding:utf-8_-_ import os,sys,getpass zoo_dict={"1":["cat","dog"], "2":["donkey","monkey"], "3":["bird","tiger"], "4":["bird","tiger"] } print(zoo_dict["4"])#查找key=4的值 print(zoo_dict.get("4"))#查找key=4的值 print(zoo_dict.get("6"))#查找key=6的值 没有也不报错,没有显示None print(zoo_dict["5"])#查找key=5的值,没有则报错 ''' Traceback (most recent call last): File "D:/python/newDay/Day1/Day2/liebiao.py", line 12, in <module> print(zoo_dict["5"])#查找key=5的值,没有则报错 KeyError: '5' ['bird', 'tiger'] ['bird', 'tiger'] None '''
>字典嵌套
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 animal={ 5 "zoo_dict":{"1":["cat","dog"], 6 "2":["donkey","monkey"], 7 "3":["bird","tiger"], 8 "4":["bird","tiger"]}, 9 "zoo_dict2":{"1":["cat","dog"], 10 "2":["donkey","monkey"], 11 "3":["bird","tiger"], 12 "4":["bird","tiger"]} 13 } 14 animal["zoo_dict"]["1"]="[cat]"#修改字典中的值 15 print(animal) 16 animal["zoo_dict"]["1"]+="cater"#拼接字符串 17 print(animal) 18 ''' 19 {'zoo_dict2': {'1': ['cat', 'dog'], '3': ['bird', 'tiger'], '2': ['donkey', 'monkey'], '4': ['bird', 'tiger']}, 'zoo_dict': {'1': '[cat]', '3': ['bird', 'tiger'], '2': ['donkey', 'monkey'], '4': ['bird', 'tiger']}} 20 {'zoo_dict2': {'1': ['cat', 'dog'], '3': ['bird', 'tiger'], '2': ['donkey', 'monkey'], '4': ['bird', 'tiger']}, 'zoo_dict': {'1': '[cat]cater', '3': ['bird', 'tiger'], '2': ['donkey', 'monkey'], '4': ['bird', 'tiger']}} 21 '''
>常用:
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import os,sys,getpass 4 5 zoo_dict={"1":["cat","dog"], 6 "2":["donkey","monkey"], 7 "3":["bird","tiger"], 8 "4":["bird","tiger"]} 9 zoo_dict2 = {"4": ["cat", "dog"], 10 "5": ["donkey", "monkey"], 11 "6": ["bird", "tiger"], 12 "7": ["bird", "tiger"]} 13 print(zoo_dict.values())#获取字典的value 14 print(zoo_dict.keys())#获取字典的key 15 print(zoo_dict.setdefault("1","cat")) 16 zoo_dict.update(zoo_dict2)#把zoo_dict2的key和value添加到zoo_dict里,注意:zoo_dict2中的key与zoo_dict中的key相同时,则能添加 17 print(zoo_dict) 18 print(zoo_dict.items())#字典迭代,生成以列表格式 19 ''' 20 [['cat', 'dog'], ['bird', 'tiger'], ['donkey', 'monkey'], ['bird', 'tiger']] 21 ['1', '3', '2', '4'] 22 ['cat', 'dog'] 23 {'1': ['cat', 'dog'], '3': ['bird', 'tiger'], '2': ['donkey', 'monkey'], '5': ['donkey', 'monkey'], '4': ['cat', 'dog'], '7': ['bird', 'tiger'], '6': ['bird', 'tiger']} 24 [('1', ['cat', 'dog']), ('3', ['bird', 'tiger']), ('2', ['donkey', 'monkey']), ('5', ['donkey', 'monkey']), ('4', ['cat', 'dog']), ('7', ['bird', 'tiger']), ('6', ['bird', 'tiger'])] 25 '''
♣字符串操作:
引用自:http://www.cnblogs.com/zhxiang/p/3385242.html
1 Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等) 2 3 4 去空格及特殊符号 5 s.strip() .lstrip() .rstrip(',') 6 7 复制字符串 8 #strcpy(sStr1,sStr) 9 sStr= 'strcpy' 10 sStr = sStr 11 sStr= 'strcpy' 12 print sStr 13 14 连接字符串 15 #strcat(sStr1,sStr) 16 sStr= 'strcat' 17 sStr = 'append' 18 sStr+= sStr 19 print sStr 20 21 查找字符 22 #strchr(sStr1,sStr) 23 sStr= 'strchr' 24 sStr = 's' 25 nPos = sStr1.index(sStr) 26 print nPos 27 28 比较字符串 29 #strcmp(sStr1,sStr) 30 sStr= 'strchr' 31 sStr = 'strch' 32 print cmp(sStr1,sStr) 33 34 扫描字符串是否包含指定的字符 35 #strspn(sStr1,sStr) 36 sStr= '1345678' 37 sStr = '456' 38 #sStrand chars both in sStrand sStr 39 print len(sStrand sStr) 40 41 字符串长度 42 #strlen(sStr1) 43 sStr= 'strlen' 44 print len(sStr1) 45 46 将字符串中的大小写转换 47 #strlwr(sStr1) 48 sStr= 'JCstrlwr' 49 sStr= sStr1.upper() 50 #sStr= sStr1.lower() 51 print sStr 52 53 追加指定长度的字符串 54 #strncat(sStr1,sStr,n) 55 sStr= '1345' 56 sStr = 'abcdef' 57 n = 3 58 sStr+= sStr[0:n] 59 print sStr 60 61 字符串指定长度比较 62 #strncmp(sStr1,sStr,n) 63 sStr= '1345' 64 sStr = '13bc' 65 n = 3 66 print cmp(sStr1[0:n],sStr[0:n]) 67 68 复制指定长度的字符 69 #strncpy(sStr1,sStr,n) 70 sStr= '' 71 sStr = '1345' 72 n = 3 73 sStr= sStr[0:n] 74 print sStr 75 76 将字符串前n个字符替换为指定的字符 77 #strnset(sStr1,ch,n) 78 sStr= '1345' 79 ch = 'r' 80 n = 3 81 sStr= n * ch + sStr1[3:] 82 print sStr 83 84 扫描字符串 85 #strpbrk(sStr1,sStr) 86 sStr= 'cekjgdklab' 87 sStr = 'gka' 88 nPos = -1 89 for c in sStr1: 90 if c in sStr: 91 nPos = sStr1.index(c) 92 break 93 print nPos 94 95 翻转字符串 96 #strrev(sStr1) 97 sStr= 'abcdefg' 98 sStr= sStr1[::-1] 99 print sStr 100 101 查找字符串 102 #strstr(sStr1,sStr) 103 sStr= 'abcdefg' 104 sStr = 'cde' 105 print sStr1.find(sStr) 106 107 分割字符串 108 #strtok(sStr1,sStr) 109 sStr= 'ab,cde,fgh,ijk' 110 sStr = ',' 111 sStr= sStr1[sStr1.find(sStr) + 1:] 112 print sStr 113 或者 114 s = 'ab,cde,fgh,ijk' 115 print(s.split(',')) 116 117 连接字符串 118 delimiter = ',' 119 mylist = ['Brazil', 'Russia', 'India', 'China'] 120 print delimiter.join(mylist) 121 PHP 中 addslashes 的实现 122 def addslashes(s): 123 d = {'"':'\"', "'":"\'", "