Python字典實現(xiàn)偽切片功能
故事是從這里開始的…

早上起床看到一條評論,有點懵逼,字典切片?
查閱了一下Python資料,3.6版本的Python改寫了dict的內(nèi)部算法,3.6版本之前是無序的;
So,現(xiàn)在就是有序的啦,注意的是這個順序是key的插入順序;
但字典雖有序沒下標怎么切片?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截取到末尾,末尾坐標是-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,且下標不重疊
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,且下標不重疊
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,或者下標重疊
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é)果:
若不在范圍

如果坐標重疊,重疊切個寂寞哦?

360°無死角切切切(正常切)
正數(shù)

負數(shù)

360°無死角切切切(從后往前切)
正數(shù)

負數(shù)

2.0代碼比較繁瑣,但是字典切片的效果還是清晰可見的~~~
到此這篇關(guān)于Python字典實現(xiàn)偽切片功能的文章就介紹到這了,更多相關(guān)Python偽切片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)批量下載SMAP數(shù)據(jù)
在科學(xué)研究和數(shù)據(jù)分析中,獲取大規(guī)模的遙感數(shù)據(jù)是一個常見的任務(wù),本文將詳細為大家介紹如何利用Python實現(xiàn)SMAP數(shù)據(jù)的批量下載,需要的可以參考下2023-12-12
python實現(xiàn)音樂播放器 python實現(xiàn)花框音樂盒子
這篇文章主要為大家詳細介紹了python實現(xiàn)音樂播放器,實現(xiàn)花框音樂盒子,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02
解決Python中l(wèi)ist里的中文輸出到html模板里的問題
今天小編就為大家分享一篇解決Python中l(wèi)ist里的中文輸出到html模板里的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python 搭建簡單的http server,可直接post文件的實例
今天小編就為大家分享一篇python 搭建簡單的http server,可直接post文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python的Django框架中TEMPLATES項的設(shè)置教程
這篇文章主要介紹了Python的Django框架中TEMPLATES項的設(shè)置教程,主要針對Django1.8后的新特性,需要的朋友可以參考下2015-05-05

