470,596 Members | 1,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

need to get an index for an item in a list

hey there,
i need to be able to get the index for an item in a list.
the list is a list of lines read from a text file.

like this:

file = open("/home/somefile.text", "r")
lines = file.readlines()
file.close()

now, i want to see if a certain string is == to one of the lines
and if so, i need to know the index of that line.

any ideas?

thanks

Jul 21 '05 #1
5 1099
ne*****@xit.net wrote:
i need to be able to get the index for an item in a list. any ideas?


Fire up the interactive interpreter and learn to use it to help
yourself. In this case, the most useful thing might be to know about
the dir() builtin method, which you can use on a list like so:

dir([])

Note in the output the presence of the "index" method... I'll leave it
up to you to read the docs to learn more about how to use it, or you can
just experiment at the prompt to see how it works.

-Peter
Jul 21 '05 #2
In article <11**********************@g44g2000cwa.googlegroups .com>,
ne*****@xit.net wrote:
hey there,
i need to be able to get the index for an item in a list.
the list is a list of lines read from a text file.

like this:

file = open("/home/somefile.text", "r")
lines = file.readlines()
file.close()

now, i want to see if a certain string is == to one of the lines
and if so, i need to know the index of that line.


Assuming you're already read the lines from the file with the above code,
something along the lines of the following will work:

for lineNumber, line in enumerate (lines):
whatever

But, starting from scratch, it'll be more efficient to do:

for lineNumber, line in enumerate (file ("filename")):
whatever

because it'll read lines one at a time as needed, instead of gulping them
all in at once and buffering them in memory. For small files (say, less
than a few hundred lines), it probably won't make any appreciable
difference, but for big files, it can be substantial.

BTW, enumerate() starts counting from 0; you might want to add 1 to what it
returns to get a file line number.
Jul 21 '05 #3
In article <Qf********************@powergate.ca>,
Peter Hansen <pe***@engcorp.com> wrote:
ne*****@xit.net wrote:
i need to be able to get the index for an item in a list.

any ideas?


Fire up the interactive interpreter and learn to use it to help
yourself. In this case, the most useful thing might be to know about
the dir() builtin method, which you can use on a list like so:

dir([])

Note in the output the presence of the "index" method... I'll leave it
up to you to read the docs to learn more about how to use it, or you can
just experiment at the prompt to see how it works.

-Peter


I certainly agree that dir() is a very handy tool to know about, and that
poking around with it in the interactive interpreter is a great way to
learn what's possible.

That being said, index() isn't isn't going to work if there are duplicate
lines in the file; it'll return the index of the first one.
Jul 21 '05 #4
Roy Smith wrote:
That being said, index() isn't isn't going to work if there are duplicate
lines in the file; it'll return the index of the first one.


It will still work, if you are willing to do a bit of work to help it:
l = range(10) + [5]
l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5] l.index(5) 5 l.index(5, 5+1)

10

As with str.index(), the one for list takes a second argument that
specifies the index at which to start the search, allowing you to skip
past items that have already been checked.

That said, other approaches (such as Roy suggested in his other post)
may well be more appropriate depending on the context.

-Peter
Jul 21 '05 #5
Hey, thanks, this has worked out for me.
i am trying to do as much of this as possible in IDLE because
it lets me know on the fly what is messed up.
thanks for your help
shawn <><

Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jackson Yap | last post: by
8 posts views Thread by andrewfelch | last post: by
11 posts views Thread by Santosh | last post: by
reply views Thread by satan | last post: by
3 posts views Thread by satan | last post: by
3 posts views Thread by Kevin Walzer | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.