Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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_Max_Python 2.x - Fatal编程技术网

Python 如何获取字典元组的最大值

Python 如何获取字典元组的最大值,python,python-3.x,dictionary,max,python-2.x,Python,Python 3.x,Dictionary,Max,Python 2.x,我有一本这样的字典: dic={(0, 1): 0.059999999999999996, (0, 5): 0.13157894736842105, (0, 15): 0.23157894736842105, (1, 0): 0.049999999999999996, (5, 0): 0.13157894736842105, (5, 15): 0.049999999999999996, (15, 5): 0.23157894736842105} 我想得到向量的第一个坐标中每个元素的

我有一本这样的字典:

dic={(0, 1): 0.059999999999999996,
 (0, 5): 0.13157894736842105,
 (0, 15): 0.23157894736842105,
 (1, 0): 0.049999999999999996,
 (5, 0): 0.13157894736842105,
 (5, 15): 0.049999999999999996,
 (15, 5): 0.23157894736842105}
我想得到向量的第一个坐标中每个元素的最大值,还有向量的第二个元素

The output would be:
For 0 [First coordinate]:      (0, 5): 0.13157894736842105
For 0 [Second coordinate]:      (5, 0): 0.13157894736842105
For 1 [First coordinate]:       (1,0) 0.049999999999999996
For 1 [Second coordinate]:       (0,1) 0.059999999999999996
and so on.
我知道我可以用这样的东西

max_keys = [k for k, v in dic.items() if v == max_value] 
但我无法找到正确的方法

def getMaxForX(number):
    return max([v for k, v in dic.items() if k[0] == number])

def getMaxForY(number):
    return max([v for k, v in dic.items() if k[1] == number])
我不确定如何充分实现这些,但我认为这是您正在寻找的理解列表

e、 g:


如果您希望每个最大值的键和值都有一点不同,但仍然可行。

这可以通过一行程序完成

例如,对于0[第一个坐标]:

打印(最大值([(k,v)表示滤波器中的k,v(λx:x[0][0]==y,dic.items()),键=λx:x[1]))
Out[2]:((0,15),0.23157894736842105)
但更好的方法是将其放入函数中:

def get_max(驾驶员信息中心、驾驶员协调中心、车辆识别号):
返回最大值(筛选器(lambda项:项[0][coord]==val,dic.items()),key=lambda x:x[1])
对于0[第一个坐标]:

打印(获取最大值(dic,0,0))
Out[5]:((0,5),0.23157894736842105)
#或者存储密钥和值:
键最大,值最大=获取最大(dic,0,0)
对于0[第二坐标]:

打印(获取最大值(dic,1,0))
Out[6]:((5,0),0.13157894736842105)
等等


希望对大家有帮助,编码快乐

这个问题不适合使用元组键的词典。您可以迭代并维护自己的结果字典,以保持每个索引键的最大值。或者您可以使用第三方库,如熊猫:

import pandas as pd

dic = {(0, 1): 0.059999999999999996, (0, 5): 0.13157894736842105,
       (0, 15): 0.23157894736842105, (1, 0): 0.049999999999999996,
       (5, 0): 0.13157894736842105, (5, 15): 0.049999999999999996,
       (15, 5): 0.23157894736842105}

df = pd.DataFrame.from_dict(dic, orient='index').reset_index()
df[['key0', 'key1']] = pd.DataFrame(df.pop('index').values.tolist())

res = pd.concat((df.groupby(f'key{i}')[0].max().rename(f'idx{i}')
                 for i in range(2)), axis=1)

print(res)

        idx0      idx1
0   0.231579  0.131579
1   0.050000  0.060000
5   0.131579  0.231579
15  0.231579  0.231579

为什么有些输出值不是具有类似键的所有值的最大值?e、 g.
(0,5):0.13157894736842105
(0,15):0.23157894736842105,但您说过结果将是前者(您预期输出的第一行)。您应该定义什么是
数字以及如何获得它。+1是的,它可以获得最大值,但是我不能得到与最大值相关的向量。
import pandas as pd

dic = {(0, 1): 0.059999999999999996, (0, 5): 0.13157894736842105,
       (0, 15): 0.23157894736842105, (1, 0): 0.049999999999999996,
       (5, 0): 0.13157894736842105, (5, 15): 0.049999999999999996,
       (15, 5): 0.23157894736842105}

df = pd.DataFrame.from_dict(dic, orient='index').reset_index()
df[['key0', 'key1']] = pd.DataFrame(df.pop('index').values.tolist())

res = pd.concat((df.groupby(f'key{i}')[0].max().rename(f'idx{i}')
                 for i in range(2)), axis=1)

print(res)

        idx0      idx1
0   0.231579  0.131579
1   0.050000  0.060000
5   0.131579  0.231579
15  0.231579  0.231579