物語はここから始まります...
朝起きてコメントを見ました。少しぎこちないです。辞書のスライス?
Pythonの情報を確認しました。3.6バージョンのPythonは、dictの内部アルゴリズムを書き直しました。バージョン3.6より前は無秩序でした。
したがって、現在は正常です。注意してください。この順序はキーの挿入順序です
が、添え字がない場合でも辞書をスライスするにはどうすればよいですか?リストリスト?
次に、キーをリストに入れ、リスト自体のインターセプト方法を使用してスライスします!
次に、インターセプトされたキーを使用して新しいディクショナリーの割り当て!
頭が熱くなったときにディクショナリースライスバージョン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("结束位置:"))
#调用切片方法
newdict =dictcut(dict, start, end)
print("dictcut(dict,%d,%d):" % (start, end), newdict)
次に、結果を実行します
-
2つの数値は正であり、境界を越えない、通常の傍受
-
2から最後までインターセプトしたい場合、終了座標は-1ですが、[2:-1]を渡すと[2、-1)がインターセプトされ、最後の座標はインターセプトされません。[2:0]を渡すと、次のようになります。以下は同じです
ので、list [2:]は2から最後までインターセプトしますが、これはパラメータ転送の点で非常に面倒です。 -
[-5、-3]を傍受しても問題ないが、[-3、-5]は機能しない場合
要約すると、辞書のスライスを、正から負、前から後ろに行き止まりなしで360度でカットできることを願っています。 、前方にカット、後方にカット
だから、辞書スライス2.0バージョンはここにあります!
# 字典切片2.0
def dictcut(dict, start, end):
# 临时存放字典的key
temp = list(dict.keys())
# 返回一个字典
result = {
}
# 分两个分支 1.start和end在可切片范围内 2.不在范围内
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不在范围内
else:
print("传入参数有误!")
return result
#原字典
dict = {
"zero": "0", "one": "1", "two": "2", "three": "3", "four": "4"}
print("dict:", dict)
print("字典切割:左闭右开")
start = eval(input("起始位置:"))
end = eval(input("结束位置:"))
newdict = dictcut(dict, start, end)
# 如果返回的字典不为空,打印结果
if len(newdict) != 0:
print("dictcut(dict,%d,%d):" % (start, end), newdict)
動作結果:
-
範囲内にない場合
-
座標が重なっている場合は、重なって孤独にカットしますか?
-
360°デッドアングルカットカットなし(ノーマルカット)
正数
負数 -
360°デッドアングルカットなし(後ろから
前にカット)正の数
負の数
2.0コードはもっと面倒ですが、辞書のスライスの効果はまだはっきりと見えます~~~