比较 python 中的“列表”

发布于 2024-10-17 01:59:38 字数 95 浏览 6 评论 0原文

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

I have multiple lists. I need to find a way to generate 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

记忆之渊 2024-10-24 01:59:38
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]

所以

uniques([1,1,2,3,5], [2,3,4,5], [3,3,3,9])  ->  [[1], [4], [9]]
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]]
流心雨 2024-10-24 01:59:38

使用集合类和其中定义的集合操作:

>>> 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])

编辑:啊,误读了你的问题。如果您的意思是“l1 中的唯一元素不在 l2、l3、...、ln 中的任何一个中,那么:

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

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)
月野兔 2024-10-24 01:59:38
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

结果:

>>> set([8, 9])
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])
自由范儿 2024-10-24 01:59:38
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 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.

你列表最软的妹 2024-10-24 01:59:38
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]

###
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]

###
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文