2012-04-14 96 views
3

我有此词典中,其中(KEY1,KEY2):值遍历字典,一个数学函数

dict = {('1', '4'): 'A', ('3', '8'): 'B', ('4', '7'): 'C', 
('8', '9'): 'D', ('4', '2'): 'E', ('2', '0'): 'F', ('3', '9'): 
'G', ('7', '7'): 'H', ('8', '6'): 'I', ('5', '3'): 'J', 
('6', '1'): 'K'} 

key1 = input('enter value of key1: ') 
key2 = input('enter value of key2: ') 

如果我输入一对KEY1,KEY2和一对不存在的,是有什么方法可以循环使用这个字典并传递一个数学函数,即找到每对键的平均值并打印出平均值最大的那个?

编辑:其实这本字典是从一个文本文件派生的,所以它必须在字符串中,我需要将其转换为int,但我不知道如何。

+2

同样的关键字会始终在每次最大平均值。 – 2012-04-14 04:15:14

+1

如果您打算对组成键的元组进行数学运算,那么您可能应该将它们存储为整数,而不是字符串。即'(1,4)'不是'('1','4')' – ulmangt 2012-04-14 04:16:55

回答

4

不要称之为dict,这会阻止您访问内置的dict

您的密钥为str,因此没有平均值。如果我们转换为int S:

dct = dict((tuple(map(int, key)), value) for key, value in str_dict.iteritems()) 

其中给出:

dct = {(8, 9): 'D', (4, 7): 'C', (6, 1): 'K', (7, 7): 'H', 
     (1, 4): 'A', (3, 8): 'B', (2, 0): 'F', (3, 9): 'G', 
     (4, 2): 'E', (8, 6): 'I', (5, 3): 'J'} 

然后你就可以在每个键的sum使用max

key = max(d, key=sum) 
# (8, 9) is the key with the highest average value 

由于具有最高sum也是最高的平均水平。

如果你再想该键的值,它是:

value = dct[key] 
# 'D' is the value for (8, 9) 
+0

我们需要某种方式告诉大家不要使用'built-in'的名字。例如,很多人使用'list = [1,2,3]'。 :( – jamylak 2012-04-14 04:27:40

+0

@agf为什么我得到这个错误:ValueError:无效的文字为int()与基10:'8'? – DarsAE 2012-04-14 04:32:25

+0

@jamylak哈哈我实际上有一个不同的名字为我的代码字典,但我在抢所以我只是简单地把它作为字典在这个问题 – DarsAE 2012-04-14 04:33:25

0
# Use NumPy. It seems this will help if you'll be needing argmin type functions. 
# This is BY NO MEANS the only way to do it in Python, but it is a good way to 
# know nonetheless. 
import numpy as np 

my_dict = {('1', '4'): 'A', ('3', '8'): 'B', ('4', '7'): 'C', 
('8', '9'): 'D', ('4', '2'): 'E', ('2', '0'): 'F', ('3', '9'): 
'G', ('7', '7'): 'H', ('8', '6'): 'I', ('5', '3'): 'J', 
('6', '1'): 'K'} 

# Get the keys 
dict_keys = my_dict.keys() 

# Get the average value of each key pair. 
averages_for_keys = np.array([np.mean(elem) for elem in dict_keys]) 

# Get the index and the key pair of the largest average. 
largest_average_key = dict_keys[averages_for_keys.argmax()] 

# Get user input 
key1 = input('enter value of key1: ') 
key2 = input('enter value of key2: ') 

# If not in dict_keys, print for largest average key pair. 
if (key1, key2) not in dict_keys: 
    print "Invalid input key pair. Proceeding with largest average." 
    print my_dict[largest_average_key] 


### 
# An alternative to index on the closest key by Euclidean distance. 
# This can be adjusted for other distances as well. 
### 
if (key1, key2) not in dict_keys: 
    print "Didn't find key pair in dict." 
    print "Proceeding with keypair of minimal distance to input." 

    dists = np.array([sqrt((elem[0]-key1)**2.0 + (elem[1]-key2)**2.0) for elem in dict_keys]) 
    min_index = dists.argmin() 
    closest_key = dict_keys[min_index] 

print my_dict[closest_key] 
+2

我认为使用numpy来实现这个简单的东西,特别是对于刚刚学习Python的人来说,是过度杀伤性的 – agf 2012-04-14 04:27:32

+0

我很尊敬地不同意当然,学习更多Pythonic方法来解决这个问题很重要,但它同样适用于很重要的一点就是要事先学习Python这种程序,当然,我的看法是,我不知道如何去学习有用的库,尤其是对于我为欧几里得距离给出的替代品。 – ely 2012-04-14 04:29:51

+0

你也可能想要为'numpy – jamylak 2012-04-14 04:29:56