开发者

Comparing `list`s in python

I have multiple lists. I need to find a way to g开发者_如何学Cenerate list of unique items in each list as compared with all the lists. Is there any simple or straight forward way to do this. I know that these lists can basically be used as sets.


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]

so

uniques([1,1,2,3,5], [2,3,4,5], [3,3,3,9])  ->  [[1], [4], [9]]


Use the set class and the set operations defined therein:

>>> l1 = [1,2,3,4,5,5]
>>> l2 = [3,4,4,6,7]
>>> set(l1) ^ set(l2)    # symmetric difference
set([1, 2, 5, 6, 7])

edit: Ah, misread your question. If you meant, "unique elements in l1 that is not in any of l2, l3, ..., ln, then:

l1set = set(l1)
for L in list_of_lists:   # list_of_lists = [l2, l3, ..., ln]
    l1set = l1set - set(L)


l1 = [4, 6, 3, 7]
l2 = [5, 5, 3, 1]
l3 = [2, 5, 4, 3]
l4 = [9, 8, 7, 6]

# first find the union of the "other" lists
l_union = reduce(set.union, (map(set, (l1, l2, l3))))

# then subtract the union from the remaining list
uniques = set(l4) - l_union

print uniques

and the result:

>>> set([8, 9])


for l in lists:
  s = set(l)
  for o in lists:
    if o != l:
      s -= set(o)

  # At this point, s holds the items unique to l

For efficiency, you could convert all the lists to sets once.


import itertools

# Test set
lists = []
lists.append([1,2,3,4,5,5])
lists.append([3,4,4,6,7])
lists.append([7,])
lists.append([8,9])
lists.append([10,10])

# Join all the lists removing the duplicates in each list
join_lists = []
for list_ in lists:
    join_lists.extend(set(list_))

# First, sort
join_lists.sort()

# Then, list only the groups with one element
print [ key for key, grp in itertools.groupby(join_lists) if len(list(grp)) < 2 ]

#>>> [1, 2, 6, 8, 9]

###
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜