I have a two dicts that I'm converting bot to tuples . Both dict contain the same elements, but they are not generated using the same logic .
我有两个dicts,我正在将bot转换为元组。两个dict都包含相同的元素,但它们不是使用相同的逻辑生成的。
For example let's say I have a tuple like this :
例如,假设我有一个这样的元组:
(('a',5),('n',4),('c',8))
And the 2nd tuple is like that :
而第二个元组是这样的:
(('c',8),('n',4),('a',5))
Their original dicts is like that (probably, I can't know how will the elements be ordered in the dict however they are generated from two dicts that contains the same elements (no more, no less) :
他们原来的说法是这样的(可能,我不知道如何在dict中对元素进行排序,但是它们是由包含相同元素的两个dicts生成的(不多也不少):
{'a':5,'c': 8,'n':4}
For a human being both tuples are similar, but for a computer they are not .
对于一个人而言,两个元组都是相似的,但对于一个计算机则不然。
How can I go with checking if two tuples are similar ?
我怎样才能检查两个元组是否相似?
5 个解决方案
#1
4
Sort both the tuple
and compare them. For example:
对元组进行排序并进行比较。例如:
>>> tuple_1 = (('a',5),('n',4),('c',8))
>>> tuple_2 = (('c',8),('n',4),('a',5))
# Non-sorted --> unequal; issue you are currently facing
>>> tuple_1 == tuple_2
False
# comparing sorted tuples -- equal
>>> sorted(tuple_1) == sorted(tuple_2)
True
If the elements of both the tuples are unique, you may also compare them via using set
as:
如果两个元组的元素都是唯一的,您也可以通过使用set来比较它们:
>>> set(tuple_1) == set(tuple_2)
True
As a side note, you do not have to convert the dict
to tuple
in order to compare the content of dictionaries. You may directly compare the dict
objects as:
作为旁注,您不必将字典转换为元组以便比较字典的内容。您可以直接将dict对象比较为:
>>> {1: 2, 3: 4} == {3: 4, 1: 2}
True
#2
2
You could also convert the tuples to a dict
and then compare them.
您还可以将元组转换为dict然后进行比较。
Not sure if this is faster/slower/the same as sorting them.
不确定这是否更快/更慢/与排序相同。
dict(t1) == dict(t2)
#3
1
def similarTuple(a,b):
h = {}
for e in a:
h[e] = True
for e in b:
if e not in h:
return False
return True
#4
0
You could also make a set of both tuples and compare those:
您还可以创建一组两个元组并进行比较:
a = (('a', 0), ('b', 1), ('c', 2))
b = (('b', 1), ('a', 0), ('c', 2))
set(a) == set(b)
#5
0
trying for a one liner, exploring getting a bool result from list comprehensions
尝试一个班轮,探索从列表推导得到一个bool结果
not efficient, possibly amusing
没有效率,可能很有趣
def SameElementsP(a,b):
return not ( [1 for e in a if e not in b] or [1 for e in b if e not in a] )
#1
4
Sort both the tuple
and compare them. For example:
对元组进行排序并进行比较。例如:
>>> tuple_1 = (('a',5),('n',4),('c',8))
>>> tuple_2 = (('c',8),('n',4),('a',5))
# Non-sorted --> unequal; issue you are currently facing
>>> tuple_1 == tuple_2
False
# comparing sorted tuples -- equal
>>> sorted(tuple_1) == sorted(tuple_2)
True
If the elements of both the tuples are unique, you may also compare them via using set
as:
如果两个元组的元素都是唯一的,您也可以通过使用set来比较它们:
>>> set(tuple_1) == set(tuple_2)
True
As a side note, you do not have to convert the dict
to tuple
in order to compare the content of dictionaries. You may directly compare the dict
objects as:
作为旁注,您不必将字典转换为元组以便比较字典的内容。您可以直接将dict对象比较为:
>>> {1: 2, 3: 4} == {3: 4, 1: 2}
True
#2
2
You could also convert the tuples to a dict
and then compare them.
您还可以将元组转换为dict然后进行比较。
Not sure if this is faster/slower/the same as sorting them.
不确定这是否更快/更慢/与排序相同。
dict(t1) == dict(t2)
#3
1
def similarTuple(a,b):
h = {}
for e in a:
h[e] = True
for e in b:
if e not in h:
return False
return True
#4
0
You could also make a set of both tuples and compare those:
您还可以创建一组两个元组并进行比较:
a = (('a', 0), ('b', 1), ('c', 2))
b = (('b', 1), ('a', 0), ('c', 2))
set(a) == set(b)
#5
0
trying for a one liner, exploring getting a bool result from list comprehensions
尝试一个班轮,探索从列表推导得到一个bool结果
not efficient, possibly amusing
没有效率,可能很有趣
def SameElementsP(a,b):
return not ( [1 for e in a if e not in b] or [1 for e in b if e not in a] )