need to get an index for an item in a list

N

nephish

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
 
P

Peter Hansen

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
 
R

Roy Smith

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

Roy Smith

Peter Hansen said:
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.
 
P

Peter Hansen

Roy said:
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
 
N

nephish

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

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top