Python 如何将可迭代对象的内容添加到集合中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4045403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:59:57  来源:igfitidea点击:

How do I add the contents of an iterable to a set?

pythonsetconventionsiterable

提问by Ian Mackinnon

What is the "one [...] obvious way"to add all items of an iterable to an existing set?

将可迭代的所有项目添加到现有的“一种[...] 明显的方式”set什么?

采纳答案by SingleNegationElimination

You can add elements of a listto a setlike this:

您可以像这样list将 a 的元素添加到 a 中set

>>> foo = set(range(0, 4))
>>> foo
set([0, 1, 2, 3])
>>> foo.update(range(2, 6))
>>> foo
set([0, 1, 2, 3, 4, 5])

回答by pyfunc

Use list comprehension.

使用列表理解。

Short circuiting the creation of iterable using a list for example :)

例如,使用列表缩短可迭代的创建:)

>>> x = [1, 2, 3, 4]
>>> 
>>> k = x.__iter__()
>>> k
<listiterator object at 0x100517490>
>>> l = [y for y in k]
>>> l
[1, 2, 3, 4]
>>> 
>>> z = Set([1,2])
>>> z.update(l)
>>> z
set([1, 2, 3, 4])
>>> 

[Edit: missed the set part of question]

[编辑:错过了问题的设置部分]

回答by jaydel

for item in items:
   extant_set.add(item)

For the record, I think the assertion that "There should be one-- and preferably only one --obvious way to do it." is bogus. It makes an assumption that many technical minded people make, that everyone thinks alike. What is obvious to one person is not so obvious to another.

为了记录,我认为“应该有一种——最好只有一种——明显的方法来做到这一点”。是假的。它做出了许多具有技术头脑的人所做的假设,每个人的想法都一样。对一个人来说显而易见的事情对另一个人来说并不那么明显。

I would argue that my proposed solution is clearly readable, and does what you ask. I don't believe there are any performance hits involved with it--though I admit I might be missing something. But despite all of that, it might not be obvious and preferable to another developer.

我认为我提出的解决方案清晰易读,并且可以满足您的要求。我不相信它会影响任何性能——尽管我承认我可能会遗漏一些东西。但尽管如此,对于其他开发人员来说,它可能并不明显和可取。

回答by gbc

You can use the set() function to convert an iterable into a set, and then use standard set update operator (|=) to add the unique values from your new set into the existing one.

您可以使用 set() 函数将可迭代对象转换为集合,然后使用标准集合更新运算符 (|=) 将新集合中的唯一值添加到现有集合中。

>>> a = { 1, 2, 3 }
>>> b = ( 3, 4, 5 )
>>> a |= set(b)
>>> a
set([1, 2, 3, 4, 5])

回答by John Machin

For the benefit of anyone who might believe e.g. that doing aset.add()in a loop would have performance competitive with doing aset.update(), here's an example of how you can test your beliefs quickly before going public:

为了任何可能相信例如aset.add()在循环中执行会比执行具有竞争力的人的利益aset.update(),这里有一个示例,说明如何在公开之前快速测试您的信念:

>\python27\python -mtimeit -s"it=xrange(10000);a=set(xrange(100))" "a.update(it)"
1000 loops, best of 3: 294 usec per loop

>\python27\python -mtimeit -s"it=xrange(10000);a=set(xrange(100))" "for i in it:a.add(i)"
1000 loops, best of 3: 950 usec per loop

>\python27\python -mtimeit -s"it=xrange(10000);a=set(xrange(100))" "a |= set(it)"
1000 loops, best of 3: 458 usec per loop

>\python27\python -mtimeit -s"it=xrange(20000);a=set(xrange(100))" "a.update(it)"
1000 loops, best of 3: 598 usec per loop

>\python27\python -mtimeit -s"it=xrange(20000);a=set(xrange(100))" "for i in it:a.add(i)"
1000 loops, best of 3: 1.89 msec per loop

>\python27\python -mtimeit -s"it=xrange(20000);a=set(xrange(100))" "a |= set(it)"
1000 loops, best of 3: 891 usec per loop

Looks like the cost per item of the loop approach is over THREE times that of the updateapproach.

看起来循环方法的每项成本是该方法的三倍多update

Using |= set()costs about 1.5x what updatedoes but half of what adding each individual item in a loop does.

使用|= set()成本大约是update在循环中添加每个单独项目的成本的 1.5 倍。

回答by Daniel Dubovski

Just a quick update, timings using python 3:

只是一个快速更新,使用 python 3 计时:

#!/usr/local/bin python3
from timeit import Timer

a = set(range(1, 100000))
b = list(range(50000, 150000))

def one_by_one(s, l):
    for i in l:
        s.add(i)    

def cast_to_list_and_back(s, l):
    s = set(list(s) + l)

def update_set(s,l):
    s.update(l)

results are:

结果是:

one_by_one 10.184448844986036
cast_to_list_and_back 7.969255169969983
update_set 2.212590195937082