Equivalent of std::set

Discussion in 'Python' started by Nicolas Fleury, Sep 26, 2003.

  1. Hi,

    I haven't work so much in python and I was wondering what python
    programmers use for string collections, like an equivalent of C++
    std::set. Do you use a dictionary with dummy values? Tuple/list? Is
    there any other structure in some module with uniqueness ensured and
    lookup optimised? I'm only interested in storing strings in that structure.

    Thx for help

    Nicolas
     
    Nicolas Fleury, Sep 26, 2003
    #1
    1. Advertising

  2. Nicolas Fleury

    anton muhin Guest

    Nicolas Fleury wrote:
    > Hi,
    >
    > I haven't work so much in python and I was wondering what python
    > programmers use for string collections, like an equivalent of C++
    > std::set. Do you use a dictionary with dummy values? Tuple/list? Is
    > there any other structure in some module with uniqueness ensured and
    > lookup optimised? I'm only interested in storing strings in that
    > structure.
    >
    > Thx for help
    >
    > Nicolas
    >


    Python 2.3 introduced Set type, in the former versions common idiom was
    to use dictionaries with dummies IMHO.

    anton.
     
    anton muhin, Sep 26, 2003
    #2
    1. Advertising

  3. On Fri, 26 Sep 2003 10:07:20 -0400, Nicolas Fleury
    <_remove_the_> wrote:

    >Hi,
    >
    >I haven't work so much in python and I was wondering what python
    >programmers use for string collections, like an equivalent of C++
    >std::set. Do you use a dictionary with dummy values? Tuple/list? Is
    >there any other structure in some module with uniqueness ensured and
    >lookup optimised? I'm only interested in storing strings in that structure.
    >
    >Thx for help
    >
    >Nicolas


    A set type was introduced in Python 2.3...

    http://www.python.org/doc/2.3/whatsnew/whatsnew23.html

    http://www.python.org/doc/2.3/lib/module-sets.html

    There are other ways, depending on what you are doing with the
    strings, though.

    For instance, although you can iterate a set, the iteration will not
    necessarily be in order (I assume it uses hashing internally, probably
    by using a dictionary internally). If you need to access the strings
    in order (and there aren't too many of them) you can keep the items in
    a sorted list, in which case the bisect module will probably be a big
    help.


    --
    Steve Horne

    steve at ninereeds dot fsnet dot co dot uk
     
    Stephen Horne, Sep 26, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page