[英]how to solve ValueError in Python
我正在处理图像搜索引擎应用程序,代码分别位于.py
文件中,并且工作正常。 但我想对其进行优化。 当我使用下面的函数时,它给了我ValueError。
我的代码是这样的(我只采取了相关行):
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']
当我分析时,我没有得到任何解决方案。 我的错误声明是:
%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
我不知道此错误是否来自命名问题。 当代码在函数外部时,它将正常工作,但是我希望代码在函数内部,以便对程序进行优化。
关于解决此错误的任何建议?
因此,我的猜测是这一部分:
zip(*sorted(zip(res_lst_srt['values'], res_lst_srt['keys'])))
向任何zip
返回一个空列表:
>>> foo, bar = []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 0 values to unpack
现在,如果您要填充res_lst_srt
(据我从您的问题中可以理解的那样),为什么不简化它呢?
>>> values=[1,2,3]
>>> keys=['a', 'b', 'c']
>>> my_dict = dict(zip(keys, values))
>>> my_dict
{'a': 1, 'c': 3, 'b': 2}
( values
在这里将res_lst_srt['values']
keys
将res_lst_srt['keys']
和my_dict
将res_lst_srt
没有?难道不应该工作?
通过查看此简单案例可以找到您的问题。
>>> 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
>>>
equals运算符一侧的值长度必须与另一侧匹配。
因此,很明显,返回的值是..
zip(*sorted(zip(res_lst_srt['values'], res_lst_srt['keys'])))
..最终返回一个空列表/元组。 这使你感到悲伤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.