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 4 11708
PhilC <pc****@philc.net> 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
That was quick :)
Thank you very much indeed.
Regards
Phil
On Fri, 29 Oct 2004 18:47:20 -0700, Josiah Carlson <jc******@uci.edu>
wrote: PhilC <pc****@philc.net> 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |