searching strings using variables

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

    searching strings using variables

    Hi, all. Another bewildered newbie struggling with Python goodness. This
    time it's searching strings. The goal is to search a string for a value.
    The string is a variable I assigned the name 'myvar'. however, it
    doesn't seem to be seeing it... Here's a snippet.

    import re

    # list of items to search...
    mylist = [ 5 , 6 , 16 , 17 , 18 , 19 , 20 , 21 ]
    # my variable I want to search with...
    myvar = '16'
    print re.search('myva r','mylist')

    .... just returns none. Tried it also with...

    mylist.index('m yvar')

    to see if I could spook it out but I get a ValueError (not in list) so
    it looks like it won't see it either. I did vague permutations trying to
    make it work but no go. I'm thinking it may be one of those "forest for
    the trees" things, i've been looking at it too hard. Any ideas?

    many thanks in advance!

    tom
  • Xavier Combelle

    #2
    Re: searching strings using variables

    It appears that giving the folowing list:[color=blue]
    > mylist = [ 5 , 6 , 16 , 17 , 18 , 19 , 20 , 21 ]
    > # my variable I want to search with...[/color]
    and the folowwing variable[color=blue]
    > myvar = '16'[/color]
    the simple way to find the var in the list is[color=blue]
    > mylist.index('m yvar')[/color]
    but that fail:

    It seems that it's a problems of quotes:
    doing that seems work better:

    mylist = [ 5 , 6 , 16 , 17 , 18 , 19 , 20 , 21 ]
    # my variable I want to search with...
    myvar = 16
    print mylist.index(my var)

    I'm not very experimented in python,
    but, it seems that in python, use quote just to write literal
    strings. you can't find a string in a list of int.

    Comment

    • Larry Bates

      #3
      Re: searching strings using variables

      Your first attempt is searching for the characters
      '16' in a list of integers, which will never be found
      and you don't need regular expression overhead to do
      this. You might try.

      # list of items to search...
      mylist = ['5', '6', '16', '17', '18', '19', '20', '21']
      # my variable I want to search with...
      myvar = '16'
      print mylist.index(my var)

      or

      # list of items to search...
      mylist = [5, 6, 16, 17, 18, 19, 20, 21]
      # my variable I want to search with...
      myvar = 16
      print mylist.index(my var)

      on the second example you are searching for the characters
      'myvar' in the same list of integers, which will never
      be found, unless you have something like:

      # list of items to search...
      mylist = ['5', '6', '16', '17', '18', '19', '20', '21', 'myvar']
      print mylist.index('m yvar')

      HTH,
      Larry Bates
      Syscon, Inc.

      "tgiles" <tgiles@nospamm ing.kc.rr.com> wrote in message
      news:WNxzc.2279 8$Fd.18743@twis ter.rdc-kc.rr.com...[color=blue]
      > Hi, all. Another bewildered newbie struggling with Python goodness. This
      > time it's searching strings. The goal is to search a string for a value.
      > The string is a variable I assigned the name 'myvar'. however, it
      > doesn't seem to be seeing it... Here's a snippet.
      >
      > import re
      >
      > # list of items to search...
      > mylist = [ 5 , 6 , 16 , 17 , 18 , 19 , 20 , 21 ]
      > # my variable I want to search with...
      > myvar = '16'
      > print re.search('myva r','mylist')
      >
      > ... just returns none. Tried it also with...
      >
      > mylist.index('m yvar')
      >
      > to see if I could spook it out but I get a ValueError (not in list) so
      > it looks like it won't see it either. I did vague permutations trying to
      > make it work but no go. I'm thinking it may be one of those "forest for
      > the trees" things, i've been looking at it too hard. Any ideas?
      >
      > many thanks in advance!
      >
      > tom[/color]


      Comment

      • Dennis Lee Bieber

        #4
        Re: searching strings using variables

        On Tue, 15 Jun 2004 07:44:54 GMT, tgiles <tgiles@nospamm ing.kc.rr.com>
        declaimed the following in comp.lang.pytho n:
        [color=blue]
        > # list of items to search...
        > mylist = [ 5 , 6 , 16 , 17 , 18 , 19 , 20 , 21 ][/color]

        This is a list of integer values
        [color=blue]
        > # my variable I want to search with...
        > myvar = '16'[/color]

        This is a character string containing two characters: "1"
        followed by "6"
        [color=blue]
        > print re.search('myva r','mylist')[/color]

        With the ' marks, you have two separate character strings: a
        string containing the value "myvar" and a string containing the value
        "mylist"... Neither is a reference to the variables you initiated
        earlier.
        [color=blue]
        > mylist.index('m yvar')
        >[/color]
        Probably better... At least you are invoking a method on the
        variable mylist -- but you still have a string literal of "myvar".

        Try removing ALL of your ' marks (since your list contains
        integers, you don't want myvar to contain a string...
        [color=blue][color=green][color=darkred]
        >>> mylist = [5, 6, 16, 17, 18, 19, 20, 21]
        >>> myvar = 16
        >>> mylist.index(my var)[/color][/color][/color]
        2[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        " and ' can both be used for string literals (as long as they
        match on each end).
        [color=blue][color=green][color=darkred]
        >>> mylist = [5, 6, 16, '16', 17, 18, 19, 20, 21][/color][/color][/color]

        note how Python lists can contain mixed types of items -- the first 16
        is an integer, the second is a string literal
        [color=blue][color=green][color=darkred]
        >>> myvar = "16"[/color][/color][/color]

        so here, using " instead of ', is a string literal again
        [color=blue][color=green][color=darkred]
        >>> mylist.index(my var)[/color][/color][/color]
        3[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        but NO " or ' on that line... you still want it to refer to the myvar
        variable, not to a literal string that contains the name myvar.

        --[color=blue]
        > =============== =============== =============== =============== == <
        > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
        > wulfraed@dm.net | Bestiaria Support Staff <
        > =============== =============== =============== =============== == <
        > Home Page: <http://www.dm.net/~wulfraed/> <
        > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

        Comment

        • Sylvain Hellegouarch

          #5
          Re: searching strings using variables

          Hi tom,

          why not trying a :

          if int(myvar) in mylist:
          print "OK"
          else:
          print "Not in"

          - Sylvain

          tgiles wrote:
          [color=blue]
          > Hi, all. Another bewildered newbie struggling with Python goodness. This
          > time it's searching strings. The goal is to search a string for a value.
          > The string is a variable I assigned the name 'myvar'. however, it
          > doesn't seem to be seeing it... Here's a snippet.
          >
          > import re
          >
          > # list of items to search...
          > mylist = [ 5 , 6 , 16 , 17 , 18 , 19 , 20 , 21 ]
          > # my variable I want to search with...
          > myvar = '16'
          > print re.search('myva r','mylist')
          >
          > ... just returns none. Tried it also with...
          >
          > mylist.index('m yvar')
          >
          > to see if I could spook it out but I get a ValueError (not in list) so
          > it looks like it won't see it either. I did vague permutations trying to
          > make it work but no go. I'm thinking it may be one of those "forest for
          > the trees" things, i've been looking at it too hard. Any ideas?
          >
          > many thanks in advance!
          >
          > tom[/color]


          Comment

          Working...