473,382 Members | 1,258 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1785
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " ...
18
by: jblazi | last post by:
I should like to search certain characters in a string and when they are found, I want to replace other characters in other strings that are at the same position (for a very simply mastermind game)...
12
by: rbt | last post by:
Not really a Python question... but here goes: Is there a way to read the content of a PDF file and decode it with Python? I'd like to read PDF's, decode them, and then search the data for certain...
3
by: googleboy | last post by:
Hi there. I have defined a class called Item with several (about 30 I think) different attributes (is that the right word in this context?). An abbreviated example of the code for this is: ...
8
by: Allan Ebdrup | last post by:
What would be the fastest way to search 18,000 strings of an average size of 10Kb, I can have all the strings in memory, should I simply do a instr on all of the strings? Or is there a faster way?...
4
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given...
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.