check if the values are prensent in a list of values

Discussion in 'Python' started by Bruno Desthuilliers, Sep 9, 2008.

  1. Matt Nordhoff a écrit :
    (snip)
    > I'm not judging whether this is a good solution or not, but that's a
    > silly use of a dict.


    Yeps, but a somewhat common one in code predating the apparition of sets
    as builtin type.
     
    Bruno Desthuilliers, Sep 9, 2008
    #1
    1. Advertising

  2. Bruno Desthuilliers

    flit Guest

    Hello All,

    I will appreciate the help from the more skillfull pythonistas..

    I have a small app that generates a sequence like

    00341
    01741
    03254

    This values I am putting in a list.

    So I have a list = [00341,01741,03254]

    after the programs find the sequence 03401 this sequence is "new" so
    it appends on the list. But I want to avoid that as the values are
    already on the first sequence of the list (00341).
    If I try to use a "in" statement it will give false. as 00341 is
    different from 00341 (but for my goal not..)


    How can I check against this list and avoid to put "different"
    sequences but same values?

    as 34100 --> dont append on the list
    14300 ---> dont append on the list
    05321 --> append to the list.

    Am I doing some conceptual error using lists?
    There is a better approach?

    Thanks
     
    flit, Sep 9, 2008
    #2
    1. Advertising

  3. flit wrote:
    > Hello All,
    >
    > I will appreciate the help from the more skillfull pythonistas..
    >
    > I have a small app that generates a sequence like
    >
    > 00341
    > 01741
    > 03254


    Consider using a dict with sorted tuple keys, eg

    d = {}

    for seq in ['00341','01741','03254']:
    ky = list(seq)
    ky.sort()
    d[tuple(ky)] = None


    then d.keys() are the unique combinations.

    HTH,

    Emile



    >
    > This values I am putting in a list.
    >
    > So I have a list = [00341,01741,03254]
    >
    > after the programs find the sequence 03401 this sequence is "new" so
    > it appends on the list. But I want to avoid that as the values are
    > already on the first sequence of the list (00341).
    > If I try to use a "in" statement it will give false. as 00341 is
    > different from 00341 (but for my goal not..)
    >
    >
    > How can I check against this list and avoid to put "different"
    > sequences but same values?
    >
    > as 34100 --> dont append on the list
    > 14300 ---> dont append on the list
    > 05321 --> append to the list.
    >
    > Am I doing some conceptual error using lists?
    > There is a better approach?
    >
    > Thanks
    >
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Emile van Sebille, Sep 9, 2008
    #3
  4. On Sep 9, 2:04 pm, flit <> wrote:

    > Hello All,
    >
    > I will appreciate the help from the more skillfull pythonistas..
    >
    > I have a small app that generates a sequence like
    >
    > 00341
    > 01741
    > 03254
    >
    > This values I am putting in a list.
    >
    > So I have a list = [00341,01741,03254]
    >
    > after the programs find the sequence 03401 this sequence is "new" so
    > it appends on the list. But I want to avoid that as the values are
    > already on the first sequence of the list (00341).
    > If I try to use a "in" statement it will give false. as 00341 is
    > different from 00341 (but for my goal not..)
    >
    > How can I check against this list and avoid to put "different"
    > sequences but same values?
    >
    > as 34100 --> dont append on the list
    > 14300 ---> dont append on the list
    > 05321 --> append to the list.
    >
    > Am I doing some conceptual error using lists?
    > There is a better approach?


    Whenever you want to keep track of unique values, think of using a set
    or dict, not list. Since you don't care about the character order
    within each string, sort the characters so that two strings are
    equivalent if and only if their sorted character lists are equal.

    One more thing to bear in mind is that the elements of a set (or the
    keys of a dict) have to be hashable. Lists are not hashable so there
    needs to be an extra step to convert the sorted list into an
    "equivalent" hashable object. A common choice that works for any list
    [*] is to convert it to a tuple. An alternative that works for strings
    only is to join() them into a single string:

    >>> values = '00341 01741 03254 34100 14300 05321'.split()
    >>> set(''.join(sorted(v)) for v in values)

    set(['01235', '00134', '01147', '02345'])

    HTH,
    George

    [*] Assuming that every element of the list is hashable.
     
    George Sakkis, Sep 9, 2008
    #4
  5. Emile van Sebille wrote:
    > flit wrote:
    >> Hello All,
    >>
    >> I will appreciate the help from the more skillfull pythonistas..
    >>
    >> I have a small app that generates a sequence like
    >>
    >> 00341
    >> 01741
    >> 03254

    >
    > Consider using a dict with sorted tuple keys, eg
    >
    > d = {}
    >
    > for seq in ['00341','01741','03254']:
    > ky = list(seq)
    > ky.sort()
    > d[tuple(ky)] = None
    >
    >
    > then d.keys() are the unique combinations.
    >
    > HTH,
    >
    > Emile


    I'm not judging whether this is a good solution or not, but that's a
    silly use of a dict. A set would be better.

    s = set()
    for seq in ['00341','01741','03254']:
    s.add(tuple(sorted(ky)))

    Then you just, well, access the set directly, instead of using d.keys()
    or something.

    (I also replaced the sorting with the sorted() function for brevity.
    This all assumes you have at least Python 2.4...)

    >> This values I am putting in a list.
    >>
    >> So I have a list = [00341,01741,03254]
    >>
    >> after the programs find the sequence 03401 this sequence is "new" so
    >> it appends on the list. But I want to avoid that as the values are
    >> already on the first sequence of the list (00341).
    >> If I try to use a "in" statement it will give false. as 00341 is
    >> different from 00341 (but for my goal not..)
    >>
    >>
    >> How can I check against this list and avoid to put "different"
    >> sequences but same values?
    >>
    >> as 34100 --> dont append on the list
    >> 14300 ---> dont append on the list
    >> 05321 --> append to the list.
    >>
    >> Am I doing some conceptual error using lists?
    >> There is a better approach?
    >>
    >> Thanks

    --
     
    Matt Nordhoff, Sep 9, 2008
    #5
  6. Bruno Desthuilliers

    flit Guest

    On 9 set, 15:13, Bruno Desthuilliers
    <> wrote:
    > Matt Nordhoff a écrit :
    > (snip)
    >
    > > I'm not judging whether this is a good solution or not, but that's a
    > > silly use of a dict.

    >
    > Yeps, but a somewhat common one in code predating the apparition of sets
    > as builtin type.


    Thanks for all contributions, sure I learn a lot with all samples.
    Very good thread and answers.

    Thank you all
     
    flit, Sep 9, 2008
    #6
    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.
Similar Threads
  1. Davisro
    Replies:
    1
    Views:
    707
    Michael D. Ober
    Jun 14, 2004
  2. =?Utf-8?B?QnJlbmRlbiBCaXhsZXI=?=

    Dropdown List - Check if value exists in list prior to selection

    =?Utf-8?B?QnJlbmRlbiBCaXhsZXI=?=, Mar 10, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    2,016
    =?Utf-8?B?QnJlbmRlbg==?=
    Mar 20, 2007
  3. b1caez02
    Replies:
    2
    Views:
    355
    shadowman
    Mar 20, 2007
  4. GIMME
    Replies:
    1
    Views:
    193
    Dr John Stockton
    Apr 9, 2004
  5. babu17
    Replies:
    1
    Views:
    148
Loading...

Share This Page