简体   繁体   English

Python:确定顺序中的任何项是否与任何其他项相同

[英]Python: determining whether any item in sequence is equal to any other

I'd like to compare multiple objects and return True only if all objects are not equal among themselves. 我想比较多个对象,只有当所有对象彼此不相等时才返回True I tried using the code below, but it doesn't work. 我尝试使用下面的代码,但它不起作用。 If obj1 and obj3 are equal and obj2 and obj3 are not equal, the result is True . 如果obj1和obj3相等且obj2和obj3不相等,则结果为True

obj1 != obj2 != obj3

I have more than 3 objects to compare. 我有超过3个对象要比较。 Using the code below is out of question: 使用下面的代码是不可能的:

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

@Michael Hoffman's answer is good if the objects are all hashable. @Michael Hoffman的答案很好,如果对象都是可以清洗的。 If not, you can use itertools.combinations : 如果没有,您可以使用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

If the objects are all hashable, then you can see whether a frozenset of the sequence of objects has the same length as the sequence itself: 如果对象都是可散列的,那么您可以看到对象序列的frozenset集是否与序列本身具有相同的长度:

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

Example: 例:

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

If the objects are unhashable but orderable (for example, lists) then you can transform the itertools solution from O(n^2) to O(n log n) by sorting: 如果对象不可删除但是可订购(例如,列表),那么您可以通过排序将itertools解决方案从O(n ^ 2)转换为O(n log n):

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

Here's a full implementation: 这是一个完整的实现:

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))
from itertools import combinations
all(x != y for x, y in combinations(objs, 2))

You can check that all of the items in a list are unique by converting it to a set. 您可以通过将列表中的所有项目转换为集合来检查列表中的所有项目是否都是唯一的。

my_obs = [obj1, obj2, obj3]
all_not_equal = len(set(my_obs)) == len(my_obs)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 确定集合中是否存在任何对象的优雅方法 - Python - Elegant method of determining whether any objects exist in a collection Python:如何检查输入是否不等于二维数组中的任何项目? - Python : How to check if input is not equal to any item in a 2d array? 在Python中查找列表中任何项的最连续序列 - Finding the most consecutive sequence of any item of a list in Python 除了循环之外还有其他方法可以从任何序列数据(Python)更新字典吗? - Is there any alternative ways other than loop to update the dictionary from any sequence data (Python)? Python检查列表项是否包含任何其他列表项 - Python Check if list item does (not) contain any of other list items Python 2.7.10:除了使用len()以外,还有其他方法可以检查输入是否为空 - Python 2.7.10 :Is there any other way to check whether an input is empty other than using len() 在数据框中选择行与列中的任何项目均相等的行 - Selecting Rows in Dataframe that have any column equal to any item in a list 新的 DataFrame boolean 列检查某些列是否等于 1 - New DataFrame boolean column that checks whether or not any of certain columns equal 1 在Python3中,如何创建数值相等但与其他任何级别的嵌套列表无关的任何级别的嵌套列表 - In Python3, how to create a any level nested list which is equal in numeric but independent from other any level nested list Python:元组列表:比较所有元组并检索其中元组的元素不等于任何其他元组的元组 - Python: List of tuples: compare all tuples and retrive tuples where the elements of tuples are not equal to any other tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM