如何解决 Python 中的 ValueError [英] how to solve ValueError in Python

查看:115
本文介绍了如何解决 Python 中的 ValueError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个图像搜索引擎应用程序,我将代码放在单独的 .py 文件中,并且运行良好.但我想优化它.当我使用下面的函数时,它给了我一个 ValueError.

我的代码是这样的(我只选取了相关的行):

def 示例():我 = 0resultlist_key = []结果列表 = 列表()a_list = list()b_list = 列表()a_list.append(feature_matrix_ip)# feature_matrix_ip 包含查询图像的特征当我 <70:b_list.append(feature_matrix_db[i])# feature_matrix_db 包含 img 的特征.在数据库中dist = distance.euclidean(a_list,b_list[i])result_list.append(dist)resultlist_key = OrderedDict(sorted(enumerate(result_list),key=lambda x: x[0])).keys()我 = 我 + 1res_lst_srt = {'values': result_list,'keys':resultlist_key}res_lst_srt['values'], res_lst_srt['keys'] = zip(*sorted(zip(res_lst_srt['values'], res_lst_srt['keys'])))#按照最小距离排序,key不变key = res_lst_srt['keys']

当我分析时,我没有得到任何解决方案.我的错误陈述是:

%run "D:/6th sem/Major project/Code/frame.py"Tkinter 回调中的异常回溯(最近一次调用最后一次):文件C:\Users\HP\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.0.3.1262.win-x86\lib\lib-tk\Tkinter.py",第 1410 行,在 __call__ 中返回 self.func(*args)文件D:\6th sem\Major project\Code\frame.py",第323行,在matching_image中res_lst_srt['values'], res_lst_srt['keys'] = zip(*sorted(zip(res_lst_srt['values'], res_lst_srt['keys'])))ValueError:需要多于 0 个值才能解包

我不知道这个错误是否来自命名问题.当代码在函数外时它会正常工作,但我希望代码在函数内,以便优化程序.

对解决此错误有什么建议吗?

解决方案

通过查看这个简单的案例可以发现您的问题.

<预><代码>>>>a, b = tuple() # 或者你可以写 a, b = []回溯(最近一次调用最后一次):文件<pyshell#319>",第 1 行,在 <module> 中a, b = 元组()ValueError:需要多于 0 个值才能解包>>>

等于运算符一侧的值长度必须与另一侧匹配.

很明显,返回的值是..

zip(*sorted(zip(res_lst_srt['values'], res_lst_srt['keys'])))

..最终返回一个空列表/元组.这让你感到悲伤.

I'm working on a image search engine application, I have the code in separate .py files and it's working fine. But I want to optimize it. When I use the function below, it's giving me a ValueError.

My code is like this(only relevant lines i have taken):

def example():
  i = 0
  resultlist_key = []
  result_list = list()
  a_list = list()
  b_list = list()
  a_list.append(feature_matrix_ip)# feature_matrix_ip contains features of the query image
  while i < 70:
      b_list.append(feature_matrix_db[i])# feature_matrix_db contains features of img. in DB
      dist = distance.euclidean(a_list,b_list[i])
      result_list.append(dist)
      resultlist_key = OrderedDict(sorted(enumerate(result_list),key=lambda x: x[0])).keys()
      i = i + 1 
      res_lst_srt = {'values': result_list,'keys':resultlist_key}
      res_lst_srt['values'], res_lst_srt['keys'] = zip(*sorted(zip(res_lst_srt['values'], res_lst_srt['keys'])))# sorting according to the least distance and the key will not change
      key = res_lst_srt['keys']

When I analyze, I didn't get any solutions for this. My error statement is:

%run "D:/6th sem/Major project/Code/frame.py"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.0.3.1262.win-x86\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "D:\6th sem\Major project\Code\frame.py", line 323, in matching_image
res_lst_srt['values'], res_lst_srt['keys'] = zip(*sorted(zip(res_lst_srt['values'], res_lst_srt['keys'])))

ValueError: need more than 0 values to unpack

I have no idea whether this error comes from the naming problem or not. It will work correctly when the code is outside the function, but I want the code to be inside the function, so that the program is optimized.

Any suggestions on solving this error?

解决方案

Your issue can be found by looking at this simple case.

>>> a, b = tuple() # or you could write a, b = []

Traceback (most recent call last):
  File "<pyshell#319>", line 1, in <module>
    a, b = tuple()
ValueError: need more than 0 values to unpack
>>> 

The length of values on one side of the equals operator must match the other.

So its clear that the values returned by..

zip(*sorted(zip(res_lst_srt['values'], res_lst_srt['keys'])))

..end up returning an empty list/tuple. Which is causing you grief.

这篇关于如何解决 Python 中的 ValueError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆