def min_keys(d, index):
# Initialize lists
values = []
matches = []
# Append tuple items to list based on index
for t in list(d.values()):
values.append(t[index])
# If the item matches the min, append the key to list
for key in d:
if d[key][index] == min(values):
matches.append(key)
# Return a list of all keys with min value at index
return matches
可以编写lambda函数,通过元素的索引从值中获取元素:
词典没有排序,没有索引。在
如果要先按字母顺序返回密钥,可以使用ascii顺序:
由于多个键可以具有相同的值,所以您可能希望返回匹配键的列表,而不仅仅是一个键。在
相关问题 更多 >
编程相关推荐