check if the values are prensent in a list of values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • flit

    check if the values are prensent in a list of values

    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,032 54]

    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



  • Emile van Sebille

    #2
    Re: check if the values are prensent in a list of values

    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,032 54]
    >
    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
    >
    >
    >
    --

    >

    Comment

    • Matt Nordhoff

      #3
      Re: check if the values are prensent in a list of values

      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(sor ted(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,032 54]
      >>
      >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
      --

      Comment

      • flit

        #4
        Re: check if the values are prensent in a list of values

        On 9 set, 15:13, Bruno Desthuilliers
        <bdesth.quelque ch...@free.quel quepart.frwrote :
        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

        Comment

        Working...