Python“设置”重复/重复的元素 [英] Python "set" with duplicate/repeated elements

查看:167
本文介绍了Python“设置”重复/重复的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



根据我的理解,一个集合只有一个或者零个元素。我希望功能有任何数字。



我目前正在使用一个字典,其中元素为键,数量为值,但由于许多原因,这似乎是错误的。 >

动机:
我相信有这样一个集合的应用程序很多。例如,对最喜欢的颜色的调查可以用以下代表:
survey = ['blue','red','blue','green']



在这里,我不在乎订单,但是我做数量。我想做这样的事情:

  survey.add('blue')
#会给调查== [ 蓝色,红色,蓝色,绿色,蓝色]

。 ..也许甚至

  survey.remove('blue')
#会给调查== ['蓝色','red','green']

注意:
是的,设置不是这种集合的正确术语。有没有更正确的一个?



列表当然可以工作,但所需的集合是无序的。更不用说,我认为这个方法的命名方式更合适了。

解决方案

你正在寻找一个 multiset



Python最接近的数据类型是 collections.Counter


一个计数器是一个用于计算可哈希对象的 dict 子类。它是一个
无序集合,其中元素存储为字典键,
,它们的计数作为字典值存储。计数允许为
任何整数值,包括零或负数。 counter class
类似于其他语言的行李或多重集。


对于多重集的实际实现,请使用 ppti中的数据结构包中的包 类。请注意,这仅适用于Python 3。如果您需要Python 2,此处是一个袋的配方为Python编写2.4。


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 a dict 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. The Counter 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“设置”重复/重复的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆