Python字典實(shí)現(xiàn)偽切片功能
故事是從這里開(kāi)始的…
早上起床看到一條評(píng)論,有點(diǎn)懵逼,字典切片?
查閱了一下Python資料,3.6版本的Python改寫(xiě)了dict的內(nèi)部算法,3.6版本之前是無(wú)序的;
So,現(xiàn)在就是有序的啦,注意的是這個(gè)順序是key的插入順序;
但字典雖有序沒(méi)下標(biāo)怎么切片?list列表?
那就把key放進(jìn)list里,利用list自身的截取方法切一下片!
再用截取后的key對(duì)新的字典賦值!
所以腦子一熱就寫(xiě)了個(gè)字典切片1.0版本
# 字典切片1.0版本 def dictcut(dict, start, end): # 臨時(shí)存放字典的key temp = list(dict.keys()) # 返回一個(gè)字典 result = {} #切列表里的key temp = temp[start:end] for i in range(len(temp)): #用切完后的key列表對(duì)新的字典賦值 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)
然后運(yùn)行結(jié)果
兩個(gè)數(shù)為正,而且不越界,正常截取
如果我想從2截取到末尾,末尾坐標(biāo)是-1,但傳[2 : -1]就會(huì)截取[2,-1),截取不到最后一個(gè);那如果傳[2 : 0]就會(huì)像下面一樣
所以list[2: ]截取從2到最后一個(gè),在傳參方面就顯得很麻煩了
如果截取[-5,-3]沒(méi)問(wèn)題,但是[-3,-5]就不行了
綜上,我希望我的字典切片可以三百六四度無(wú)死角切,從正到負(fù),從前到后,正著切,逆著切
所以字典切片2.0版本就登場(chǎng)了!
# 字典切片2.0 def dictcut(dict, start, end): # 臨時(shí)存放字典的key temp = list(dict.keys()) # 返回一個(gè)字典 result = {} # 分兩個(gè)分支 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同時(shí)為大于等于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同時(shí)小等于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同時(shí)為大于等于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同時(shí)大小等于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("切了個(gè)寂寞!") # 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("字典切割:左閉右開(kāi)") 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)
運(yùn)行結(jié)果:
若不在范圍
如果坐標(biāo)重疊,重疊切個(gè)寂寞哦?
360°無(wú)死角切切切(正常切)
正數(shù)
負(fù)數(shù)
360°無(wú)死角切切切(從后往前切)
正數(shù)
負(fù)數(shù)
2.0代碼比較繁瑣,但是字典切片的效果還是清晰可見(jiàn)的~~~
到此這篇關(guān)于Python字典實(shí)現(xiàn)偽切片功能的文章就介紹到這了,更多相關(guān)Python偽切片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python 如何讀取字典的所有鍵-值對(duì)
- python中必會(huì)的四大高級(jí)數(shù)據(jù)類(lèi)型(字符,元組,列表,字典)
- 淺談Python列表嵌套字典轉(zhuǎn)化的問(wèn)題
- python字典與json轉(zhuǎn)換的方法總結(jié)
- python字典按照value排序方法
- python3訪問(wèn)字典里的值實(shí)例方法
- 在Python中字典按值排序的實(shí)現(xiàn)方法
- python 獲取字典鍵值對(duì)的實(shí)現(xiàn)
- Python txt文件如何轉(zhuǎn)換成字典
- python munch庫(kù)的使用解析
相關(guān)文章
python利用os模塊編寫(xiě)文件復(fù)制功能——copy()函數(shù)用法
這篇文章主要介紹了python利用os模塊編寫(xiě)文件復(fù)制功能——copy()函數(shù)用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Python django框架應(yīng)用中實(shí)現(xiàn)獲取訪問(wèn)者ip地址示例
這篇文章主要介紹了Python django框架應(yīng)用中實(shí)現(xiàn)獲取訪問(wèn)者ip地址,涉及Python Request模塊相關(guān)函數(shù)使用技巧,需要的朋友可以參考下2019-05-05給大家整理了19個(gè)pythonic的編程習(xí)慣(小結(jié))
這篇文章主要介紹了給大家整理了19個(gè)pythonic的編程習(xí)慣(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Pandas使用分隔符或正則表達(dá)式將字符串拆分為多列
本文主要介紹了Pandas使用分隔符或正則表達(dá)式將字符串拆分為多列,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02解決Matplotlib圖表不能在Pycharm中顯示的問(wèn)題
今天小編就為大家分享一篇解決Matplotlib圖表不能在Pycharm中顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05