Python – 获取字典中的下一个键
有时,在使用Python字典时,我们可能会遇到需要按字典顺序提取下一个键的问题。这可以从Python 3.6 开始应用,字典是有序的。让我们讨论可以执行此任务的某些方式。
方法 #1:使用 index() + 循环(O (n))
上述功能的组合可用于执行此任务。在此,我们执行字典元素到列表的转换。然后 index() 用于检查索引并附加索引计数以获取下一项。
Python3
# Python3 code to demonstrate working of
# Get next key in Dictionary
# Using index() + loop
# initializing dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing key
test_key = 'is'
# Get next key in Dictionary
# Using index() + loop
temp = list(test_dict)
try:
res = temp[temp.index(test_key) + 1]
except (ValueError, IndexError):
res = None
# printing result
print("The next key is : " + str(res))
Python3
# Python3 code to demonstrate working of
# Get next key in Dictionary
# Using iter() + next()
# initializing dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing key
test_key = 'is'
# Get next key in Dictionary
# Using iter() + next()
res = None
temp = iter(test_dict)
for key in temp:
if key == test_key:
res = next(temp, None)
# printing result
print("The next key is : " + str(res))
Python3
#!/usr/bin/python3
# Python3 code to demonstrate working of
# Get next key in Dictionary
# Using two additional dictionaries
# "index_of_key" and "key_of_index"
# initializing dictionary
test_dict = {'gfg': 1, 'is': 2, 'best': 3}
# prepare additional dictionaries
ki = dict()
ik = dict()
for i, k in enumerate(test_dict):
ki[k] = i # dictionary index_of_key
ik[i] = k # dictionary key_of_index
# printing original dictionary
print("The original dictionary is:", test_dict)
# initializing key and offset
test_key = 'is'
offset = 1 # (1 for next key, but can be any existing distance)
# Get next key in Dictionary
index_of_test_key = ki['is']
index_of_next_key = index_of_test_key + offset
res = ik[index_of_next_key] if index_of_next_key in ik else None
# printing result
print("The next key is:", res)
输出 :
The original dictionary is : {'gfg': 1, 'best': 3, 'is': 2}
The next key is : best
方法 #2:使用 iter() + next() (O (n))
这是可以执行此任务的另一种方式。在此,我们使用 iter() 将字典转换为迭代器,然后使用 next() 提取下一个键。
Python3
# Python3 code to demonstrate working of
# Get next key in Dictionary
# Using iter() + next()
# initializing dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing key
test_key = 'is'
# Get next key in Dictionary
# Using iter() + next()
res = None
temp = iter(test_dict)
for key in temp:
if key == test_key:
res = next(temp, None)
# printing result
print("The next key is : " + str(res))
输出 :
The original dictionary is : {'gfg': 1, 'best': 3, 'is': 2}
The next key is : best
方法#3:如果你有很多查询,它们应该很快 -> (O (1))
在 O (n) 中创建两个额外的字典“index_of_key”和“key_of_index”。然后很容易找到原始字典的任何键的索引,并选择任何其他的加号或减号,检查它是否在字典“key_of_index”中,如果,然后得到它。
Python3
#!/usr/bin/python3
# Python3 code to demonstrate working of
# Get next key in Dictionary
# Using two additional dictionaries
# "index_of_key" and "key_of_index"
# initializing dictionary
test_dict = {'gfg': 1, 'is': 2, 'best': 3}
# prepare additional dictionaries
ki = dict()
ik = dict()
for i, k in enumerate(test_dict):
ki[k] = i # dictionary index_of_key
ik[i] = k # dictionary key_of_index
# printing original dictionary
print("The original dictionary is:", test_dict)
# initializing key and offset
test_key = 'is'
offset = 1 # (1 for next key, but can be any existing distance)
# Get next key in Dictionary
index_of_test_key = ki['is']
index_of_next_key = index_of_test_key + offset
res = ik[index_of_next_key] if index_of_next_key in ik else None
# printing result
print("The next key is:", res)
输出:
The original dictionary is : {'gfg': 1, 'best': 3, 'is': 2}
The next key is : best