Skip to content

Weighted Set implementation for Python - a collection of unique keys with attached weights

License

Notifications You must be signed in to change notification settings

Oovvuu/weightedset

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

weightedset: A Weighted Set implementation for Python.

Package version Python versions Monthly downloads

A Weighted Set is a collection of unique keys with attached weights. Keys can be assigned a different (positive) numeric value.

  • A weighted set is not a normal set, because each item in the set has a different weight.
  • A weighted set is like a multi-set, except the weights do not have to be integers (whole numbers).
  • A weighted set is not a dictionary, because the values can only be weights, along with special semantics for adding duplicate keys.

Installation

weightedset is distributed as a standard python package through pypi, so you can install it with your favourite Python package manager. For example:

pip install weightedset

Usage

You can add keys to the set as many times you want. Each time you add a key, it increases that keys weight, using a default weight of 1 if nothing is specified.

>>> from weightedset import WeightedSet
>>> labels = WeightedSet()

>>> labels.add("foo")
>>> labels.add("bar", 1.5)
>>> labels.add("baz")
>>> labels.add("baz")

>>> labels["foo"]
1.0
>>> labels["bar"]
1.5
>>> labels["baz"]
1.5

Normal set-like and dict-like access patterns are supported where they make sense:

>>> labels.keys()
['baz', 'bar', 'foo']
>>> "bar" in labels
True
>>> labels["bar"]
1.5

The weights can be manipulated as a group:

>>> labels *= 2
>>> labels["bar"]
3.0
>>> labels.clamp(1.0)
>>> labels["bar"]
1.0

Licence

This project uses the MIT licence.

Credits

Thanks to Oovvuu for supporting this project.

Primary authors:

About

Weighted Set implementation for Python - a collection of unique keys with attached weights

Topics

Resources

License

Stars

Watchers

Forks

Languages