473,597 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 18 '05 #1
4 1800
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('m yvar')

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.

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(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" <tg****@nospamm ing.kc.rr.com> wrote in message
news:WN******** **********@twis ter.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('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

Jul 18 '05 #3
On Tue, 15 Jun 2004 07:44:54 GMT, tgiles <tg****@nospamm ing.kc.rr.com>
declaimed the following in comp.lang.pytho n:
# 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('myva r','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('m yvar')
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(my var) 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(my var) 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.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.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('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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
9664
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 " 4". Six spaces than the value of intQuantity. This is correct. But all the others end up being string objects of only 6 characters long (with the exception of strTotal). The left most positions of the string object are being padded with one...
18
2501
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) for my pupils. This very simple thing does not seem simple at all. If I use strings, I cannot replace their parts (though I can use string.find for the searching). I think it is a bad idea that strings are not mutable, but I suspect that...
12
4328
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 strings. Thanks, rbt
3
1862
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: class Item(object): def __init__(self, height, length, function): params = locals()
8
1868
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? I would like to have a kind of search like google where you can enter several words to search for, guess that calls for a regular expression "word1|word2|word3|...". Is there any kind of indexing tools available for this kind of thing, I have my...
4
5329
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 the Identifier. The logical way would be to read the record once and put it in an STL container such as vector and then use lower_bound to search for a given identifier. But for some strange reason i'm asked to not use a container but instead...
4
455
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 COMPACT COHERENT CODE THAT WORKS WITH STRINGS, IT ALL BEGINS TO MAKE SINCE*/ /* The basics of C++ are Classes, that build Types. Which are used to create quick and dirty routines in the smallest possible space. The Classes & Routines uses the...
3
3277
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 "Searching..." page then, refreshes and displays the table I want. I have code that grabs data from the page using cURL but when I look at the data it contains the "Searching..." page and not the table that I want. below is the code i have so far....Thanks...
0
7965
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3881
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2399
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1231
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.