英文:
Is there a way to find the 2 outside numbers in a dictionary of "Name": Number pairs from an input number
问题
抱歉,标题可能有点复杂。尽力了哈哈。
为了更容易理解这个问题,这里有一个示例:
levels = {"Level 1": 3.9, "Level 2": 4.5, "Level 3": 5.6, "Level 4": 9.2, "Level 5": 11.93}
current_number = 6.3
def find_upper_lower(current_number, levels):
### 在这里进行一些操作
upper_level = {"Level 4": 9.2}
lower_level = {"Level 3": 5.6}
return {"upper_level": upper_level, "lower_level": lower_level}
print(find_upper_lower(current_number, levels))
### 结果
### {'upper_level': {'Level 4': 9.2}, 'lower_level': {'Level 3': 5.6}}
所以基本上我想要找到最接近当前数字的两个数字(大于和小于当前数字),然后返回它们,但是返回整个字典项,而不仅仅是数字。我认为如果它是一个列表,可以将当前数字添加到项目列表中,然后对其进行排序,然后找到插入的数字的索引,然后添加和减去1来获取插入的数字之前和之后的项目 - 但我认为这仅适用于列表,我需要以某种方式保留字典中的“键”,而不仅仅是值。
谢谢您的任何帮助 - 如果您需要我解释更多内容,没问题,只需告诉我。
英文:
Sorry if the title is convuluted. Tried my best haha.
An example for easier understanding of the question:
levels = {"Level 1": 3.9, "Level 2": 4.5, "Level 3": 5.6, "Level 4": 9.2, "Level 5": 11.93}
current_number = 6.3
def find_upper_lower(current_number, levels):
###something here
upper_level = {"Level 4": 9.2}
lower_level = {"Level 3": 5.6}
return {"upper_level": upper_level, "lower_level": lower_level}
print(find_upper_lower(current_number, levels))
### Results
### {'upper_level': {'Level 4': 9.2}, 'lower_level': {'Level 3': 5.6}}
So essentially I'm looking to find the two nearest numbers (above and below the current number) and return them but with the whole dictionary item, not just the number. I think a way to do this if it was a list would be to add the current number to the list of items, then sort it and then find the index of the number inserted, and then add and minus 1 to the index to get the items before and after the number inserted - but I believe this would only work with a list and I need to somehow keep the "keys" in the dictionary, not just the values.
Thank you for any help - if you need me to explain anything more no problem just let me know.
答案1
得分: 1
你可以使用min
和max
函数与一个排序函数(lambda)来获取选定的键,然后从这些键组装你的结果字典:
highKey = min(levels, key=lambda k: (levels[k] <= current_number, levels[k] - current_number))
lowKey = max(levels, key=lambda k: (levels[k] < current_number, levels[k] - current_number))
result = {"upper_level": {highKey: levels[highKey]},
"lower_level": {lowKey: levels[lowKey]}}
这将得到以下结果:
{'upper_level': {'Level 4': 9.2}, 'lower_level': {'Level 3': 5.6}}
min
和max
函数中提供的lambda函数返回一个包含布尔值的元组作为它们的第一个元素。这允许这些函数排除不符合条件的值,并将它们放在值顺序的另一端(True 大于 False)。
英文:
you can use the min and max function with an ordering function (lambda) to get the selected keys and then assemble your resulting dictionary from those keys:
highKey = min(levels,key=lambda k:(levels[k]<=current_number,levels[k]-current_number))
lowKey = max(levels,key=lambda k:(levels[k]<current_number,levels[k]-current_number))
result = {"upper_level":{highKey:levels[highKey]},
"lower_level":{lowKey:levels[lowKey]}}
{'upper_level': {'Level 4': 9.2}, 'lower_level': {'Level 3': 5.6}}
The key lambda supplied to min and max return a tuple with a boolean as their first entry. This allow the functions to exclude the subset of values that are not eligible by placing them at the other end of the value order (True being greater than False)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论