Python 检查OrderedICT中是否存在密钥
我正在处理从python2到python3的代码迁移。我对python中的OOP和Python 检查OrderedICT中是否存在密钥,python,python-3.x,dictionary,python-2to3,Python,Python 3.x,Dictionary,Python 2to3,我正在处理从python2到python3的代码迁移。我对python中的OOP和OrderedDict没有太多经验。这是我无法解决的问题。有人问过类似的问题,但对这个案子似乎没有任何效果。我有以下代码: for row in rows: new_ = dummy(row.attr, row.error_type, row.array_agg_1, row.with_error, row.total, row.level, row.order_)
OrderedDict
没有太多经验。这是我无法解决的问题。有人问过类似的问题,但对这个案子似乎没有任何效果。我有以下代码:
for row in rows:
new_ = dummy(row.attr, row.error_type, row.array_agg_1,
row.with_error, row.total, row.level, row.order_)
if new_ in result[n.id]: # <- never becomes true
print('I am in the if')
if new_.with_error is not None:
result[n.id][new_][1] += new_.with_error
result['total'][new_][1] += new_.with_error
else:
print('I am in the else')
if new_.with_error is not None:
result[n.id][new_] = [new_.attr, new_.with_error]
新建
如下所示:
#an example of result[n.id]
>>> ([(Eclipse Required Items'nce'[1540972], ['Eclipse Required Items', 1, 1, [1540972], 'nce', 1, 1681]),
(Other Story Tab Info learned/discovered documentation and accuracy'nce'[1540973], ['Other Story Tab Info learned/discovered documentation and accuracy', 1, 1, [1540973], 'nce', 1, 1684]),
(Other (please provide detail in Comments section)'bce'[1541001], ['Other (please provide detail in Comments section', 1, 1, [1540973], 'bce', 1, 1684]
(Other Static bar information was documented'nce'[1540974], ['Other Static bar information was documented', 1, 1, [1541001], 'nce', 1, 1707])])
#type of result[n.id]
>>> <class 'collections.OrderedDict'>
#print(new_)
Other (please provide detail in Comments section)'bce'[1541001]
#print(type(new_))
<class 'smd.lib.asynch.calibration.dummy'>
class dummy(object):
def __init__(self, attr, error_type, path, with_error=None, total=None,
level=None, order=None):
self.attr = attr
self.error_type = error_type
self.path = path
self.with_error = with_error
self.total = total
self.level = level
self.order = order
def __cmp__(self, other):
return not (
self.attr == other.attr and self.error_type == other.error_type
and self.path == other.path
)
def __base_repr__(self):
return "{0}{1}{2}".format(self.attr, self.error_type, self.path)
def __hash__(self):
return hash(self.__base_repr__())
def __repr__(self):
return self.__base_repr__()
但是,它在使用KeyError
时失败,我不确定这是否是最好的方法。此外,它还可能破坏软件的其他功能
if new_ in result[n.id].keys():
不起作用。我们非常感谢您为解决此问题提供的任何帮助和指导
dummy
类如下所示:
#an example of result[n.id]
>>> ([(Eclipse Required Items'nce'[1540972], ['Eclipse Required Items', 1, 1, [1540972], 'nce', 1, 1681]),
(Other Story Tab Info learned/discovered documentation and accuracy'nce'[1540973], ['Other Story Tab Info learned/discovered documentation and accuracy', 1, 1, [1540973], 'nce', 1, 1684]),
(Other (please provide detail in Comments section)'bce'[1541001], ['Other (please provide detail in Comments section', 1, 1, [1540973], 'bce', 1, 1684]
(Other Static bar information was documented'nce'[1540974], ['Other Static bar information was documented', 1, 1, [1541001], 'nce', 1, 1707])])
#type of result[n.id]
>>> <class 'collections.OrderedDict'>
#print(new_)
Other (please provide detail in Comments section)'bce'[1541001]
#print(type(new_))
<class 'smd.lib.asynch.calibration.dummy'>
class dummy(object):
def __init__(self, attr, error_type, path, with_error=None, total=None,
level=None, order=None):
self.attr = attr
self.error_type = error_type
self.path = path
self.with_error = with_error
self.total = total
self.level = level
self.order = order
def __cmp__(self, other):
return not (
self.attr == other.attr and self.error_type == other.error_type
and self.path == other.path
)
def __base_repr__(self):
return "{0}{1}{2}".format(self.attr, self.error_type, self.path)
def __hash__(self):
return hash(self.__base_repr__())
def __repr__(self):
return self.__base_repr__()
在Python3中,cmp不再使用。
你需要在你的类中实现
\uuuuuuuuuuuueq\uuuuu
,然后删除\uuuucmp\uuuuuu删除\uuuuuucmp\uuuuuuuuuuuu
并改为实现\uuuuuuuuuuuueq\uuu
。@balderman感谢你的洞察力。我会调查一下的。你能推荐一个好的学习资源吗?是一个很好的资源吗?请看@balderman谢谢你的帮助。现在可以了。你介意写个答案吗?我很乐意接受并结束这个问题。很乐意帮忙。写了一份答复。