I keep getting a Persistent ValueError: too many values to unpack and i don't understand why
I keep getting a Persistent ValueError: too many values to unpack and i don't understand why
我遇到了这个错误,我想我已经花了 5 个小时解决这个问题。请有人帮忙。
我最终想从中获得价值。
是什么导致有很多值?是我的字符串值还是字典本身的设置。
import ast
# -*- coding: utf-8 -*-
class MyMetric:
def __init__(self, name):
self.name = name
self.metric_system = {
"yotta": ("Y", str(10^24) ),
"zetta": ("Z", str(10^21)),
"exa": ("E", str(10^18)),
"peta": ("P", str(10^15)),
"tera": ("T", str(10^12)),
"giga": ("G", str(10^9)),
"mega": ("M", str(10^6)),
"kilo": ("k", str(10^3)),
"hecto": ("h", str(10^2)),
"deka": ("da", str(10^1)),
"deci": ("d", str(10^-1)),
"centi": ("c", str(10^-2)),
"milli": ("m", str(10^-3)),
"micro": ("μ", str(10^-6)),
"nano": ("n", str(10^-9)),
"pico": ("p", str(10^-12)),
"femto": ("f", str(10^-15)),
"atto": ("a", str(10^-18)),
"zepto": ("z", str(10^-21)),
"yocto": ("y", str(10^-24))
}
def __str__(self):
for key,val in self.metric_system:
self.metric_name = key
self.metric_symbol = val[0]
self.metric_value = val[1]
if self.name == self.metric_name:
print ("it is there")
me = MyMetric("milli")
print(me)
你的循环应该是
for key,val in self.metric_system.items():
这将迭代 (key, value)
对
循环字典给你只有键。当您需要键和值时使用 dict.items()
method:
for key, val in self.metric_system.items():
iter(d)
Return an iterator over the keys of the dictionary. This is a shortcut for iter(d.keys())
.
for
循环在被循环的项目内部使用 iter()
。
请注意,__str__
方法需要 return 一个字符串,而不是打印它。
但是,当您已经有了键时,迭代字典中的所有值就没有意义了。你可以简单地做:
def __str__(self):
if self.name in self.metric_system:
return "it is here"
return "it is not here"
或使用字典查找来获取值:
def __str__(self):
try:
symbol, magnitude = self.metric_system[self.name]
except KeyError:
return 'Unknown metric {}'.format(self.name)
return 'Metric {} ({}, {})'.format(self.name, symbol, magnitude)
演示:
>>> print(MyMetric("milli"))
Metric milli (m, -9)
>>> print(MyMetric("foobar"))
Unknown metric foobar
我遇到了这个错误,我想我已经花了 5 个小时解决这个问题。请有人帮忙。 我最终想从中获得价值。 是什么导致有很多值?是我的字符串值还是字典本身的设置。
import ast
# -*- coding: utf-8 -*-
class MyMetric:
def __init__(self, name):
self.name = name
self.metric_system = {
"yotta": ("Y", str(10^24) ),
"zetta": ("Z", str(10^21)),
"exa": ("E", str(10^18)),
"peta": ("P", str(10^15)),
"tera": ("T", str(10^12)),
"giga": ("G", str(10^9)),
"mega": ("M", str(10^6)),
"kilo": ("k", str(10^3)),
"hecto": ("h", str(10^2)),
"deka": ("da", str(10^1)),
"deci": ("d", str(10^-1)),
"centi": ("c", str(10^-2)),
"milli": ("m", str(10^-3)),
"micro": ("μ", str(10^-6)),
"nano": ("n", str(10^-9)),
"pico": ("p", str(10^-12)),
"femto": ("f", str(10^-15)),
"atto": ("a", str(10^-18)),
"zepto": ("z", str(10^-21)),
"yocto": ("y", str(10^-24))
}
def __str__(self):
for key,val in self.metric_system:
self.metric_name = key
self.metric_symbol = val[0]
self.metric_value = val[1]
if self.name == self.metric_name:
print ("it is there")
me = MyMetric("milli")
print(me)
你的循环应该是
for key,val in self.metric_system.items():
这将迭代 (key, value)
循环字典给你只有键。当您需要键和值时使用 dict.items()
method:
for key, val in self.metric_system.items():
iter(d)
Return an iterator over the keys of the dictionary. This is a shortcut foriter(d.keys())
.
for
循环在被循环的项目内部使用 iter()
。
请注意,__str__
方法需要 return 一个字符串,而不是打印它。
但是,当您已经有了键时,迭代字典中的所有值就没有意义了。你可以简单地做:
def __str__(self):
if self.name in self.metric_system:
return "it is here"
return "it is not here"
或使用字典查找来获取值:
def __str__(self):
try:
symbol, magnitude = self.metric_system[self.name]
except KeyError:
return 'Unknown metric {}'.format(self.name)
return 'Metric {} ({}, {})'.format(self.name, symbol, magnitude)
演示:
>>> print(MyMetric("milli"))
Metric milli (m, -9)
>>> print(MyMetric("foobar"))
Unknown metric foobar