2012-04-14 24 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: ') 

I入力KEY1の一対、KEY2ペアが存在しないである場合私はこの辞書をループして数学関数を渡すことができます。すなわち、キーの各ペアの平均値を求め、最大の平均値を持つものを印刷します。

編集:実際には、この辞書はテキストファイルから派生したものなので、まず文字列にする必要があり、intに変換する必要がありますが、私はその方法を知らない。

+2

同じキーが常に最大の平均値を毎回持っています。 –

+1

キーを構成するタプルの数学演算を実行する予定がある場合は、文字列ではなくintとして格納することをお勧めします。 ''(1,4) ''( '1'、 '4') ' – ulmangt

答えて

4

dictと呼ばず、内蔵のdictにアクセスできないようにしてください。

鍵はstrですので、平均値はありません。我々はint秒に変換する場合:

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'} 

を次に使用すると、各キーのsummaxを使用することができます。最高sumと1ので

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

また、平均が最も高い。

あなたは、そのキーの値をしたい場合、それはです:

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

皆さんに'組み込み 'の名前を使用しないように指示する必要があります。多くの人が 'list = [1,2,3]'を使用しています。 ( – jamylak

+0

@agfなぜこのエラーが発生するのですか?ValueError:基数10の「8」のint()のリテラルが無効ですか? – DarsAE

+0

@jamylak haha​​実際にはコード内の辞書とは別の名前が付いていますが、 – DarsAE

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

私はこの単純なもの、特にPythonを学ぶ人のためにnumpyを使うのは残念だと思います。 – agf

+0

私は正直に言って反対です。これを解決するPythonの方法をもっと学ぶことは大切です。この種の手続きをPythonで学ぶことが大切ですが、私の意見ではもちろん、有用な図書館について学ぶのはいかに過度のことでもないのですが、特にユークリッド距離のために教えてくれました。 – ely

+0

また、あなたはnumpyの 'import'をアドバイスしたいかもしれません – jamylak