我目前正在使用以下函数来比较字典值.有更快或更好的方法吗?
match = True
for keys in dict1:
if dict1[keys] != dict2[keys]:
match = False
print keys
print dict1[keys],
print '->' ,
print dict2[keys]
Run Code Online (Sandbox Code Playgroud)
编辑:两个词都包含相同的键.
我想断言两个Python字典是相等的(这意味着:等量的密钥,每个从键到值的映射是相等的;顺序并不重要).assert A==B然而,一种简单的方法是,如果字典的值是,则这不起作用numpy arrays.如果两个词典相同,我怎样才能编写一个函数来检查?
>>> import numpy as np
>>> A = {1: np.identity(5)}
>>> B = {1: np.identity(5) + np.ones([5,5])}
>>> A == B
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud)
编辑我知道应该检查numpy矩阵是否相等.all().我正在寻找的是检查这一点的一般方法,而无需检查isinstance(np.ndarray).这可能吗?
没有numpy数组的相关主题:
这看似微不足道,但我无法找到确定两个字典是否相等的内置或简单方法。
我想要的是:
a = {'foo': 1, 'bar': 2}
b = {'foo': 1, 'bar': 2}
c = {'bar': 2, 'foo': 1}
b = {'foo': 2, 'bar': 1}
e = {'foo': 1, 'bar': 2, 'baz':3}
f = {'foo': 1}
equal(a, b) # True
equal(a, c) # True - order does not matter
equal(a, d) # False - values do not match
equal(a, e) # False - e has additional elements
equal(a, f) # False - a has additioonal elements
Run Code Online (Sandbox Code Playgroud)
我可以编写一个简短的循环脚本,但是我无法想象我的是一个如此独特的用例
我有两个词典
x = {0:[1,2,3], 1:[1,2,3], 2:[1,2,3]}
y = {0:[1,2,3], 1:[1,2,3], 2:[1,2,3]}
Run Code Online (Sandbox Code Playgroud)
我需要检查这两个词典的相似性.我正在使用以下代码
shared_items = set(x.items()) & set(y.items())
Run Code Online (Sandbox Code Playgroud)
但它给了我
TypeError: unhashable type: 'list' 错误.
这个问题的原因是什么以及如何纠正它.
您可以解决此错误吗:df.isin函数在做什么错?
cursor = con.cursor()
cursor.execute("""SELECT distinct date FROM raw_finmis_online_activation_temp""")
existing_dates = [x[0] for x in cursor.fetchall()]
if df[df['date'].isin(existing_dates)]:
print "Yes it's in there"
else:
print "N"
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误:
ValueError:DataFrame的真值不明确。使用a.empty,a.bool(),a.item(),a.any()或a.all()。
如何仅比较两个字典的值?
所以我有这个:
dict1 = {"appe": 3962.00, "waspeen": 3304.08}
dic2 = {"appel": 3962.00, "waspeen": 3304.08}
def compare_value_dict(dic):
return dic
def compare_value_dict2(dic2):
return dic2
def compare_dic(dic1, dic2):
if dic1 == dic2:
print('the same dictionary')
else:
print('difference dictionary')
compare_dic(compare_value_dict(dict1).values(), compare_value_dict2(dic2.values()))
Run Code Online (Sandbox Code Playgroud)
这有效:
compare_dic(compare_value_dict(dict1).keys(), compare_value_dict2(dic2.keys()))
Run Code Online (Sandbox Code Playgroud) 对于两个字典d1,d2定义为
d1 = {'foo':123, 'bar':789}
d2 = {'bar':789, 'foo':123}
Run Code Online (Sandbox Code Playgroud)
键的顺序在Python 3.6+中保留。当我们遍历字典并打印项目时,这一点很明显。
>>> for x in d1.items():
... print(x)
...
('foo', 123)
('bar', 789)
>>> for x in d2.items():
... print(x)
...
('bar', 789)
('foo', 123)
Run Code Online (Sandbox Code Playgroud)
为什么Python仍然考虑d1并d2保持平等?
>>> d1 == d2
True
Run Code Online (Sandbox Code Playgroud) 是否有更有效的方法来比较两个字典而不是双循环?
for i in d:
for i2 in d2:
if i == i2:
key1 = d.get(i)
key2 = d2.get(i2)
print("First key:", key1)
print("Second key:", key2)
Run Code Online (Sandbox Code Playgroud) python ×8
dictionary ×4
equality ×2
python-3.x ×2
compare ×1
defaultdict ×1
dictview ×1
numpy ×1
pandas ×1
performance ×1