Python quot;setquot; with duplicate/repeated elements(Python“设置带有重复/重复的元素)
问题描述
是否有一种标准方式来表示可以包含重复元素的集合".
据我了解,一个集合恰好有一个元素或零个元素.我希望功能有任何数字.
我目前正在使用以元素为键、数量为值的字典,但这似乎是错误的,原因有很多.
动机:我相信这样的收藏有很多应用.例如,对最喜欢的颜色的调查可以表示为:调查 = ['蓝色','红色','蓝色','绿色']
在这里,我不关心订单,但我关心数量.我想做这样的事情:
survey.add('blue')# 会给出调查 == ['blue', 'red', 'blue', 'green', 'blue']
...甚至可能
survey.remove('blue')# 会给出调查 == ['blue', 'red', 'green']
注意事项:是的,set 不是这种集合的正确术语.还有更正确的吗?
列表当然可以,但所需的集合是无序的.更不用说集合的方法命名在我看来更合适.
您正在寻找 multiset.p>
Python 最接近的数据类型是 collections.Counter
:
Counter
是一个 dict
子类,用于计算可散列对象.它是一个无序集合,其中元素存储为字典键和它们的计数存储为字典值.允许计数任何整数值,包括零或负数.Counter
类类似于其他语言中的 bag 或 multisets.
对于多重集的实际实现,请使用 bag
类来自 pypi 上的数据结构包.请注意,这仅适用于 Python 3.如果您需要 Python 2,这里 是为 Python 2.4 编写的 bag
的配方.
Is there a standard way to represent a "set" that can contain duplicate elements.
As I understand it, a set has exactly one or zero of an element. I want functionality to have any number.
I am currently using a dictionary with elements as keys, and quantity as values, but this seems wrong for many reasons.
Motivation: I believe there are many applications for such a collection. For example, a survey of favourite colours could be represented by: survey = ['blue', 'red', 'blue', 'green']
Here, I do not care about the order, but I do about quantities. I want to do things like:
survey.add('blue')
# would give survey == ['blue', 'red', 'blue', 'green', 'blue']
...and maybe even
survey.remove('blue')
# would give survey == ['blue', 'red', 'green']
Notes: Yes, set is not the correct term for this kind of collection. Is there a more correct one?
A list of course would work, but the collection required is unordered. Not to mention that the method naming for sets seems to me to be more appropriate.
You are looking for a multiset.
Python's closest datatype is collections.Counter
:
A
Counter
is adict
subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. TheCounter
class is similar to bags or multisets in other languages.
For an actual implementation of a multiset, use the bag
class from the data-structures package on pypi. Note that this is for Python 3 only. If you need Python 2, here is a recipe for a bag
written for Python 2.4.
这篇关于Python“设置"带有重复/重复的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python“设置"带有重复/重复的元素


基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01