Python 理解 set() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15181867/
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:33:36  来源:igfitidea点击:

Understanding the set() function

pythonset

提问by Prakhar Mehrotra

In python, set()is an unordered collection with no duplicate elements. However, I am not able to understand how it generates the output.

在python中,set()是一个没有重复元素的无序集合。但是,我无法理解它是如何生成输出的。

For example, consider the following:

例如,请考虑以下情况:

>>> x = [1, 1, 2, 2, 2, 2, 2, 3, 3]
>>> set(x)
set([1, 2, 3])

>>> y = [1, 1, 6, 6, 6, 6, 6, 8, 8]
>>> set(y)
set([8, 1, 6])

>>> z = [1, 1, 6, 6, 6, 6, 6, 7, 7]
>>> set(z)
set([1, 6, 7])

Shouldn't the output of set(y)be: set([1, 6, 8])? I tried the above two in Python 2.6.

的输出不应该set(y)是:set([1, 6, 8])?我在 Python 2.6 中尝试了上述两个。

采纳答案by user

Sets are unordered, as you say. Even though one way to implement sets is using a tree, they can also be implemented using a hash table (meaning getting the keys in sorted order may not be that trivial).

正如你所说,集合是无序的。尽管实现集合的一种方法是使用树,但它们也可以使用哈希表实现(意味着按排序顺序获取键可能不是那么简单)。

If you'd like to sort them, you can simply perform:

如果你想对它们进行排序,你可以简单地执行:

sorted(set(y))

which will produce a sorted list containing the set's elements. (Not a set. Again, sets are unordered.)

这将产生一个包含集合元素的排序列表。(不是集合。同样,集合是无序的。)

Otherwise, the only thing guaranteed by setis that it makes the elements unique (nothing will be there more than once).

否则,唯一能保证的set就是它使元素独一无二(不会出现多次)。

Hope this helps!

希望这可以帮助!

回答by I82Much

As +Volatility and yourself pointed out, sets are unordered. If you need the elements to be in order, just call sortedon the set:

正如 +Volatility 和你自己指出的那样,集合是无序的。如果您需要按顺序排列元素,只需调用sorted集合:

>>> y = [1, 1, 6, 6, 6, 6, 6, 8, 8]
>>> sorted(set(y))
[1, 6, 8]

回答by James Henstridge

As an unordered collection type, set([8, 1, 6])is equivalent to set([1, 6, 8]).

作为无序集合类型,set([8, 1, 6])相当于set([1, 6, 8]).

While it might be nicer to display the set contents in sorted order, that would make the repr()call more expensive.

虽然按排序顺序显示设置内容可能会更好,但这会使repr()调用成本更高。

Internally, the settype is implemented using a hash table: a hash function is used to separate items into a number of buckets to reduce the number of equality operations needed to check if an item is part of the set.

在内部,该set类型是使用散列表实现的:散列函数用于将项目分成多个桶,以减少检查项目是否属于集合的一部分所需的相等操作的数量。

To produce the repr()output it just outputs the items from each bucket in turn, which is unlikely to be the sorted order.

为了产生repr()输出,它只是依次输出每个桶中的项目,这不太可能是排序顺序。

回答by Blckknght

Python's sets (and dictionaries) will iterate and print out in someorder, but exactly what that order will be is arbitrary, and not guaranteed to remain the same after additions and removals.

Python 的集合(和字典)将按某种顺序迭代和打印出来,但该顺序究竟是什么是任意的,并且不能保证在添加和删除后保持不变。

Here's an example of a set changing order after a lot of values are added and then removed:

下面是在添加和删除大量值后设置更改顺序的示例:

>>> s = set([1,6,8])
>>> print(s)
{8, 1, 6}
>>> s.update(range(10,100000))
>>> for v in range(10, 100000):
    s.remove(v)
>>> print(s)
{1, 6, 8}

This is implementation dependent though, and so you should not rely upon it.

不过,这取决于实现,因此您不应依赖它。

回答by Ania

After reading the other answers, I still had trouble understanding whythe set comes out un-ordered.

阅读其他答案后,我仍然无法理解为什么该集合是无序的。

Mentioned this to my partner and he came up with this metaphor: take marbles. You put them in a tube a tad wider than marble width : you have a list. A set, however, is a bag. Even though you feed the marbles one-by-one into the bag; when you pour them from a bag back into the tube, they will not be in the same order (because they got all mixed up in a bag).

向我的搭档提到这一点,他想出了这个比喻:拿弹珠。你把它们放在一个比大理石宽度稍宽的管子里:你有一个清单。然而,一套就是一个包。即使你把弹珠一个一个地放进袋子里;当您将它们从袋子中倒回管中时,它们的顺序不会相同(因为它们都混在了一个袋子中)。