Git Product home page Git Product logo

randomdict's Introduction

RandomDict Build Status

This is based on robtandy/randomdict, the only reason I forked it was to make the change outlined here: robtandy#1

What is it?

Random dict is a dictionary compatible with python's dict but with a few methods added to facilitate fast random access of elements. It inherits from collections.MutableMapping so it behaves exactly like a python dict, once created.

Why?

Python's dict data structure doesn't provide fast random access to elements. Existing ways are O(n) scaling, and get slow when the number of elements is large.

If you need to randomly access keys in a python dictionary, you have two choices out of the box:

  1. random.sample(the_dict, 1) or random.choice(list(the_dict)) both of these are O(n)
  2. the_dict.pop() This is O(1) but returns an arbitrary, rather than strictly random item. The order of items returned depends on the underlying implentation of the dictionary.

If you need random key access and cannot afford the time penalty of the above methods, then randomdict is probably what you are looking for.

Installation

randomdict works and is tested on python2.6+, python3.2+

pip install -i https://test.pypi.org/simple/ beykylerandomdict

How do i use it?

Other than creating one, use it just like a python dict.

    from beykylerandomdict import RandomDict
    
    r = RandomDict() # use it just like a regular python dict
    r['a'] = True
    r['b'] = 2

    print r.random_key()
    print r.random_value()
    print r.random_item()

How slow was getting a random item anyway?

The following timings where done on python 2.7.3 with ipython:

    In [24]: r = randomdict.RandomDict()

    In [25]: for i in range(10000000): r[i] = random.random()

    In [26]: %timeit random.sample(r,1)
    10 loops, best of 3: 162 ms per loop

    In [27]: %timeit r.random_key()
    1000000 loops, best of 3: 1.74 µs per loop

randomdict's People

Contributors

beykyle avatar robtandy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.