python迭代地在字典中找到最小值

时间:2015-10-12 06:22:28

标签: python dictionary

我有一个字典(名为distance),如下所示:

{0: {0: 122.97560733739029, 1: 208.76062847194152, 2: 34.713109915419565}, 1: {0: 84.463009655114703, 1: 20.83266665599966, 2: 237.6299644405141}, 2: {0: 27.018512172212592, 1: 104.38390680559911, 2: 137.70257804413103}}

现在,我需要做的是我必须找到与每个键对应的最小值,然后分别存储其索引。我为此写了这段代码:

weights_indexes = {}    
for index1 in distances:
    min_dist = min(distances[index1], key=distances[index1].get)
    weights_indexes[index1] = min_dist

此输出如下:

{0: 2, 1: 1, 2: 0}

现在,问题在于索引应始终是唯一的。让我们说现在,如果我们有一个字典,如:

{0: {0: 34.713109915419565, 1: 208.76062847194152, 2: 122.97560733739029}, 1: {0: 84.463009655114703, 1: 20.83266665599966, 2: 237.6299644405141}, 2: {0: 27.018512172212592, 1: 104.38390680559911, 2: 137.70257804413103}}

因此,为此找到最小索引的输出将是:

{0: 0, 1: 1, 2: 0}

这里,获得的索引(值)不是唯一的。在这种情况下,必须比较与找到重复项的索引相对应的值。因此,将比较34.713109915419565和27.018512172212592。由于27.018512172212592较小,因此其指数将被选中。对于索引0,将映射到下一个最小索引,即索引122.97560733739029。因此,最终映射将如下所示:

    {0: 2, 1: 1, 2: 0}

这应该迭代发生,除非所有值都是唯一的。 我无法弄清楚如何检查唯一性,并迭代地继续寻找下一个最小值来进行映射。

1 个答案:

答案 0 :(得分:0)

这是一个可行的解决方案:

test = {0: {0: 12.33334444, 1: 208.76062847194152, 2: 34.713109915419565}, 1: {0: 84.463009655114703, 1: 20.83266665599966, 2: 237.6299644405141}, 2: {0: 27.018512172212592, 1: 104.38390680559911, 2: 137.70257804413103}}

sorted_index_map = {}

for key, value in test.iteritems():
    sorted_index_map[key] = sorted(value, key=lambda k: value[k])

index_of_min_index_map = {key: 0 for key in test}

need_to_check_duplicate = True

while need_to_check_duplicate:
    need_to_check_duplicate = False
    min_index_map = {key: sorted_index_map[key][i] for key, i in index_of_min_index_map.iteritems()}
    index_set = list(min_index_map.itervalues())
    for key, index in min_index_map.iteritems():
        if index_set.count(index) == 1:
            continue
        else:
            for key_to_check, index_to_check in min_index_map.iteritems():
                if key != key_to_check and index == index_to_check:
                    if test[key][index] > test[key_to_check][index_to_check]:
                        index_of_min_index_map[key] += 1
                        need_to_check_duplicate = True
                        break

result = {key: sorted_index_map[key][i] for key, i in index_of_min_index_map.iteritems()}
print result

结果:

{0: 0, 1: 1, 2: 2}

分解: 首先按照它的值对索引进行排序:

test = {0: {0: 12.33334444, 1: 208.76062847194152, 2: 34.713109915419565}, 1: {0: 84.463009655114703, 1: 20.83266665599966, 2: 237.6299644405141}, 2: {0: 27.018512172212592, 1: 104.38390680559911, 2: 137.70257804413103}}

sorted_index_map = {}

for key, value in test.iteritems():
sorted_index_map[key] = sorted(value, key=lambda k: value[k])

然后,对于每个键,最小值的索引是sorted_index_map中的第一个数字:

index_of_min_index_map = {key: 0 for key in test}

现在我们需要检查是否存在任何重复索引(如果有),对于相同索引的所有值都不是最小的。我们转移到下一个小索引,即密钥的sorted_index_map中的下一个索引。如果没有重复,我们就完成了。

need_to_check_duplicate = True

while need_to_check_duplicate:
    need_to_check_duplicate = False
    min_index_map = {key: sorted_index_map[key][i] for key, i in index_of_min_index_map.iteritems()}
    index_set = list(min_index_map.itervalues())
    for key, index in min_index_map.iteritems():
        if index_set.count(index) == 1:
            continue
        else:
            for key_to_check, index_to_check in min_index_map.iteritems():
                if key != key_to_check and index == index_to_check:
                    if test[key][index] > test[key_to_check][index_to_check]:
                        index_of_min_index_map[key] += 1
                        need_to_check_duplicate = True
                        break

请注意,如果有两个相同的值,你还没有提到如何处理索引,所以我认为不会有。