473,395 Members | 1,379 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,395 software developers and data experts.

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

Similar topics

6
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've...
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
4
by: Steph. | last post by:
I have a List view displaying data in Detail mode with several columns. How I can get the column index the user clicked on ? (when user click on an item inside the ListView, not on a column...
8
by: andrewfelch | last post by:
I write a lot of code that looks like this: for myElement, elementIndex in zip( elementList, range(len(elementList))): print "myElement ", myElement, " at index: ",elementIndex My question...
11
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
0
by: satan | last post by:
The others classes public class OrderedArrayList extends ArrayListClass { //default constructor public OrderedArrayList() { super(); }
3
by: satan | last post by:
I'm having problem to test a recursive version of a binary search algorithm. These are my codes: public class OrderedArrayList extends ArrayListClass { //default constructor public...
3
by: Kevin Walzer | last post by:
I'm trying to set the active item in a Tkinter listbox to my application's currently-defined default font. Here's how I get the fonts loaded into the listbox: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.