Python:字典列表字符串方法測試


測試的一些Python中的關于字典、列表、字符串的使用方法,放在這里備查。
整個測試代碼和說明如下:

# -*- coding: utf-8 -*-
"""Python:函數中全是指針傳遞,而任何變量都是給予一個指針指向一個內存空間"""import types
import copy
import string###define _Debug  to enable assert__Debug__=1def my_format(x,y):"""par one: what des your printpar two: value"""x="-->"+x+" {}"print(x.format(y))returndef get_format(s,len):"""頭部分割"""len1 = int(len/2)print("%s %s %s" %("="*len1,s,"="*len1))returndef get_pointer(y,obj):"""獲得obj中每個元素的內存地址并且打印"""list_index=[]list_values=[]if __Debug__:assert isinstance(obj,list) or isinstance(obj,dict) or isinstance(obj,str)if isinstance(obj,list) or isinstance(obj,str):for i in range(len(obj)):list_index.append(i)list_values.append("0X{:x}".format(id(obj[i])))else: ##對于字典來講這樣取應該是不對的,但是可以用作測試key_list = list(obj.keys())for i in range(len(key_list)):list_index.append(key_list[i])list_values.append("0X{:x}".format(id(obj[key_list[i]])))y = "-->" + y + " {}"print(y.format(list(zip(list_index,list_values))))returndef convert_to_int(obj):"""將list中的字符也轉換為ASCII返回一個新list"""list_test_m=[]if __Debug__:assert isinstance(obj,list)for m in obj:if isinstance(m, str):list_test_m.append(ord(m))else:list_test_m.append(m)return list_test_mdef init_smaple(x):"""初始化測試數據"""if __Debug__:assert str(type(x))=="<class 'list'>"for i in  range(10):x.append(i)return tuple(x)sample_list=[]  #外部全局變量
sample_tupe= init_smaple(sample_list) #外部全局變量def main_dict(dict_test):"""測試的dict一些常用的方法:
1、字典的鍵不可能相同
2、字典是hash算法KEY-VALUES的數據結構
3、字典沒有順序,這是hash算法注定的
4、查找時間復雜度O(1)
5、不能進行切片操作"""#part1:最簡單的更改和增加字典的方式dict_test['gaopeng_4'] = '44'dict_test['gaopeng_5'] = '44'dict_test['gaopeng_5'] = '55'dict_test['gaopeng_5'] = '0' #修改my_format ("part1:simple add modify value",dict_test)#part2:setdefault:會返回原有的值 并且更新 如果沒有則設置一個,返回老值 注意和get的區別 part10 get不會更新只能設置返回的值dict_test.setdefault("gaopeng_1",'N/A')dict_test.setdefault("gaopeng_2",'N/A')my_format ("part2:setdefault:",dict_test.setdefault("gaopeng_2",'33')) ##更改my_format ("part2:setdefault:",dict_test)#part3:update:根據一個字典更改或者增加到另外一個字典,鍵相同則更新,不相同則增加dict_test_1 = {'gaopeng_1':'18','gaopeng_3':'N/A'}dict_test.update(dict_test_1)my_format ("part3:update:",dict_test)#part4:values:通過字典的values返回一個字典視圖dict_viewmy_format ("part4:values:",dict_test.values())dict_test_v_list= list(dict_test.values())my_format ("part4:values::dict_view:",dict_test_v_list)#part5:keys:通過字典的keys返回一個字典視圖my_format ("part5:keys:",dict_test.keys())dict_test_k_list= list(dict_test.keys())my_format ("part5:keys:dict_view:",dict_test_k_list)# part6:items:通過字典的返回一個字典視圖,其值為key-values p61 需要轉換為listdict_test_k_v_list = list(dict_test.items())my_format ("part6:items:", dict_test.items())my_format ("part6:items:dict_view:", dict_test_k_v_list)#part7:pop:根據鍵值刪除values 返回刪除的valuesmy_format("part7:pop:刪除前", dict_test)my_format ("part7:pop:刪除",dict_test.pop('gaopeng_1'))my_format("part7:pop:刪除后", dict_test)#part8:popitem:隨機彈出一個元素 返回刪除的key-valuesdict_test_copy = dict_test.copy() #一層淺拷貝即可for i in range(len(dict_test)):my_format ("part8:popitem:", dict_test_copy.popitem())#part9:fromkeys;根據一個字典的keys創建另外一個字典,值為None可以認為指定my_format ("part9:fromkeys;",  {}.fromkeys(dict_test))my_format ("part9:fromkeys;",  dict.fromkeys(dict_test,'NULL'))#part10:get 通過key訪問字典數據,但是如果沒有不會報錯,也不會像setdefault一樣設置一個,沒有則#返回None,也可以自己指定返回值my_format ("part10:當前字典為:", dict_test)my_format ("part10:get:", dict_test.get("gaopeng_10"))my_format ("part10:get:", dict_test.get("gaopeng_10",'NULL'))my_format ("part10:get:", dict_test.get("gaopeng_5"))#part11:clear python引用(指針)傳遞,clear用于同時清理變量但是 是一個指針指向的內存空間的值#dict_test__c = dict_test.copy()dict_test__c = dict_testif dict_test__c is dict_test:my_format ("part11:他們是同樣地址指向的字典如果要清理使用clear方法:", str(id(dict_test__c))+"=="+str(id(dict_test)))dict_test.clear()my_format ("part11:clear:", dict_test__c)my_format ("part11:clear:", dict_test)else:my_format ("part11:Diff addr not use clear:", str(id(dict_test__c)) + "==" + str(id(dict_test)))#part12:format_map 這種方法是字符串的方法用于通過字典的key 進行映射到 values 替換到字符串str_m = """name is  {name} age is {age}""".format_map({"name":"gaopeng","age":22})my_format("part12:format_map:", str_m)returndef main_list(list_test):"""測試的list一些常用的方法:
1、list類似于鏈表數據結構
2、感覺查找的時間復雜度為O(n)
3、list可以進行頭插尾插,按照索引插入,這是鏈表數據結構決定的
4、list可以進行頭刪尾刪,按照索引刪除,這是鏈表數據結構決定的"""#part0:init list:for i in range(97,110):if i%2 == 0:list_test.append(chr(i))else:list_test.append(i)else:my_format("part0:init list:",list_test)#part1:list:常用于將字符串按字符分割為一個list 當然也有其他作用my_format("part1:list:", list("test"))if list({"test1":1,"test2":2,"test3":3}) == list({"test1": 1, "test2": 2, "test3": 3}.keys()): #其他作用舉例my_format("part1:list:", "The same!!")#part2:修改列表的方式也非常簡單找到下標修改即可,但是不能修改沒有的值,這點和字典不一樣,字典試圖修改沒有的值則新加入一個元素list_test[0]=110my_format("part2:", list_test)#part3:list切片#1、左邊包含右邊不包含#2、并且切片可以賦值,也就是說改變元素內存的位置#3、切片左邊不能大于等于右邊否則返回為空#4、左邊不填值表示最開頭,最開頭可以用0代替,右邊不填值表示最末尾,無法代替#5、切片可以指定步長 如果為正數則左到右開始計數步長 如果是負數則改變默認的取值方式# EXP:[-3::-2]  [110, 'b', 99, 'd', 101, 'f', 103, 'h', 105, 'j', 107, 'l', 109]#          結尾<------------------------[2]----------------------*開頭my_format("part3:init list:", list_test)my_format("part3:list切片", list_test[0:2])my_format("part3:list負數切片", list_test[-3:-2]) #只包含倒數第三個元素my_format("part3:list空切片", list_test[-3:-3])  # 為空測試my_format("part3:list最后3個元素", list_test[-3:])  # 最后3個元素my_format("part3:list最后3個元素步[正]長為2", list_test[-3::2])  # 最后3個元素my_format("part3:list最后3個元素步[負]長為2", list_test[-3::-2])  #改變順序my_format("part3:list切片賦值內存位置改變:改變前的位置", [ id(x) for x in list_test ])list_test[:2] = [112,'z'] #此處會改變list_test[0] list_test[1] 的引入指向(指針指向)my_format("part3:list切片賦值內存位置改變:改變后的位置", [id(x) for x in list_test])m_test = list_test[:2]my_format("part3:list切片后給予新對象地址是同一地址", [id(x) for x in m_test])m_test[1] =900my_format("part3:修改切片是否修改源數據[不修改基礎對象賦值重新給予內存空間]", list_test)#part4:None列表:None 類似 C 語言中*p=NULL及空指針但是它是指向了一個固定的全局區? list中可以用于建立一個N個元素但是元素沒有指向的空列表test = [None] * 10test[1] = 'I' #注意這里我修改了這個值明顯這里內存指向會重新指向my_format("part4:None列表:None初始化后", test)my_format("part4:None列表:本機指向:", "0X{:x}".format(id(None)))for i in test:my_format("part4:None列表:每個元素的地址打印:", "0X{:x}".format(id(i)))#part5:刪除元素:使用del可以刪除list中的一個元素,引用(指針)刪除list_test_m=[]for m in list_test:if isinstance(m,int):list_test_m.append(str(m))else:list_test_m.append(m)print(list_test_m)x=list_testmy_format("part5:刪除元素:刪除前", "+".join(list_test_m)+" "+"addr: "+str(id(list_test)))del list_test[2]list_test_m.clear()for m in list_test:if isinstance(m,int):list_test_m.append(str(m))else:list_test_m.append(m)my_format("part5:刪除元素:刪除后","+".join(list_test_m)+" "+"addr: "+str(id(list_test)))my_format("part5:刪除元素:",x)#part6:刪除元素:pop也用于刪除列表中的一個元素 默認刪除尾部最后一個可以指定INDEX 并且返回刪除的列表值my_format("part6:刪除元素:刪除前",list_test)my_format("part6:刪除元素尾部:", list_test.pop())my_format("part6:刪除元素指定INDEX:", list_test.pop(0))my_format("part6:刪除元素:刪除后", list_test)#part7:刪除元素:remove用于刪除列表中的一個元素 通過指定值來刪除[第一個]是指定值的元素my_format("part7:刪除元素:刪除前", list_test)list_test.remove(101)my_format("part7:刪除元素:刪除后", list_test)#part8:反轉元素:有兩種方法一種使用reverse方法另外一種使用切換步長 reverse方法是用反轉的后的元素內容覆蓋原來地址#但是方法二則是一個匿名的內存空間切片方式my_format("part8:反轉元素:初始列表", list_test)my_format("part8:反轉元素:內存地址", id(list_test))list_test.reverse()my_format("part8:反轉元素:反轉后", list_test)my_format("part8:反轉元素:內存地址", id(list_test))list_test.reverse()my_format("part8:反轉元素:反轉后", list_test[::-1])#part9:增加一個元素:使用append可以為列表在末尾增加一個元素my_format("part9:增加一個元素:初始列表", list_test)list_test.append(100)my_format("part9:增加一個元素:使用append可以為列表在末尾增加一個元素", list_test)#part10:clear:使用clear會清空整個列表和字典一樣即便有多個指向同一個列表的變量指針全部清空list_test_c1 = list_test.copy()list_test_c2 = list_test_c1my_format("part10:clear:清空前", list_test_c1)my_format("part10:clear:清空前", list_test_c2)my_format("part10:clear:清空前", list_test)list_test_c2.clear()my_format("part10:clear:清空后", list_test_c1)my_format("part10:clear:清空后", list_test_c2)my_format("part10:clear:清空后原始列表", list_test)#part11:copy:使用copy用于將列表復制第一層引用(指針),如果需要完全深復制到最后一層指針使用deepcopymy_format("part11:copy:原始列表內存地址", id(list_test))get_pointer("part11:copy:原始列表各元素內存地址",list_test)list_test_copy=list_test.copy()my_format("part11:copy:copy列表內存地址", id(list_test_copy))get_pointer("part11:copy:copy列表各元素內存地址", list_test_copy)list_test_deepcopy=copy.deepcopy(list_test)my_format("part11:copy:deepcopy列表內存地址", id(list_test_deepcopy))get_pointer("part11:copy:deepcopy列表各元素內存地址", list_test_deepcopy)#part12:count:count用于計算元素在列表中出現的次數 返回次數list_test.append(100) #再增加一個100my_format("part12:count:count用于計算元素在列表中出現的次數 返回次數",list_test.count(100))#part13:擴展一個list有三種方法:#方法1:使用extend,原list地址不變#方法2:使用切片賦值,原list地址不變#方法3:使用list加法,生成一個新的listlist_test.extend([990,991,992])my_format("part13:擴展:方法1:使用extend,原list地址不變", list_test)list_test[len(list_test):] = [993,994,995]my_format("part13:擴展:方法2:使用切片賦值,原list地址不變", list_test)tmp_list_test_ =  list_test+[996,997,998]my_format("part13:擴展:方法3:使用list加法,生成一個新的list", tmp_list_test_)#part14:index:index用于查找指定值第一次出現的索引 返回位置my_format("part14:index:index用于查找指定值第一次出現的索引 返回位置", list_test.index(103))my_format("part14:index:index用于查找指定值第一次出現的索引 返回位置", list_test.index(100))#part15:insert:insert用于在指定index后增加一個值 無返回#part15:insert:同時也可以用切片賦值的方式代替get_pointer("part15:insert:原有內存地址", list_test)list_test.insert(list_test.index(103), 99998)my_format("part15:insert:insert用于在指定index后增加一個值 無返回", list_test)list_test[list_test.index(103):list_test.index(103)] = [99999]my_format("part15:insert:同時也可以用切片賦值的方式代替", list_test)get_pointer("part15:insert:原有內存地址未變化", list_test)#part16:sort:對列表進行排序,在原有列表上修改 并不是返回新的副本 但是如果列表有不同類型元素 排序將不能進行#從內存地址的變化,就像是鏈表排序后NEXT指針重新指向,也就是改變的只是NEXT指針而已#也可以使用sorted函數返回一個副本tmp_list_test_2 = convert_to_int(list_test)[:3]tmp_list_test_2.append(1)my_format("part16:sort:原始列表", tmp_list_test_2)my_format("part16:sort:原始列表內存", id(tmp_list_test_2))get_pointer("part16:sort:原有內存地址變化", tmp_list_test_2)tmp_list_test_2.sort()my_format("part16:sort:對列表進行排序,在原有列表上修改", tmp_list_test_2)get_pointer("part16:sort:原有內存地址未變化", tmp_list_test_2)my_format("part16:sort:排除后列表內存", id(tmp_list_test_2))#part17:sorted:本方法可用用于排序很多類型,返回一個副本,但是元素的內存指針未變(python中都是這種方法節約內存)tmp_list_test_3 = convert_to_int(list_test)[:3]tmp_list_test_3.append(1)my_format("part17:sorted:原始列表", tmp_list_test_3)my_format("part17:sorted:原始列表內存", id(tmp_list_test_3))get_pointer("part17:sorted:原有內存地址變化", tmp_list_test_3)tmp_list_test_4 = sorted(tmp_list_test_3)my_format("part17:sorted:對列表進行排序,新返回副本", tmp_list_test_4)get_pointer("part17:sorted:副本元素內存地址未變化", tmp_list_test_4)my_format("part17:sorted:排除后副本列表內存改變", id(tmp_list_test_4))#part18:高級排序;sort和sorted接受兩個關鍵字參數key和reverse#key:根據自定義的函數返回一個值作為排序的基礎,類似一個函數指針#reverse:是否反向def sort_key(obj):if isinstance(obj,str):obj = ord(obj) % 10else:obj = obj%10return objmy_format("part18:sort高級排序;原始列表", list_test)list_test.sort(key=sort_key,reverse=1)my_format("part18:sort高級排序;排序后列表", list_test)list_test_1 = sorted(list_test,key=sort_key,reverse=1)my_format("part18:sorted高級排序;排序后列表", list_test_1)def main_str():"""測試的str一些常用的方法
1、字符串本身是不能修改的,實際上PYTHON簡單對象(非list dict)都不能更改,只能重新賦予指向內存空間
2、在C中使用char a[10]或者 char* a=(char*)malloc(10*sizeof(char))來做,類似一個數組區別只是在棧還是堆空間,
python的str卻是不可修改的
3、分片方法同樣適用于字符串
"""#初始化測試字符串str_test = string.digits[::3]+string.ascii_lowercase[0:10:2]#part1:一些有用的string方法,返回新的字符串:my_format("part1:一些有用的string方法:string.digits 返回0-9的字符串:", string.digits)my_format("part1:一些有用的string方法:ascii_lowercase 返回a-z小寫的字符串:", string.ascii_lowercase)my_format("part1:一些有用的string方法:ascii_uppercase 返回A-Z小寫的字符串:", string.ascii_uppercase)my_format("part1:一些有用的string方法:printable 返回ASCII中可以打印的字符:", string.printable)my_format("part1:一些有用的string方法:punctuation 返回ASCII中可以標點字符:", string.punctuation)#part2:center:在讓指定字符串居中兩邊填充字符(默認為空格),返回新的字符串#類似的還有#ljust:左邊頂格右邊填充#rjust:右邊頂格左邊填充str_test_center_20 = str_test.center(len(str_test) + 20, '=')my_format("part2:center:在讓指定字符串居中兩邊填充字符(默認為空格),返回新的字符串:",str_test.center(len(str_test)+20,'='))my_format("part2:ljust:在讓指定字符串居中兩邊填充字符(默認為空格),返回新的字符串:", str_test.ljust(len(str_test) + 20,'='))my_format("part2:rjust:在讓指定字符串居中兩邊填充字符(默認為空格),返回新的字符串:", str_test.rjust(len(str_test) + 20,'='))#part3:find:查找字符串中的字符,找到第一個符合的其所在的index,沒找到返回-1 可以指定起點終點 包含起點不包含終點#類似的還有# index:和find類似但是沒有找到出現異常# rfind:和find類似但是最后一個符合的# rindex:和rfind類似但是沒有知道出現異常# count:指定字符串出現的次數,返回次數# startswith:是否已某個字符串開頭返回bool類型# endswith:是否已某個字符串結尾返回bool類型my_format("part3:find:初始化字符串:", str_test)my_format("part3:find:查找字符串中的字符:", str_test.find('a'))my_format("part3:find:查找字符串中的字符:", str_test.find('go'))my_format("part3:find:查找字符串中的字符:", str_test.find('a',2,6))my_format("part3:index:查找字符串中的字符:", str_test.index('a', 2, 6))# part4:split:其作用和join相反,用于將字符串按照指定分隔符分割為一個list,返回一個序列tmp_list=list(str_test)tmp_str='+'.join(tmp_list) ##初始化測試tmp_strstr_test=tmp_strmy_format("part4:split:字符初始化", str_test)tmp_list=str_test.split('+')my_format("part4:split:按照'+'號進行分割為一個list", tmp_list)#part5:join:其作用和split相反,用于按照指定的分割符合并字符元素的list為一個字符串,返回一個新的字符串my_format("part5:join:列表初始化", tmp_list)str_test = "".join(tmp_list)my_format("part5:join:沒有指定分隔符的合并", str_test)my_format("part5:join:指定'='分隔符的合并", "=".join(tmp_list))#part6:strip:刪除開頭和結尾的某些字符,默認不指定為空格,返回一個新的字符串,注意只是匹配末尾#類似的還有#lstrip:刪除左邊#rstrip:刪除右邊str_test_center_26=str_test_center_20.center(len(str_test_center_20)+6,' ')str_test_center_32 = str_test_center_26.center(len(str_test_center_26) + 6, '*')my_format("part6:strip:初始化字符串為:", str_test_center_32)my_format("part6:strip:去掉符號和空格字符后:", str_test_center_32.strip(' =*')) #匹配多種字符my_format("part6:strip:去掉星號和空格字符后:", str_test_center_32.strip('*').strip())my_format("part6:lstrip:去掉符號和空格字符后:", str_test_center_32.lstrip(' =*'))my_format("part6:rstrip:去掉符號和空格字符后:", str_test_center_32.rstrip(' =*'))#part7:replace:將字符串的某些子字符串進行替換為另外的字符串,返回一個新的字符串my_format("part7:replace:初始化字符串為:", str_test_center_32)my_format("part7:replace:替換空格為+號", str_test_center_32.replace(' ','+'))#part8:translate:需要建立映射字典表,單個字符匹配進行替換,效率高于replace?#映射還是非常有用的my_format("part8:translate:初始化字符串為:", str_test_center_32)table=str_test_center_32.maketrans('a ','A+')my_format("part8:translate:映射字典為:", table) ##只做映射?my_format("part8:translate:替換后:",str_test_center_32.translate(table))str_test_center_32=str_test_center_32.translate(table)#part9:upper lower:字母全部大寫或者小寫返回一個新字符串my_format("part9:upper lower:初始化字符串為:", str_test_center_32)my_format("part9:upper lower:lower:", str_test_center_32.lower())my_format("part9:upper lower:upper:", str_test_center_32.upper())#part10:swapcase:將字符串中字母大小寫反轉 返回一個新的字符串my_format("part10:swapcase:初始化字符串為:", str_test_center_32)my_format("part10:swapcase:反轉后:", str_test_center_32.swapcase())#part11:字符串反轉 使用切片步長返回一個新的字符串my_format("part11:swapcase:初始化字符串為:", str_test_center_32)my_format("part11:swapcase:初始化字符串為:", str_test_center_32[::-1])#part12:swapcase將字符串第一個字母大寫 返回一個新字符串#title:字符串每個單詞的首字符大寫 返回一個新的字符串my_format("part12:swapcase:初始化字符串為:", 'abcdef oooo')my_format("part12:swapcase:首字母大寫后:", 'abcdef oooo'.capitalize())my_format("part12:title:首字母大寫后:", 'abcdef oooo'.title())#part12:各種字符串檢查# startswith:是否已某個字符串開頭# endswith:是否已某個字符串結尾# isalnum:是否字符串的所有的字符都是數字或者字母# isalpha:是否字符串的所有的字符都是字母# isdecimal:是否所有字符都是十進制數字# isdigit:是否所有字符都是數字# isnumeric:是否字符串中字符都是數字字符"""來自網上的區別isdigit()True: Unicode數字,byte數字(單字節),全角數字(雙字節),羅馬數字False: 漢字數字Error: 無  isnumeric()True: Unicode數字,全角數字(雙字節),羅馬數字,漢字數字False: 無Error: byte數字(單字節)"""# islower:是否所有字母都是小寫# isupper:是否所有字母都是大寫# isspace:是否字符串中的字符都是空白字符?(制表符 空格等?)my_format("part12:各種字符串檢查:初始化字符串為:", str_test)my_format("part12:各種字符串檢查:startswith:是否已某個字符串開頭:", str_test.startswith('**'))my_format("part12:各種字符串檢查:endswith:是否已某個字符串結尾:", str_test.endswith('-*'))my_format("part12:各種字符串檢查:isalnum:是否字符串的所有的字符都是數字或者字母:", str_test.isalnum())my_format("part12:各種字符串檢查:isalpha:是否字符串的所有的字符都是字母:", str_test.isalpha())my_format("part12:各種字符串檢查:isdecimal:是否所有字符都是十進制數字:", str_test.isdecimal())my_format("part12:各種字符串檢查:isdigit:是否所有字符都是十進制數字:", str_test.isdigit())my_format("part12:各種字符串檢查:isnumeric:是否字符串中字符都是數字字符:", str_test.isnumeric())my_format("part12:各種字符串檢查:islower:是否所有字母都是小寫:", str_test.islower())my_format("part12:各種字符串檢查:isupper:是否所有字母都是小寫:", str_test.isupper())my_format("part12:各種字符串檢查:isspace:是否字符串中的字符都是空白字符?:", '     '.isspace())#part13:字符串格式匯總#常用字符串格式#b 二進制數字#d 將整數作為十進制數字進行處理 默認設置#e 科學計數法表示小數(e表示指數)#E 同e 但用E表示指數#f 將小數表示為定點小數#F 同f 但是對于nan inf使用大寫表示#o 整數使用八進制#s 保持字符串的格式不變 默認設置#x 將整數表示為16進制使用小寫字母#X 同x 但是使用大寫字母#% 顯示百分比 及乘以100 用f表示然后加上%符號#測試字符初始化temp_inter = -100temp_inter_1 = 100temp_float = 93.33633temp_str   = 'abcdef'#字符串格式化方法1my_format("part13:字符串格式匯總:字符串格式化方法1", "this is test %s %x %o"%(temp_str,temp_inter,temp_inter))# 字符串格式化方法2 這里使用位置作為映射,默認就是0 1 2 3的方式所以可以不要 #號是一個自適應的前綴轉換符 非常有用my_format("part13:字符串格式匯總:字符串格式化方法2 #號是一個自適應的前綴轉換符","this is test {0:s} {1:#x} {2:#o} {3:#b} {4:%} {5:f}".format(temp_str,temp_inter,temp_inter,temp_inter,temp_float,temp_float))#字符串格式化方法3 這里使用關鍵字作為映射my_format("part13:字符串格式匯總:字符串格式化方法3","this is test {temp_str_ad:s} {temp_inter_ad1:x} {temp_inter_ad2:o} {temp_inter_ad3:b} {temp_float_ad1:%} {temp_float_ad2:f}".format(temp_str_ad=temp_str,temp_inter_ad1=temp_inter,temp_inter_ad2=temp_inter,temp_inter_ad3=temp_inter,temp_float_ad1=temp_float,temp_float_ad2=temp_float))#對于格式2和格式3可以使用更加詳細的格式化#左對齊/居中/右對齊: <  , ^  , >my_format("part13:字符串格式匯總:左對齊/居中/右對齊","this is test \n{:<50s}\n{:^50s}\n{:>50s}".format(temp_str,temp_str,temp_str))#使用填充符號進行填充my_format("part13:字符串格式匯總:使用填充符號進行填充","this is test \n{:*<50s}\n{:*^50s}\n{:*>50s}".format(temp_str, temp_str, temp_str))#對于一個整數的大部分格式# +(用+填充) ^(居中) +(顯示符號) #(自適應轉換) 50(精度) x(16進制顯示)my_format("part13:字符串格式匯總:對于一個整數的大部分格式:","this is test {:+^+#50x}".format(temp_inter_1))# =(將符號左邊頂格) +(顯示符號) #(自適應轉換) 50(精度) x(16進制顯示)my_format("part13:字符串格式匯總:對于一個整數的大部分格式:", "this is test {:=+#50x}".format(temp_inter_1))#對于一個小數的大部分格式# *(用*填充) ^(居中) +(顯示符號) #(自適應轉換) 50(精度) .2(刻度) f(float顯示)my_format("part13:字符串格式匯總:對于一個小數的大部分格式:", "this is test {:*^+#50.2f}".format(temp_float))def main_normal():# part0:序列(list和tuple都支持乘法加法),字符串也支持 返回一個新的對象my_format("part0:序列:元組初始化",sample_tupe)my_format("part0:序列:列表初始化",sample_list)my_format("part0:序列:元組相加", sample_tupe+sample_tupe)my_format("part0:序列:元組相乘", sample_tupe*2)my_format("part0:序列:列表相加", sample_list+sample_list)my_format("part0:序列:列表相乘", sample_list*2)my_format("part0:序列:字符串相加", 'AAAA'+'aaaa')my_format("part0:序列:字符串相乘", 'AAAA'*2)#part1:內存地址測試temp_chr = 'abcde'temp_list = list(temp_chr)temp_dict = {}for i in range(len(temp_list)):temp_dict.setdefault( temp_list[i],i)my_format("part1:內存地址測試:初始化的字符串:", temp_chr)temp_chr_1 = temp_chrmy_format("part1:內存地址測試:字符串自身內存地址:", "0X{:x}".format(id(temp_chr)))my_format("part1:內存地址測試:temp_chr_1 = temp_chr內存地址:", "0X{:x}".format(id(temp_chr_1)))get_pointer("part1:內存地址測試:字符串各個字符內存地址?:", temp_chr)my_format("part1:內存地址測試:一個字符內存地址?:","0X{:x}".format(id(temp_chr[0])))my_format("part1:內存地址測試:初始化的列表:", temp_list)temp_list_1 = temp_listmy_format("part1:內存地址測試:list自身內存地址:", "0X{:x}".format(id(temp_list)))my_format("part1:內存地址測試:temp_list_1 = temp_list內存地址:", "0X{:x}".format(id(temp_list_1)))get_pointer("part1:內存地址測試:list各個元素內存地址:", temp_list)my_format("part1:內存地址測試:一個list元素內存地址:", "0X{:x}".format(id(temp_list[0])))my_format("part1:內存地址測試:初始化的字典:", temp_dict)temp_dict_1 = temp_dictmy_format("part1:內存地址測試:dict自身內存地址:", "0X{:x}".format(id(temp_dict)))my_format("part1:內存地址測試:temp_dict_1 = temp_dict內存地址:", "0X{:x}".format(id(temp_dict_1)))get_pointer("part1:內存地址測試:dict各個values內存地址:", temp_dict)my_format("part1:內存地址測試:一個dict values內存地址:", "0X{:x}".format(id(temp_dict['a'])))temp_dict2 = temp_dict.copy()temp_dict_3 = copy.deepcopy(temp_dict)get_pointer("part1:內存地址測試:dict.copy各個values內存地址:", temp_dict2)get_pointer("part1:內存地址測試:copy.deepcopy各個values內存地址:", temp_dict_3)returndef main():get_format("dict test",100)print(main_dict.__doc__)dict_test={}main_dict(dict_test)get_format("list test",100)print(main_list.__doc__)list_test=[]main_list(list_test)get_format("charset test", 100)print(main_str.__doc__)main_str()get_format("normal test", 100)main_normal()return##begin
if __name__ == '__main__':main()

作者微信:
微信.jpg

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/452388.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/452388.shtml
英文地址,請注明出處:http://en.pswp.cn/news/452388.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

什么是確認測試

確認測試也稱為驗收測試&#xff0c;它的目標是驗證軟件的有效性。 通常&#xff0c;驗證指的是保證軟件正確地實現了某個特定要求的一系列活動&#xff1b;確認指的是為了保證軟件確實滿足了用戶需求而進行的一系列活動。 軟件有效性的一個簡單定義是&#xff1a;如果軟件的功…

Diango博客--17.統計各個分類和標簽下的文章數

文章目錄0.思路引導1.Model 回顧2.數據庫數據聚合3.使用 Annotate4.在模板中引用新增的屬性0.思路引導 在我們的博客側邊欄有分類列表和標簽列表&#xff0c;顯示博客已有的全部文章分類。現在想在分類名和標簽名后顯示該分類或者標簽下有多少篇文章&#xff0c;該怎么做呢&am…

HTTP協議中request報文請求方法和狀態響應碼

一個HTTP請求報文由4部分組成&#xff1a; 請求行&#xff08;request line&#xff09;請求頭部&#xff08;header&#xff09;空行請求數據下圖給出了請求報文的一般格式&#xff1a; 請求行中包括了請求方法&#xff0c;常見的請求方法有&#xff1a; GET&#xff1a;從服務…

計算機無法安裝64位操作系統,為什么我的win7旗艦版service Pack 1 64位操作系統 無法安裝(KB2670838) 這個更新...

您好&#xff01;我了解到您遇到關于這邊的問題請問無法安裝是否出現了什么錯誤代碼提示呢&#xff1f;請參考以下的步驟解決按WindowsR打開cmd里使用下面的命令關閉相關的一些服務&#xff1a;net stop wuauservnet stop cryptSvcnet stop bitsnet stop msiserver完成后&#…

bmon:一個強大的網絡帶寬監視和調試工具

bmon 是類 Unix 系統中一個基于文本&#xff0c;簡單但非常強大的 網絡監視和調試工具&#xff0c;它能抓取網絡相關統計信息并把它們以用戶友好的格式展現出來。它是一個可靠高效的帶寬監視和網速估測工具。 它能使用各種輸入模塊讀取輸入&#xff0c;并以各種輸出模式顯示輸出…

函數的調用規則(__cdecl,__stdcall,__fastcall,__pascal)

關于函數的調用規則&#xff08;調用約定&#xff09;&#xff0c;大多數時候是不需要了解的&#xff0c;但是如果需要跨語言的編程&#xff0c;比如VC寫的dll要delphi調用&#xff0c;則需要了解。 microsoft的vc默認的是__cdecl方式&#xff0c;而windows API則是__stdcall&a…

Linux 下的/usr/bin /usr/sbin /usr/local/bin /usr/local/sbin區別

1、/bin 是所有用戶都可以訪問并執行的可執行程序。包括超級用戶及一般用戶。 供所有用戶&#xff08;包括root用戶和一般用戶&#xff09;使用的基本命令&#xff0c;主要有cat,chmod,date,cp,bash等等常用的命令。 2、/usr/bin&#xff1a;系統預裝的可執行程序&#xff0c;…

alpha測試和betal測試

如果一個軟件是為許多客戶開發的&#xff08;例如&#xff0c;向大眾公開出售的盒裝軟件產品&#xff09;&#xff0c;那么絕大多數軟件開發商都使用被稱為Alpha測試和Beta測試的過程&#xff0c;來發現那些看起來只有最終用戶才能發現的錯誤。 Alpha測試由用戶在開發者的場所進…

計算機d盤無法格式化,四種方法解決D盤無法格式化問題

不少朋友系統出現故障&#xff0c;幾乎都是選擇重裝系統的方法來解決問題。系統重裝后&#xff0c;不少朋友覺得D盤沒有什么重要的東西&#xff0c;就想將其格式化&#xff0c;可是系統出現windows無法格式該驅動器的提示&#xff0c;這是怎么回事呢&#xff1f;D盤無法格式化要…

sqlserver視圖

作用 ①簡化了操作&#xff0c;把經常使用的數據定義為視圖。 ②安全性&#xff0c;用戶只能查詢和修改能看到的數據。 ③邏輯上的獨立性&#xff0c;屏蔽了真實表的結構帶來的影響。 對視圖的修改&#xff1a;單表視圖一般用于查詢和修改&#xff0c;會改變基本表的數據&#…

非root用戶sudo_ssh免密鑰

非root用戶sudo_ssh免密鑰 目標&#xff1a;從服務器上ssh登陸后sudo免密鑰執行相應的命令 環境介紹&#xff1a; 192.168.65.128 my1-222 192.168.65.129 my2-223 192.168.65.130 web224# 步驟一&#xff1a; # 每個節點執行(不是必須&#xff0c;但是建議這樣做) cat …

復旦大學和吉大計算機考研選哪個,2016復旦大學VS吉林大學 基于排名角度的比較?...

復旦大學好還是吉林大學好首先&#xff0c;2016復旦大學好還是吉林大學好是近日問得較多的問題之一&#xff0c;小伙伴們有的從吉林大學和復旦大學的地理位置優勢角度進行分析&#xff0c;有的從吉林大學和復旦大學的強勢專業角度進行PK&#xff0c;有的甚至從吉林大學和復旦大…

Diango博客--18.使用 Fabric 自動化部署 Django 項目

文章目錄1.本地安裝 Fabric2.部署過程回顧3.完善項目配置4.修改 BASE_DIR 配置項5.設置 Supervisor 環境變量6.編寫 Fabric 腳本7.執行 Fabric 自動部署腳本1.本地安裝 Fabric $ pipenv install fabric --dev 報錯&#xff1a; An error occurred while installing django-pure…

白盒測試詳解

通常把測試數據和預期的輸出結果稱為測試用例。 &#xff08;一&#xff09;邏輯覆蓋 是對一系列測試過程的總稱&#xff0c;這組測試過程逐漸進行越來越完整的通路測試。 1.語句覆蓋 語句覆蓋的含義是&#xff0c;選擇足夠多的測試數據&#xff0c;使被測程序中每個語句至少…

git 的安裝以及使用:是一個開源的分布式版本控制系統,可以對項目進行版本管理。 早期是linux之父用來管理linux系統源代碼的(linux是和windows一樣操作系統 開源免費的操作...

## 總結 - 學會使用基本的git命令 管理源代碼- 學會去github創建倉庫 并將代碼上傳到github的倉庫 (有待完成 回家有網再push)- 了解本地的.git和服務器github的.git的關系 是兩個倉庫 一個是本地 一個是服務器 需要使用一句命令來關聯 git remote add origin 遠端的.git的地址…

自斷前程,未來80%IT工作將實現自動化

技術人員革了自己的命&#xff1f; 在上周的Structure大會上&#xff0c;硅谷著名風險投資人、億萬富翁唯諾德稱未來80%的IT工作都將被AI系統所替代。不過&#xff0c;唯諾德好像并不擔心&#xff0c;相反他補充了一句“我覺得很興奮。”唯諾德表示自己并非危言聳聽&#xff0c…

2013計算機二級試題,2013全國計算機二級上機考試試題46-100套試題

*第46 套上機考試試題一、基本操作題**請根據以下各小題的要求設計Visual Basic 應用程序(包括界面和代碼)。(1)在名稱為Form1、標題為“框架練習”的窗體上*畫一個名稱為Frame1、標題為“字體”的框架控件&#xff1b;在框架中畫兩個單選按鈕&#xff0c;名稱分別為Option1、O…

黑盒測試詳解

黑盒測試著重測試軟件功能。黑盒測試并不能取代白盒測試&#xff0c;它是與白盒測試互補的測試方法&#xff0c;它很可能發現白盒測試不易發現的其他類型的錯誤。白盒測試在測試過程的早期階段進行&#xff0c;而黑盒測試主要用于測試過程的后期。 黑盒測試力圖發現下述類型的…

linux中rpm、yum、apt-get的關系

Linux中yum和apt-get用法及區別 一般來說著名的linux系統基本上分兩大類&#xff1a; 1.RedHat系列&#xff1a;Redhat、Centos、Fedora等 2.Debian系列&#xff1a;Debian、Ubuntu等 RedHat 系列 1 常見的安裝包格式 rpm包,安裝rpm包的命令是“rpm -參數” 2 包管理工具 y…

echart事件

reference&#xff1a; https://www.w3cschool.cn/echarts_tutorial/echarts_tutorial-7o3u28yh.html var mychart echarts.init(document.getElementById(abc)); var option {....}//忽略了具體參數 mychart.on(click,function(params){console.log(params);console.log(para…