在python中比较`list`s

Sam*_*Sam 2 python list unique set

我有多个列表.我需要找到一种方法来生成每个列表中的唯一项目列表,与所有列表进行比较.有没有简单或直接的方法来做到这一点.我知道这些列表基本上可以用作sets.

Hug*_*ell 5

import collections

def uniques(*args):
    """For an arbitrary number of sequences,
           return the items in each sequence which
            do not occur in any of the other sequences
    """

    # ensure that each value only occurs once in each sequence
    args = [set(a) for a in args]

    seen = collections.defaultdict(int)
    for a in args:
        for i in a:
            seen[i] += 1
    # seen[i] = number of sequences in which value i occurs

    # for each sequence, return items
    #  which only occur in one sequence (ie this one)
    return [[i for i in a if seen[i]==1] for a in args]
Run Code Online (Sandbox Code Playgroud)

所以

uniques([1,1,2,3,5], [2,3,4,5], [3,3,3,9])  ->  [[1], [4], [9]]
Run Code Online (Sandbox Code Playgroud)