468,253 Members | 1,269 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

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('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
Jul 18 '05 #1
4 1543
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.

Jul 18 '05 #2
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.

"tgiles" <tg****@nospamming.kc.rr.com> wrote in message
news:WN******************@twister.rdc-kc.rr.com...
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

Jul 18 '05 #3
On Tue, 15 Jun 2004 07:44:54 GMT, tgiles <tg****@nospamming.kc.rr.com>
declaimed the following in comp.lang.python:
# 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
myvar = "16"
so here, using " instead of ', is a string literal again
mylist.index(myvar) 3


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.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #4
Hi tom,

why not trying a :

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

- Sylvain

tgiles wrote:
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

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Adrian Parker | last post: by
12 posts views Thread by rbt | last post: by
3 posts views Thread by googleboy | last post: by
3 posts views Thread by Aaron | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.