Python字典實現(xiàn)偽切片功能
故事是從這里開始的…
早上起床看到一條評論,有點懵逼,字典切片?
查閱了一下Python資料,3.6版本的Python改寫了dict的內(nèi)部算法,3.6版本之前是無序的;
So,現(xiàn)在就是有序的啦,注意的是這個順序是key的插入順序;
但字典雖有序沒下標(biāo)怎么切片?list列表?
那就把key放進list里,利用list自身的截取方法切一下片!
再用截取后的key對新的字典賦值!
所以腦子一熱就寫了個字典切片1.0版本
# 字典切片1.0版本 def dictcut(dict, start, end): # 臨時存放字典的key temp = list(dict.keys()) # 返回一個字典 result = {} #切列表里的key temp = temp[start:end] for i in range(len(temp)): #用切完后的key列表對新的字典賦值 result[temp[i]] = dict.get(temp[i]) return result #原字典 dict = {"zero": "0", "one": "1", "two": "2", "three": "3", "four": "4"} print("dict:", dict) start = eval(input("起始位置:")) end = eval(input("結(jié)束位置:")) #調(diào)用切片方法 newdict =dictcut(dict, start, end) print("dictcut(dict,%d,%d):" % (start, end), newdict)
然后運行結(jié)果
兩個數(shù)為正,而且不越界,正常截取
如果我想從2截取到末尾,末尾坐標(biāo)是-1,但傳[2 : -1]就會截取[2,-1),截取不到最后一個;那如果傳[2 : 0]就會像下面一樣
所以list[2: ]截取從2到最后一個,在傳參方面就顯得很麻煩了
如果截取[-5,-3]沒問題,但是[-3,-5]就不行了
綜上,我希望我的字典切片可以三百六四度無死角切,從正到負,從前到后,正著切,逆著切
所以字典切片2.0版本就登場了!
# 字典切片2.0 def dictcut(dict, start, end): # 臨時存放字典的key temp = list(dict.keys()) # 返回一個字典 result = {} # 分兩個分支 1.start和end在可切片范圍內(nèi) 2.不在范圍內(nèi) if start <= len(temp) - 1 and start >= -len(temp) and end <= len(temp) - 1 and end >= -len(temp): #start大于end,且下標(biāo)不重疊 if start > end and start - 1 != len(temp) + end: #start和end同時為大于等于0 if start >= 0 and end >= 0: # (4,2) 4 0 1 for i in range(start, len(temp)): result[temp[i]] = dict.get(temp[i]) for i in range(0, end): result[temp[i]] = dict.get(temp[i]) # start和end同時小等于0 if start <= 0 and end <= 0: # (-1,-3) 4 0 1 for i in range(len(temp) + start, len(temp)): result[temp[i]] = dict.get(temp[i]) for i in range(0, len(temp) + end): result[temp[i]] = dict.get(temp[i]) # start大于0,end小于0 if start >= 0 and end < 0: # (1,-2) 1 2 for i in range(start, len(temp) + end): result[temp[i]] = dict.get(temp[i]) # end大于start,且下標(biāo)不重疊 elif end > start and start + len(temp) != end - 1: # start和end同時為大于等于0 if start >= 0 and end >= 0: # (0,3) 0 1 2 for i in range(start, end): result[temp[i]] = dict.get(temp[i]) # start和end同時大小等于0 if start <= 0 and end <= 0: # (-4,-1) 1 2 3 for i in range(len(temp) + start, len(temp) + end): result[temp[i]] = dict.get(temp[i]) # end大等于0,start小于0 if end >= 0 and start < 0: # (-1,3) 4 0 1 2 for i in range(len(temp) + start, len(temp)): result[temp[i]] = dict.get(temp[i]) for i in range(end): result[temp[i]] = dict.get(temp[i]) #start等于end,或者下標(biāo)重疊 elif end == start or start + len(temp) == end - 1 or end <= 0 and start - 1 == len(temp) + end: print("切了個寂寞!") # start或者end不在范圍內(nèi) else: print("傳入?yún)?shù)有誤!") return result #原字典 dict = {"zero": "0", "one": "1", "two": "2", "three": "3", "four": "4"} print("dict:", dict) print("字典切割:左閉右開") start = eval(input("起始位置:")) end = eval(input("結(jié)束位置:")) newdict = dictcut(dict, start, end) # 如果返回的字典不為空,打印結(jié)果 if len(newdict) != 0: print("dictcut(dict,%d,%d):" % (start, end), newdict)
運行結(jié)果:
若不在范圍
如果坐標(biāo)重疊,重疊切個寂寞哦?
360°無死角切切切(正常切)
正數(shù)
負數(shù)
360°無死角切切切(從后往前切)
正數(shù)
負數(shù)
2.0代碼比較繁瑣,但是字典切片的效果還是清晰可見的~~~
到此這篇關(guān)于Python字典實現(xiàn)偽切片功能的文章就介紹到這了,更多相關(guān)Python偽切片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法
這篇文章主要介紹了python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python django框架應(yīng)用中實現(xiàn)獲取訪問者ip地址示例
這篇文章主要介紹了Python django框架應(yīng)用中實現(xiàn)獲取訪問者ip地址,涉及Python Request模塊相關(guān)函數(shù)使用技巧,需要的朋友可以參考下2019-05-05給大家整理了19個pythonic的編程習(xí)慣(小結(jié))
這篇文章主要介紹了給大家整理了19個pythonic的編程習(xí)慣(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09解決Matplotlib圖表不能在Pycharm中顯示的問題
今天小編就為大家分享一篇解決Matplotlib圖表不能在Pycharm中顯示的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05