在字典中查找最接近特定int / float的int / float键

时间:2019-01-13 18:12:11

标签: python dictionary

我需要在字典中找到最接近特定int / float变量的int / float键。我尝试过:

record = {1:370,2:45,5:87}
binum = ''
t = raw_input("")
memory = [("hi", "lee"), ("lee", "ma"), ("ma","bree")]
bi2 = ''
bi = []
def b(): 
  words = t.split()

  result = list(zip(words, words[1:]))
  print result

  for x in memory:
    global bi2
    global bi

    if(x in result):
      bi.append("1")

    else:
      bi.append("0")

   print bi    
   bi2 = ''.join(bi)
   print bi2

   dec = int(bi2, 2)
   print dec
   print(min(record, key = lambda x:abs(record[x]-dec)))

b()

我希望它返回5,但它给出类型错误。错误是:

  

TypeError:“ key”是main.py中第36行上该函数的无效关键字参数

我缺少什么吗?更好的方法吗?

0 个答案:

没有答案