473,549 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding the array index number for known content.

Hi Folks,

If I have an array holding a pair of numbers, and that pairing is
unique, is there a way that I can find the array index number for that
pair?

Thanks,

PhilC
Jul 18 '05 #1
4 11721

PhilC <pc****@philc.n et> wrote:
If I have an array holding a pair of numbers, and that pairing is
unique, is there a way that I can find the array index number for that
pair?


If by "array" you mean "Python list", then yes:
lst.index(pair)

If by "array" you mean "array from the array module", then "pair of
numbers" is just two adjacent numbers that are of your 'pair' value,
then you are going to have to do it by hand...

def find_pair(arry, val1, val2):
for i in xrange(len(arry )-1):
if arry[i] == val1 and arry[i+1] == val2:
return i
raise IndexError, "pair (%s, %s) not found in array"%(val1, val2)
- Josiah

Jul 18 '05 #2
That was quick :)

Thank you very much indeed.

Regards

Phil

On Fri, 29 Oct 2004 18:47:20 -0700, Josiah Carlson <jc******@uci.e du>
wrote:

PhilC <pc****@philc.n et> wrote:
If I have an array holding a pair of numbers, and that pairing is
unique, is there a way that I can find the array index number for that
pair?


If by "array" you mean "Python list", then yes:
lst.index(pair)

If by "array" you mean "array from the array module", then "pair of
numbers" is just two adjacent numbers that are of your 'pair' value,
then you are going to have to do it by hand...

def find_pair(arry, val1, val2):
for i in xrange(len(arry )-1):
if arry[i] == val1 and arry[i+1] == val2:
return i
raise IndexError, "pair (%s, %s) not found in array"%(val1, val2)
- Josiah


Jul 18 '05 #3

Phil> If I have an array holding a pair of numbers, and that pairing is
Phil> unique, is there a way that I can find the array index number for
Phil> that pair?
From the "teach them to fish" department...


Python, being the introspective language that it is, helps you answer these
sorts of questions pretty easily. Your question was in general, "is there a
way to do X with a list?". The first step is to ask a list what it knows
about itself:

% python
Python 2.4b1 (#52, Oct 17 2004, 13:54:51)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1493)] on darwin
Type "help", "copyright" , "credits" or "license" for more information.
dir([]) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute __',
'__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__',
'__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__',
'__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__' ,
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__',
'__setslice__', '__str__', 'append', 'count', 'extend', 'index',
'insert', 'pop', 'remove', 'reverse', 'sort']

Hmmm... there's an "index" attribute. Let's see what that's all about:
help([].index) Help on built-in function index:

index(...)
L.index(value, [start, [stop]]) -> integer -- return first index of
value

Looks promising (as Josiah already suggested it would be).

It's often also helpful to ask for help about an object's type (or class).
List objects are instances of the list type, and there is useful help about
it:
help(list)


Help on class list in module __builtin__:

class list(object)
| list() -> new list
| list(sequence) -> new list initialized from sequence's items
|
| Methods defined here:
|
| __add__(...)
| x.__add__(y) <==> x+y
|
| __contains__(.. .)
| x.__contains__( y) <==> y in x
|
| __delitem__(... )
| x.__delitem__(y ) <==> del x[y]
...

The powers of the help builtin are available via the pydoc command as well,
e.g.:

% pydoc sys
Help on built-in module sys:

NAME
sys

FILE
(built-in)

MODULE DOCS
http://www.python.org/doc/current/lib/module-sys.html

DESCRIPTION
This module provides access to some objects used or maintained by
the interpreter and to functions that interact strongly with the
interpreter.
...

% pydoc list
Help on class list in module __builtin__:

class list(object)
| list() -> new list
| list(sequence) -> new list initialized from sequence's items
|
...

Pydoc even supports a web server interface. Try something like:

pydoc -p 8709

then visit

http://localhost:8709/

To find out about builtin types (and builtin objects in general), click the
__builtin__ link. If you want help on a module the help displayed will
likely have a link to the relevant section of the standard documentation as
well.

Now go fish my son. Don't come back until you have caught your limit of
rainbow trout...

Skip
Jul 18 '05 #4
Hi Skip,

This is very much appreciated. I certainly understand the fishing
concept. I'm probably like many others here in that I have no formal
computer training. (I used to be a plumber :) I think I've downloaded
..... and more to the point read .... most of the major help files but
with this last one I was truly stuck.

Now going to go through your and Josiah's answers in greater depth.

Again my sincere thanks to you both.

Phil
Jul 18 '05 #5

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

Similar topics

5
3489
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use strings as keys: three, four, five, six - do I add the key names in the file?
9
3428
by: Randell D. | last post by:
Folks, I can program fairly comfortably in PHP and can, for the most part using these skills and others that I've picked up over the years manage to read/understand most code in Javascript... so I'm just asking for a few pointers (or the full solution if you have the time) for what I want to do. Basically, I want to write a javascript...
38
5163
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please let's us do not go into an "each dot over i" clarification discussion now - however you want to call - you call it ;-) array contains records of...
2
3289
by: B Moor | last post by:
I have a database with 100,000's records, each with a unique reference, eg A123BNK456 I would like to generate a search facility whereby we can choose an exact match or partial match, where the partial match could be any of the following:- Partial match 1: 1 character maybe wrong Partial match 2: 2 characters maybe wrong Partial match 3:...
8
4599
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line later on. Before that happens there has been a problem that causes a crash of the program. This is a little program "test.exe" I wrote to test what...
9
11669
by: Harsha Srinath | last post by:
Athough this might not be directly relayed to C syntax, I thought some of you may find this an interesting problem :- 1.One number, in an array integers from 1 to 1,000,000 is present twice. How can you determine which one? Can you think of a way to do it using little extra memory? 2. How would you find the only missing element in that...
15
1609
by: pemo | last post by:
Are there standard macros that a (c99) compliant compiler should support? I note that in the c99 std that __FILE__ __LINE__ __func__ are mentioned, but others (that seem to be built in to every compiler I currently use) like __DATE__ __TIME___ don't appear. Presumably, __TIME__ etc are just simply 'popular' rather than required?
10
12182
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to help do the most basic things. One of the "basic" things I haven't been able to find out how to do is how to delete an item from a...
3
1682
by: Ouwet5775 | last post by:
Hey guys i have been working on a C++ program as a revision for an upcoming final exam. One of the main point is to know how to create a function that would invert the elements in your array. That one has given me alot of trouble. A tip or a hint would be greatly appriciate it :D ie: A={1,2,3} -------> A'={3,2,1} Now i have tried using...
0
7471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7740
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7985
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...
1
7503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6071
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...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3517
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...
1
1962
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
0
784
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...

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.