1 #元組相對列表來講不一樣之處是隻讀不可寫 讀操做和列表一致 2 letter_tuple = ('a','b','c','d'); 3 print(letter_tuple[0]);#result:a 4 print(letter_tuple[0:3]);#result:['a', 'b', 'c'] 5 print(letter_tuple[3::-1]);#result:['c', 'b', 'a']
1 addressDic = {'湖北':['武漢','襄陽'],'湖南':['長沙','岳陽']} 2 for province in addressDic: 3 cityList = addressDic[province]; 4 print(province,end = ":"); 5 for city in cityList: 6 print(city,end = ";"); 7 print(); 8 #result: 9 # 湖北: 武漢;襄陽; 10 # 湖南: 長沙;岳陽;
1 letterStr = 'abcdefg'; 2 #重複輸出 3 print(letterStr*2);#result:abcdefgabcdefg 4 #切片 5 print(letterStr[2:]);#result:cdefg 6 #判斷是否存在 7 print('abc' in letterStr);#result:True 8 #格式化輸出 9 print('abcd%s'%'efg'); 10 11 #string的內置方法 12 #獲取元素個數 13 print(letterStr.count('c'));#result:1 14 #字符串首字母大寫 15 print(letterStr.capitalize());#result:Abcdefg 16 #指定長度居中 17 print(letterStr.center(20,'-'));#result:------abcdefg------- 18 #是否以指定元素結尾 19 print(letterStr.endswith("g"));#result:True 20 #是否以指定元素開頭 21 print(letterStr.startswith("a"));#result:True 22 #指定tab空格長度 23 print("a\tb".expandtabs(tabsize=10));#result:a b 24 #返回指定第一個元素索引值 25 print(letterStr.find('c'));#result:2 26 #格式化 27 print("hello {next}".format(next="world"));#result:hello world 28 print("name:{name};{age} years old".format_map({"name":"zhangsan","age":12}));#result:name:zhangsan;12 years old 29 #返回指定元素索引 不存在時會報錯 30 print(letterStr.find('c'));#result:2 31 #判斷只包含數字或字母 32 print("deqad#".isalnum());#result:False; 33 #判斷是不是整形數字 34 print('12'.isdigit());#result:True 35 #判斷是否不是非法命名 36 print("123a".isidentifier());#result:False 37 #判斷是否小寫 38 print(letterStr.islower());#result:True 39 #判斷是否大寫 40 print(letterStr.isupper());#result:False 41 #判斷是不是個空格 42 print(letterStr.isspace());#result:False 43 #判斷是否每一個單詞首字母大寫 44 print(letterStr.istitle())#result:False 45 #轉小寫 46 print(letterStr.lower());#result:abcdefg 47 #轉大寫 48 print(letterStr.upper());#result:ABCDEFG 49 #大小寫反轉 50 print(letterStr.swapcase());#result:ABCDEFG 51 #左填充 52 print(letterStr.ljust(20,'*'));#abcdefg************* 53 #右填充 54 print(letterStr.rjust(20,'*'));#*************abcdefg 55 #去首尾空白字符 56 print(" ade \n".strip());#result:ade 57 #去首部空白字符 58 print(" ade ".lstrip());#result:ade 59 #去尾部空白字符 60 print(" ade \n".rstrip());#result: ade 61 #替換 62 print(letterStr.replace('g','ghi'));#result:abcdefghi 63 #從右往左查找 返回實際位置 64 print("acbbbbcss".rfind('c'));#result:6 65 #切割字符串 返回列表 66 print(letterStr.split('d'));#result:['abc', 'efg'] 67 #字符串內每一個單詞首字母大寫 68 print('hello world'.title());#result:Hello World
1 import queue 2 3 q = queue.Queue() 4 q.put(1) 5 q.put(2) 6 q.put(3) 7 print(q.qsize()) # 3 查看隊列元素個數 8 print(q.get()) # 1 9 print(q.get()) # 2 10 print(q.get()) # 3 11 print(q.qsize()) # 0 12 print(q.get()) # 阻塞
1 mainDic = { 2 'a1':{ 3 'a1b1':{ 4 'a1b1c1':{}, 5 'a1b1c2':{}, 6 'a1b1c3':{} 7 }, 8 'a1b2':{ 9 'a1b2c1': {}, 10 'a1b2c2': {}, 11 'a1b2c3': {} 12 }, 13 'a1b3':{ 14 'a1b3c1': {}, 15 'a1b3c2': {}, 16 'a1b3c3': {} 17 } 18 }, 19 'a2':{ 20 'a2b1': { 21 'a2b1c1': {}, 22 'a2b1c2': {}, 23 'a2b1c3': {} 24 }, 25 'a2b2': { 26 'a2b2c1': {}, 27 'a2b2c2': {}, 28 'a2b2c3': {} 29 }, 30 'a2b3': { 31 'a2b3c1': {}, 32 'a2b3c2': {}, 33 'a2b3c3': {} 34 } 35 }, 36 'a3':{ 37 'a3b1': { 38 'a3b1c1': {}, 39 'a3b1c2': {}, 40 'a3b1c3': {} 41 }, 42 'a3b2': { 43 'a3b2c1': {}, 44 'a3b2c2': {}, 45 'a3b2c3': {} 46 }, 47 'a3b3': { 48 'a3b3c1': {}, 49 'a3b3c2': {}, 50 'a3b3c3': {} 51 } 52 } 53 }; 54 currentLayer = mainDic; 55 parentLayers = []; 56 while True: 57 for item in currentLayer: 58 print(item); 59 choose = input('input your choose:'); 60 if(choose in currentLayer): 61 if not currentLayer[choose]: 62 print('last layer'); 63 continue; 64 parentLayers.append(currentLayer); 65 currentLayer = currentLayer[choose]; 66 elif choose=='e': 67 currentLayer = parentLayers.pop(); 68 elif choose=='exit': 69 break; 70 else: 71 print('該項不存在'); 72 73 #result: 74 # >>a1 75 # >>a2 76 # >>a3 77 # >>input your choose:a2 78 # >>a2b1 79 # >>a2b2 80 # >>a2b3 81 # >>input your choose:a2b2 82 # >>a2b2c1 83 # >>a2b2c2 84 # >>a2b2c3 85 # >>input your choose:e 86 # >>a2b1 87 # >>a2b2 88 # >>a2b3 89 # >>input your choose:e 90 # >>a1 91 # >>a2 92 # >>a3 93 # >>input your choose:exit
1 a,b = [1,2]; 2 print(a);#result:1 3 print(b);#result:2
該函數用於將一個可遍歷的數據對象(如列表、元組或字符串)組合爲一個索引序列,同時列出數據和數據下標,通常用在 for 循環當中。css
letter_tuple = ['a','b','c','d','e']; for index,value in enumerate(letter_tuple,1): print(index,value); #result: # (1, 1) # (2, 2) # (3, 3) # (4, 4) # (5, 5)
1 letterStr = 'abcdefg'; 2 letterList = ['a','b','c','d','e','f','g']; 3 print(len(letterStr));#result:7 4 print(len(letterList));#result:7
1 list = []; 2 if list:print('True'); 3 else:print('False'); 4 #result:False; 5 list.append(1); 6 if list:print('True'); 7 else:print('False') 8 #result:True