I'm still fairly new to programming. Python is my first language and I am
teaching myself as best I can. I'm struggling with a situation that I expect
must come up all the time. I can come up with relatively complicated solutions
but I wonder if there's not a more pythonic way of doing it.
I've seen a lot of examples in books for dealing with lists of alternating data
types, but what about a list that doesn't follow a simple numeric pattern? For
example, say I have a list that's a composite of two elements: books and key
pages / albums and favorite tracks / medicines and times taken, whatever. To
make a program that does something to the first set of elements based on the
second set of elements, what kind of structure should I set up?
Probably easier to choose one of these. So pretend I have a list like this:
(Crime and punishment, page 10, page 40, page 30, Brother's Karamazov, page 22,
page 55, page 9000, Father's and Sons, page 100, Anna Karenina, page 1, page 2,
page 4, page 7, page 9)
Since I can identify the elements and since I know the values are 'in order,'
in other words the page numbers between the first and second book all belong to
the first book, I can make a mapping. But I've been surprised at the
complexity. So in this hypothetical, with a regular expression, I can easily
convert the pages to integers, and identify the two lists. But what's the
right way to map them to each other, if I am planning to, for example, tear out
these key pages and make a wall hanging. (I would never do this with precious
books like these, of course). Am I right to think that I want to get them into
a form that clearly relates them to each other from the outset? Does a
dictionary make sense-- I've read that I should expect to put a lot of my data
into dictionaries?
My tentative approach has been as follows:
a. Make a sublist of the Books. Here we could just get the non-integers so
Books = ('C and P', 'Brothers K' ...)
b. Look each up book in the main list to get an index values
c. Now my approach becomes ugly. In pseudo code-
For book in Books:
A dictionary should map the book to a list of all the elements in the main
list that fall between the book's index value and the next book's index value
I keep coming up with embedded loops to express this but I simultaneously feel
like I am missing a third layer (somehow maybe it's 'for book,' 'for index,'
'for element'?) and like Occham is going to come by with his razor and laugh at
me and say, "oh there's a function that does this called the "one to many
mapping function."
I think I'm reading the right books and going to the right web pages and such
to learn, but in this case, I must have just not comprehended. Would be
grateful for any input. Have enjoyed reading the archives of this group as
I've been trying to get my head around programming. Thanks again
Soren
--- On Mon, 7/12/10, [email protected] <[email protected]> wrote:
From: [email protected] <[email protected]>
Subject: Tutor Digest, Vol 77, Issue 32
To: [email protected]
Date: Monday, July 12, 2010, 3:29 PM
Send Tutor mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."
Today's Topics:
1. Re: extract a submatrix (Eike Welk)
2. Re: extract a submatrix (Dave Angel)
3. Re: extract a submatrix (Bala subramanian)
4. Python Documentation Clarification (Huy Ton That)
5. Re: Python Documentation Clarification (Adam Bark)
6. Re: Python Documentation Clarification (Nick Raptis)
----------------------------------------------------------------------
Message: 1
Date: Mon, 12 Jul 2010 12:03:22 +0200
From: Eike Welk <[email protected]>
To: [email protected]
Subject: Re: [Tutor] extract a submatrix
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="iso-8859-15"
Hello Bala!
On Sunday July 11 2010 23:41:14 Bala subramanian wrote:
> I have a
> matrix of size 550,550. I want to extract only part of this matrix say
> first 330 elements, i dnt need the last 220 elements in the matrix. is
> there any function in numpy that can do this kind of extraction.
I demonstrate it with a integer matrix of dimension (5, 10):
In [3]: a = array(range(50)).reshape(5,10)
In [4]: a
Out[4]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]])
In [5]: a[:, 0:7]
Out[5]:
array([[ 0, 1, 2, 3, 4, 5, 6],
[10, 11, 12, 13, 14, 15, 16],
[20, 21, 22, 23, 24, 25, 26],
[30, 31, 32, 33, 34, 35, 36],
[40, 41, 42, 43, 44, 45, 46]])
In [6]: a[:, 7:]
Out[6]:
array([[ 7, 8, 9],
[17, 18, 19],
[27, 28, 29],
[37, 38, 39],
[47, 48, 49]])
The colons ":" denote slices. In a 2D array you can have slices in two
dimensions. in the first dimension (downwards) I always select all elements.
A good explanation of slices is here:
http://tiny.cc/ohl2g
http://stackoverflow.com/questions/509211/good-primer-for-python-slice-
notation
A nice list of Numpy's many functions and methods is here: (This is the Numpy
page I use most often.)
http://tiny.cc/qzwoq
http://www.scipy.org/Numpy_Example_List_With_Doc#head-11717acafb821da646a8db6997e59b820ac8761a
The funny prompt is from IPython (ipython --pylab), a program that enhances
Python's interactive mode, and keeps Matplotlib graphs alive.
Eike.
------------------------------
Message: 2
Date: Mon, 12 Jul 2010 07:17:13 -0400
From: Dave Angel <[email protected]>
To: Bala subramanian <[email protected]>
Cc: [email protected]
Subject: Re: [Tutor] extract a submatrix
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Bala subramanian wrote:
> Friends,
> Excuse me if this question is not appropriate for this forum. I have a
> matrix of size 550,550. I want to extract only part of this matrix say first
> 330 elements, i dnt need the last 220 elements in the matrix. is there any
> function in numpy that can do this kind of extraction. I am quite new to
> numpy. How can do the same ?
>
> Thank you,
> Bala
>
>
I don't know numpy, and it probably would be better to use that forum.
But there are several people here who do, and one of them will probably
help.
However, I would point out that if you fetch the first 220 elements of a
550x550 matrix, you'll have 302170 elements left.
DaveA
------------------------------
Message: 3
Date: Mon, 12 Jul 2010 15:38:36 +0200
From: Bala subramanian <[email protected]>
To: Dave Angel <[email protected]>, [email protected]
Subject: Re: [Tutor] extract a submatrix
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Dear Eike,
Thank you so much, the simple slicing operation solved my problem. Thank you
for the links, i am just going through the same.
Dave
I wanted was to extract a matrix of dimension 330,330 from a matrix of
dimension 550,550. Sorry if my previous post was not clear. I am able to do
it by slicing as suggested by Eike.
Thank you,
Bala
On Mon, Jul 12, 2010 at 1:17 PM, Dave Angel <[email protected]> wrote:
>
>
> Bala subramanian wrote:
>
>> Friends,
>> Excuse me if this question is not appropriate for this forum. I have a
>> matrix of size 550,550. I want to extract only part of this matrix say
>> first
>> 330 elements, i dnt need the last 220 elements in the matrix. is there any
>> function in numpy that can do this kind of extraction. I am quite new to
>> numpy. How can do the same ?
>>
>> Thank you,
>> Bala
>>
>>
>>
> I don't know numpy, and it probably would be better to use that forum. But
> there are several people here who do, and one of them will probably help.
>
> However, I would point out that if you fetch the first 220 elements of a
> 550x550 matrix, you'll have 302170 elements left.
>
> DaveA
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/tutor/attachments/20100712/7c47092c/attachment-0001.html>
------------------------------
Message: 4
Date: Mon, 12 Jul 2010 10:49:04 -0400
From: Huy Ton That <[email protected]>
To: [email protected]
Subject: [Tutor] Python Documentation Clarification
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
This is going to sound silly, but I realized there are some areas within the
documentation that do not make absolute sense to me.
e.g.
compile(source, filename, mode[, flags[, dont_inherit]])
I see within this built in function, the first argument can be what they
define as source, the second argument as the filename and the third as the
mode.
But what confuses me is sometimes I see a bracket, above as [, flags[,
dont_inherit]]. Is this an optional argument like flags=dont_inherit?
Just not grokking it correctly and I can't seem to track down where the
documentation formatting is defined within the python.org documentation...
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/tutor/attachments/20100712/5f601e0f/attachment-0001.html>
------------------------------
Message: 5
Date: Mon, 12 Jul 2010 16:26:20 +0100
From: Adam Bark <[email protected]>
To: tutor-python <[email protected]>
Subject: Re: [Tutor] Python Documentation Clarification
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
On 12 July 2010 15:49, Huy Ton That <[email protected]> wrote:
> This is going to sound silly, but I realized there are some areas within
> the documentation that do not make absolute sense to me.
>
> e.g.
>
> compile(source, filename, mode[, flags[, dont_inherit]])
>
> I see within this built in function, the first argument can be what they
> define as source, the second argument as the filename and the third as the
> mode.
>
> But what confuses me is sometimes I see a bracket, above as [, flags[,
> dont_inherit]]. Is this an optional argument like flags=dont_inherit?
>
> Just not grokking it correctly and I can't seem to track down where the
> documentation formatting is defined within the python.org documentation...
>
>
You're about right really, it's a keyword argument which means it will have
a default so you can specify it or leave the default by ignoring it.
HTH,
Adam.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/tutor/attachments/20100712/166e603d/attachment-0001.html>
------------------------------
Message: 6
Date: Mon, 12 Jul 2010 18:29:47 +0300
From: Nick Raptis <[email protected]>
To: [email protected]
Subject: Re: [Tutor] Python Documentation Clarification
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> compile(source, filename, mode[, flags[, dont_inherit]])
>
> I see within this built in function, the first argument can be what
> they define as source, the second argument as the filename and the
> third as the mode.
>
> But what confuses me is sometimes I see a bracket, above as [, flags[,
> dont_inherit]]. Is this an optional argument like flags=dont_inherit?
>
>
Brackets do indeed mean optional arguments.
So you can do
compile(source, filename, mode, flags=whatever, dont_inherit=True)
or something.
The nested brackets most likely show that (in your example),
dont_inherit is optional, but can be used only if you (optionally) also
provide the flags argument.
Of course, don't take my word for it and read the rest of the
description in the documentation.
Also read here:
<http://docs.python.org/reference/introduction.html?highlight=brackets>http://docs.python.org/reference/introduction.html?highlight=brackets#notation
Nick
------------------------------
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor
End of Tutor Digest, Vol 77, Issue 32
*************************************
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor