473,597 Members | 2,174 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 11723

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
3491
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
3430
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 wherby I only need to pass it the names of form fields - then my javascript will check each form field...
38
5178
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 all files in the current dir. array contains records of all subs in the current dir
2
3294
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: 3 characters maybe wrong
8
4603
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 happens. compiler: mingw 3.1.01 for win32 command line
9
11673
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 array? Can you think of a way to do it while iterating through the array only once. Is overflow a...
15
1619
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
12192
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 multidimensional array object and resize it afterwards. It seems so easy to conceive of the code to delete...
3
1685
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 the fonction on the top of my program but it messed it up for me. and all my output was invalid. ...
0
7962
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
7884
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8267
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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
5423
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3880
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...
1
2394
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
1229
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.