【问题标题】:Loop through dictionary with a math function使用数学函数遍历字典
【发布时间】:2012-04-14 04:11:37
【问题描述】:

我有这本字典,其中 (key1, key2): value

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 但我不知道如何。

【问题讨论】:

  • 同一个key每次都会有最大的平均值。
  • 如果您计划对组成键的元组执行数学运算,您可能应该将它们存储为整数,而不是字符串。即(1,4) 不是('1','4')

标签: python


【解决方案1】:

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

您的密钥是strings,因此没有平均值。如果我们转换为ints:

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)

【讨论】:

  • 我们需要某种方式告诉大家不要使用built-ins 的名字。例如,很多人使用list = [1,2,3]。 :(
  • @agf 为什么会出现此错误:ValueError: invalid literal for int() with base 10: '8' ?
  • @DarsAE 我不知道。我将您的问题中的dict 复制并粘贴到我的IDE 中,并且该代码可以正常工作。尝试将其从您的问题复制回您的代码中。
【解决方案2】:
# 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]

【讨论】:

  • 我认为将 numpy 用于这么简单的事情,尤其是对于刚刚学习 Python 的人来说,是矫枉过正。
  • 我不同意。当然,学习更多 Pythonic 方法来解决这个问题很重要。但是用 Python 预先学习这种过程同样重要。当然只是我的意见。我看不出学习有用的库是怎么过分的。特别是对于我给出的欧几里得距离的替代方案。
  • 你也可能想为 numpy 添加 import
  • 0.5*(elem[0]+elem[1],为什么不在这里使用 np.mean(elem)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 2012-10-16
  • 2019-09-24
相关资源
最近更新 更多