2012-07-30 24 views
9

我想比較多個對象,並且僅當所有對象之間的對象不相等時才返回True。我嘗試使用下面的代碼,但它不起作用。如果obj1和obj3相等且obj2和obj3不相等,則結果爲TruePython:確定序列中的任何項是否等於任何其他

obj1 != obj2 != obj3 

我有超過3個對象進行比較。使用下面的代碼是毫無疑問的:

all([obj1 != obj2, obj1 != obj3, obj2 != obj3]) 
+0

是對象的數量固定,或變量?它們是數組的元素還是單個變量? – 2012-07-30 22:06:02

回答

20

@邁克爾霍夫曼的答案是好的,如果對象都是可排列的。如果沒有,你可以使用itertools.combinations

>>> all(a != b for a, b in itertools.combinations(['a', 'b', 'c', 'd', 'a'], 2)) 
False 
>>> all(a != b for a, b in itertools.combinations(['a', 'b', 'c', 'd'], 2)) 
True 
18

如果對象都是可哈希的,那麼你就可以看到物體的序列的frozenset是否具有相同的長度序列本身:

def all_different(objs): 
    return len(frozenset(objs)) == len(objs) 

例子:

>>> all_different([3, 4, 5]) 
True 
>>> all_different([3, 4, 5, 3]) 
False 
+0

該死!打我10秒。 – inspectorG4dget 2012-07-30 19:59:55

+1

請注意,如果對象不可用,這將不起作用。 – BrenBarn 2012-07-30 20:00:33

+0

如果其中一個(或全部)對象不可用(例如列表),則不起作用 – mgilson 2012-07-30 20:01:15

3

您可以檢查所有在列表中的項目都是通過它轉換成一組獨一無二的。

my_obs = [obj1, obj2, obj3] 
all_not_equal = len(set(my_obs)) == len(my_obs) 
4
from itertools import combinations 
all(x != y for x, y in combinations(objs, 2)) 
+1

這是O(n^2)比較 - 一個很大的名單很多。 – inspectorG4dget 2012-07-30 20:15:00

6

如果對象是unhashable但訂購(例如,列表),那麼你可以將來自澳itertools溶液(N^2)至O(N日誌n)的排序:

def all_different(*objs): 
    s = sorted(objs) 
    return all(x != y for x, y in zip(s[:-1], s[1:])) 

這裏是一個全面實施:

def all_different(*objs): 
    try: 
     return len(frozenset(objs)) == len(objs) 
    except TypeError: 
     try: 
      s = sorted(objs) 
      return all(x != y for x, y in zip(s[:-1], s[1:])) 
     except TypeError: 
      return all(x != y for x, y in itertools.combinations(objs, 2)) 
+0

hashability的檢查過於複雜(也有點不對)。在Python中這樣做的慣用方法是嘗試構建'frozenset()'並捕獲'TypeError'(如果失敗)。 (這個測試有點不對,因爲'isinstance(obj,collections.Hashable)'是'True'並不能保證對象實際上可以被哈希,它只會檢查對象是否有一個允許哈希的類型,嘗試' ([],)'作爲反例。) – 2012-07-31 13:17:33

+0

@SvenMarnach謝謝,修正。 – ecatmur 2012-07-31 13:31:45

相關問題