Python有一个有序字典。那么有序集呢?
当前回答
所以我也有一个小列表,我显然有可能引入非唯一的值。
我搜索是否存在某种唯一列表,但随后意识到在添加元素之前测试元素是否存在就可以了。
if(not new_element in my_list):
my_list.append(new_element)
我不知道这种简单的方法是否需要注意,但它解决了我的问题。
其他回答
正如其他答案所提到的,对于python 3.7+,字典是按定义排序的。我们可以继承abc.collections.MutableSet或typing,而不是继承OrderedDict。MutableSet使用字典的键来存储我们的值。
import itertools
import typing
T = typing.TypeVar("T")
class OrderedSet(typing.MutableSet[T]):
"""A set that preserves insertion order by internally using a dict."""
def __init__(self, iterable: typing.Iterator[T]):
self._d = dict.fromkeys(iterable)
def add(self, x: T) -> None:
self._d[x] = None
def discard(self, x: T) -> None:
self._d.pop(x, None)
def __contains__(self, x: object) -> bool:
return self._d.__contains__(x)
def __len__(self) -> int:
return self._d.__len__()
def __iter__(self) -> typing.Iterator[T]:
return self._d.__iter__()
def __str__(self):
return f"{{{', '.join(str(i) for i in self)}}}"
def __repr__(self):
return f"<OrderedSet {self}>"
然后:
x = OrderedSet([1, 2, -1, "bar"])
x.add(0)
assert list(x) == [1, 2, -1, "bar", 0]
我在一个小库中添加了这些代码和一些测试,所以任何人都可以安装它。
如果您正在使用有序集来维护有序的顺序,请考虑使用来自PyPI的有序集实现。sortedcontainers模块为此提供了一个SortedSet。一些好处:纯python,像c一样快的实现,100%的单元测试覆盖率,数小时的压力测试。
使用pip从PyPI安装很容易:
pip install sortedcontainers
注意,如果不能pip安装,只需从开源存储库中拉出sortedlist.py和sortedset.py文件。
安装完成后,您可以简单地:
from sortedcontainers import SortedSet
help(SortedSet)
sortedcontainers模块还维护了与几个备选实现的性能比较。
对于询问Python的包数据类型的注释,还有一种SortedList数据类型可用于有效地实现包。
有一个pip库是这样做的:
pip install ordered-set
然后你可以使用它:
from ordered_set import OrderedSet
如果您已经在代码中使用了pandas,那么它的Index对象的行为就非常像一个有序集,如本文所示。
文章中的例子:
indA = pd.Index([1, 3, 5, 7, 9])
indB = pd.Index([2, 3, 5, 7, 11])
indA & indB # intersection
indA | indB # union
indA - indB # difference
indA ^ indB # symmetric difference
正如其他人所说,OrderedDict在功能方面是有序集的超集,但如果你需要一个与API交互的集,并且不需要它是可变的,OrderedDict.keys()实际上是一个实现abc.collections.Set:
import random
from collections import OrderedDict, abc
a = list(range(0, 100))
random.shuffle(a)
# True
a == list(OrderedDict((i, 0) for i in a).keys())
# True
isinstance(OrderedDict().keys(), abc.Set)
注意事项是不可变性,必须像字典一样构建集合,但它很简单,只使用内置。
推荐文章
- 如何读取文件的前N行?
- 如何删除matplotlib中的顶部和右侧轴?
- 解析.py文件,读取AST,修改它,然后写回修改后的源代码
- Visual Studio Code:如何调试Python脚本的参数
- 使用元组/列表等等。从输入vs直接引用类型如list/tuple/etc
- 结合conda环境。Yml和PIP requirements.txt
- 将命名元组转换为字典
- 如何使x轴和y轴的刻度相等呢?
- Numpy在这里函数多个条件
- 在Python中,使用argparse只允许正整数
- 如何排序mongodb与pymongo
- 不可变与可变类型
- 列表是线程安全的吗?
- 操作系统。makdirs在我的路径上不理解“~”
- 如何在Django模板中获得我的网站的域名?