跳转到主要内容

额外的Python集合 - 袋(多重集)和集合列表(有序集)

项目描述

Coverage Downloads
文档

http://collections-extended.lenzm.net/

GitHub

https://github.com/mlenzen/collections-extended

PyPI

https://pypi.python.org/pypi/collections-extended

概述

collections_extended 是一个没有依赖的纯Python模块,提供

  • 一个 bag 类,又称 多重集

  • 一个 setlist 类,它是一个 唯一列表有序集

  • 一个 bijection 类,RangeMap,它将范围映射到值,

  • 以及一个 IndexedDict 类,它是一个有序映射,可以使用索引访问其元素,除了键之外。

还有冻结的(可哈希的)袋和集合列表的变体。

兼容并已针对Python 3.6、3.7、3.8、3.9、3.10 & PyPy3进行测试。

入门

>>> from collections_extended import bag, setlist, bijection, RangeMap, IndexedDict
>>> from datetime import date
>>> b = bag('abracadabra')
>>> b.count('a')
5
>>> b.remove('a')
>>> b.count('a')
4
>>> 'a' in b
True
>>> b.count('d')
1
>>> b.remove('d')
>>> b.count('d')
0
>>> 'd' in b
False

>>> sl = setlist('abracadabra')
>>> sl
setlist(('a', 'b', 'r', 'c', 'd'))
>>> sl[3]
'c'
>>> sl[-1]
'd'
>>> 'r' in sl  # testing for inclusion is fast
True
>>> sl.index('d')  # so is finding the index of an element
4
>>> sl.insert(1, 'd')  # inserting an element already in raises a ValueError
Traceback (most recent call last):
...
        raise ValueError
ValueError
>>> sl.index('d')
4

>>> bij = bijection({'a': 1, 'b': 2, 'c': 3})
>>> bij.inverse[2]
'b'
>>> bij['a'] = 2
>>> bij == bijection({'a': 2, 'c': 3})
True
>>> bij.inverse[1] = 'a'
>>> bij == bijection({'a': 1, 'c': 3})
True

>>> version = RangeMap()
>>> version[date(2017, 10, 20): date(2017, 10, 27)] = '0.10.1'
>>> version[date(2017, 10, 27): date(2018, 2, 14)] = '1.0.0'
>>> version[date(2018, 2, 14):] = '1.0.1'
>>> version[date(2017, 10, 24)]
'0.10.1'
>>> version[date(2018, 7, 1)]
'1.0.1'
>>> version[date(2018, 6, 30):] = '1.0.2'
>>> version[date(2018, 7, 1)]
'1.0.2'

>>> idict = IndexedDict()
>>> idict['a'] = "A"
>>> idict['b'] = "B"
>>> idict['c'] = "C"
>>> idict.get(key='a')
'A'
>>> idict.get(index=2)
'C'
>>> idict.index('b')
1

安装

pip 安装 collections-extended

使用方法

from collections_extended import bag, frozenbag, setlist, frozensetlist, bijection

提供了七个新的集合

bag

这是一个包,也称为多重集。

frozenbag

这是包的冻结(可哈希)版本。

集合列表

setlist

一个有序集合或一个唯一元素列表,具体取决于你怎么看待它。

frozensetlist

这是集合列表的冻结(可哈希)版本。

映射

bijection

一对一映射。

RangeMap

从范围(数字/日期等)映射。

IndexedDict

一个保持插入顺序并允许通过索引访问的映射。

Python 2

该包不再支持 Python 2。最后一个支持 Python 2.7、3.4 和 3.5 的版本是 1.0。1.x 版本将不再发布新功能,但任何出现的重大错误可能会被修复。

作者:

Michael Lenzen

版权:

2021 Michael Lenzen

许可证:

Apache License,版本 2.0

项目主页:

https://github.com/mlenzen/collections-extended

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

collections-extended-2.0.2.tar.gz (22.8 kB 查看哈希值)

上传时间

构建分布

collections_extended-2.0.2-py3-none-any.whl (24.3 kB 查看哈希值)

上传时间 Python 3

支持者:

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面