Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python字典中查找索引范围之间的最小值_Python_Python 3.x_Dictionary_Range_Minimum - Fatal编程技术网

在python字典中查找索引范围之间的最小值

在python字典中查找索引范围之间的最小值,python,python-3.x,dictionary,range,minimum,Python,Python 3.x,Dictionary,Range,Minimum,我正在寻找字典中索引范围之间的最小值;例如: A = {1: -3, 2: -5, 3: 4, 5: 12, -34: 23, 64: 32} 我想找到: min(A[2..5]) = min(A[2] A[3] A[4]A [5]) = -5 这可能吗?Pythonlist不支持通过索引器列表进行索引。有几种解决方案可以克服这一问题 你知道钥匙在哪里吗 您可以使用itemgetter和序列解包: from operator import itemgetter A = {1: -3, 2:

我正在寻找字典中索引范围之间的最小值;例如:

A = {1: -3, 2: -5, 3: 4, 5: 12, -34: 23, 64: 32}
我想找到:

min(A[2..5]) = min(A[2] A[3] A[4]A [5]) = -5

这可能吗?

Python
list
不支持通过索引器列表进行索引。有几种解决方案可以克服这一问题

你知道钥匙在哪里吗 您可以使用
itemgetter
和序列解包:

from operator import itemgetter

A = {1: -3, 2: -5, 3: 4, 5: 12, -34: 23, 64: 32}

res = min(itemgetter(*[2, 3, 5])(A))

# -5
其中可能不存在密钥 可以使用
范围
对象指定关键点的范围。
列表
集合
也可以。这里,我们将生成器表达式馈送到
min

res = min(v for k, v in A.items() if k in range(2, 6))

# -5

你甚至都没用谷歌搜索过。这里是一个链接。使用它well@SaNa,我编辑了您的问题以修复语法错误;希望没问题。