Simple - looking for a way to do an element exists check..

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

    Simple - looking for a way to do an element exists check..

    Hi all,

    I have a simple list to which I want to append another tuple if
    element 0 is not found anywhere in the list.

    element = ('/smsc/chp/aztec/padlib/5VT.Cat',
    '/smsc/chp/aztec/padlib',
    '5VT.Cat', (33060))

    element1 = ('/smsc/chp/aztec/padlib/5VT.Cat2',
    '/smsc/chp/aztec/padlib',
    '5VT.Cat2', (33060))

    a = [ ('/smsc/chp/aztec/padlib/5VT.Cat',
    '/smsc/chp/aztec/padlib',
    '5VT.Cat', (33060)),
    ('/smsc/chp/aztec/padlib/padlib.TopCat%' ,
    '/smsc/chp/aztec/padlib',
    'padlib.TopCat% ', (33204)),
    ('/smsc/chp/aztec/padlib/Regulators.Cat% ',
    '/smsc/chp/aztec/padlib',
    'Regulators.Cat %', (33204))]

    So my code would look something like this.

    found = False
    for item in a:
    if item[0] == element[0]
    found = True
    break
    if not found:
    a.append(elemen t)

    But this is just ugly - Is there a simpler way to interate over all
    items in a without using a found flag?

    Thanks


  • Paul McGuire

    #2
    Re: Simple - looking for a way to do an element exists check..

    On Feb 22, 11:20 am, rh0dium <steven.kl...@g mail.comwrote:
    >
    found = False
    for item in a:
      if item[0] == element[0]
        found = True
        break
    if not found:
      a.append(elemen t)
    >
    But this is just ugly - Is there a simpler way to interate over all
    items in a without using a found flag?
    >
    Thanks

    for item in a:
    if item[0] == element[0]
    break
    else: # only called if we never 'break' out of the for loop
    a.append(elemen t)


    But what about a dict?

    adict = dict((elem[0],elem) for elem in a)

    if item[0] not in adict:
    adict[item[0]] = item

    # need the final list?
    a = adict.values()

    No list searching, and will scale well if a gets real long.

    -- Paul

    Comment

    • Paul McGuire

      #3
      Re: Simple - looking for a way to do an element exists check..

      On Feb 22, 11:20 am, rh0dium <steven.kl...@g mail.comwrote:
      Hi all,
      >
      I have a simple list to which I want to append another tuple if
      element 0 is not found anywhere in the list.
      >
      element =  ('/smsc/chp/aztec/padlib/5VT.Cat',
        '/smsc/chp/aztec/padlib',
        '5VT.Cat', (33060))
      >
      element1 =  ('/smsc/chp/aztec/padlib/5VT.Cat2',
        '/smsc/chp/aztec/padlib',
        '5VT.Cat2', (33060))
      >
      a =  [ ('/smsc/chp/aztec/padlib/5VT.Cat',
        '/smsc/chp/aztec/padlib',
        '5VT.Cat', (33060)),
       ('/smsc/chp/aztec/padlib/padlib.TopCat%' ,
        '/smsc/chp/aztec/padlib',
        'padlib.TopCat% ', (33204)),
       ('/smsc/chp/aztec/padlib/Regulators.Cat% ',
        '/smsc/chp/aztec/padlib',
        'Regulators.Cat %', (33204))]
      >
      So my code would look something like this.
      >
      found = False
      for item in a:
        if item[0] == element[0]
          found = True
          break
      if not found:
        a.append(elemen t)
      >
      But this is just ugly - Is there a simpler way to interate over all
      items in a without using a found flag?
      >
      Thanks
      Well, that's what I get for typing before thinking...

      If the remaining items in each element tuple are the same for any
      given element[0], then just use a set.

      aset = set(a)
      for element in list_of_new_ele ment_tuples:
      aset.add(elemen t)

      -- Paul

      Comment

      • Jason

        #4
        Re: Simple - looking for a way to do an element exists check..

        On Feb 22, 10:20 am, rh0dium <steven.kl...@g mail.comwrote:
        Hi all,
        >
        I have a simple list to which I want to append another tuple if
        element 0 is not found anywhere in the list.
        >
        element = ('/smsc/chp/aztec/padlib/5VT.Cat',
        '/smsc/chp/aztec/padlib',
        '5VT.Cat', (33060))
        >
        element1 = ('/smsc/chp/aztec/padlib/5VT.Cat2',
        '/smsc/chp/aztec/padlib',
        '5VT.Cat2', (33060))
        >
        a = [ ('/smsc/chp/aztec/padlib/5VT.Cat',
        '/smsc/chp/aztec/padlib',
        '5VT.Cat', (33060)),
        ('/smsc/chp/aztec/padlib/padlib.TopCat%' ,
        '/smsc/chp/aztec/padlib',
        'padlib.TopCat% ', (33204)),
        ('/smsc/chp/aztec/padlib/Regulators.Cat% ',
        '/smsc/chp/aztec/padlib',
        'Regulators.Cat %', (33204))]
        >
        So my code would look something like this.
        >
        found = False
        for item in a:
        if item[0] == element[0]
        found = True
        break
        if not found:
        a.append(elemen t)
        >
        But this is just ugly - Is there a simpler way to interate over all
        items in a without using a found flag?
        >
        Thanks
        How-about using a generator expression and Python's built-in "in"
        operator:
        >>def example(myData, newData):
        .... if newData[0] not in (x[0] for x in myData):
        .... myData.append( newData )
        ....
        >>l = []
        >>example( l, ('a', 'apple', 'aviary') )
        >>l
        [('a', 'apple', 'aviary')]
        >>example( l, ('s', 'spam', 'silly') )
        >>l
        [('a', 'apple', 'aviary'), ('s', 'spam', 'silly')]
        >>example( l, ('s', 'suck-tastic') )
        >>l
        [('a', 'apple', 'aviary'), ('s', 'spam', 'silly')]
        >>>

        Comment

        • Paul Rubin

          #5
          Re: Simple - looking for a way to do an element exists check..

          Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrites:
          if any(x==element[0] for x in a):
          a.append(elemen t)
          Should say:

          if any(x[0]==element[0] for x in a):
          a.append(elemen t)

          Comment

          • Paul McGuire

            #6
            Re: Simple - looking for a way to do an element exists check..

            On Feb 22, 12:54 pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
            Paul Rubin <http://phr...@NOSPAM.i nvalidwrites:
                if any(x==element[0] for x in a):
                  a.append(elemen t)
            >
            Should say:
            >
                 if any(x[0]==element[0] for x in a):
                    a.append(elemen t)
            I think you have this backwards. Should be:

            if not any(x[0]==element[0] for x in a):
            a.append(elemen t)

            or

            if all(x[0]!=element[0] for x in a):
            a.append(elemen t)

            -- Paul

            Comment

            • Paul Rubin

              #7
              Re: Simple - looking for a way to do an element exists check..

              Paul McGuire <ptmcg@austin.r r.comwrites:
              I think you have this backwards. Should be:
              >
              if not any(x[0]==element[0] for x in a):
              a.append(elemen t)
              I think you are right, it was too early for me to be reading code when
              I posted that ;-)

              Comment

              • Boris Ozegovic

                #8
                Re: Simple - looking for a way to do an element exists check..

                Paul Rubin wrote:
                if any(x[0]==element[0] for x in a):
                How come this list comprehension isn't in [] brackets?

                Comment

                • TeroV

                  #9
                  Re: Simple - looking for a way to do an element exists check..

                  Boris Ozegovic wrote:
                  Paul Rubin wrote:
                  >
                  > if any(x[0]==element[0] for x in a):
                  >
                  How come this list comprehension isn't in [] brackets?
                  It isn't list comprehension, it is generator expression


                  --
                  Tero

                  Comment

                  • Boris Ozegovic

                    #10
                    Re: Simple - looking for a way to do an element exists check..

                    TeroV wrote:
                    It isn't list comprehension, it is generator expression
                    http://en.wikipedia.org/wiki/Python_...or_expressions
                    Nice. :)

                    Comment

                    • Paul Hankin

                      #11
                      Re: Simple - looking for a way to do an element exists check..

                      On Feb 22, 7:01 pm, Paul McGuire <pt...@austin.r r.comwrote:
                      On Feb 22, 12:54 pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
                      >
                      Paul Rubin <http://phr...@NOSPAM.i nvalidwrites:
                          if any(x==element[0] for x in a):
                            a.append(elemen t)
                      >
                      Should say:
                      >
                           if any(x[0]==element[0] for x in a):
                              a.append(elemen t)
                      >
                      I think you have this backwards.  Should be:
                      >
                           if not any(x[0]==element[0] for x in a):
                              a.append(elemen t)
                      IMO Jason's solution of testing containment in a generator is better
                      (more readable).
                      if element[0] not in (x[0] for x in a):
                      a.append(elemen t)

                      --
                      Paul Hankin

                      Comment

                      • Marc 'BlackJack' Rintsch

                        #12
                        Re: Simple - looking for a way to do an element exists check..

                        On Sat, 23 Feb 2008 17:19:47 -0800, thebjorn wrote:
                        On Feb 23, 6:18 pm, Paul Hankin <paul.han...@gm ail.comwrote:
                        >
                        >IMO Jason's solution of testing containment in a generator is better
                        >(more readable).
                        > if element[0] not in (x[0] for x in a):
                        > a.append(elemen t)
                        >
                        It may be more readable (although that's debatable), but it always
                        traverses the entire list.
                        The ``not in`` stops if the element is found.

                        Ciao,
                        Marc 'BlackJack' Rintsch

                        Comment

                        Working...