searching strings using variables

T

tgiles

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('myvar','mylist')

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

mylist.index('myvar')

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
 
X

Xavier Combelle

It appears that giving the folowing list:
mylist = [ 5 , 6 , 16 , 17 , 18 , 19 , 20 , 21 ]
# my variable I want to search with... and the folowwing variable
myvar = '16'
the simple way to find the var in the list is
mylist.index('myvar')
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(myvar)

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.
 
S

Sylvain Hellegouarch

Hi tom,

why not trying a :

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

- Sylvain
 
L

Larry Bates

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(myvar)

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(myvar)

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('myvar')

HTH,
Larry Bates
Syscon, Inc.
 
D

Dennis Lee Bieber

# list of items to search...
mylist = [ 5 , 6 , 16 , 17 , 18 , 19 , 20 , 21 ]

This is a list of integer values
# my variable I want to search with...
myvar = '16'

This is a character string containing two characters: "1"
followed by "6"
print re.search('myvar','mylist')

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.
mylist.index('myvar')
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...
mylist = [5, 6, 16, 17, 18, 19, 20, 21]
myvar = 16
mylist.index(myvar) 2

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

note how Python lists can contain mixed types of items -- the first 16
is an integer, the second is a string literal

so here, using " instead of ', is a string literal again

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.

--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top