Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old August 15th, 2003, 11:04 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
Bidimensional arrays in Python

Hi, my problem is simple : I'm trying to make some 2x2 matrix operations, similar to the C code like
Code:
int a[4][15]

(not necessarily int). And honestly, I have no clue about how to do that in Python. So how can I do that ?

I COULD make a "flat", one-dimensional array, and take each x'th element, but I'd be surprised if nobody ever worked with Python and matrices.
__________________
Time is the greatest of teachers ; sadly, it kills all of its students.
- Hector Berlioz

Reply With Quote
  #2  
Old August 16th, 2003, 01:40 AM
jeych jeych is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 6 jeych User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello,

I'm not trained in Python but isn't :

Code:
>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> a[0]
[1, 2, 3]
>>> a[0][0]
1
>>> a[2][2]
9


what you're looking for ? Just an array of arrays

Julien.

Reply With Quote
  #3  
Old August 16th, 2003, 07:37 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Also, note that what you will be creating is not an array, but a list. A mutable list. You can't directly create an empty array, but you can of course create an array filled with default values:
Code:
[[0 for i in range(2)] for i in range(2)]

will return:
Code:
[[0, 0], [0, 0]]

For more info, see Python Tutorial: List Comprehensions

Last edited by percivall : August 16th, 2003 at 07:40 AM.

Reply With Quote
  #4  
Old August 16th, 2003, 10:04 AM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
Ah, that'll teach me to RTFM. Thanks.

Reply With Quote
  #5  
Old August 18th, 2003, 07:42 AM
FiveGrainJa FiveGrainJa is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Alexandria, VA
Posts: 5 FiveGrainJa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Another way of doing the same thing:

Code:
[ [0] * 2 ] * 2


This is essentially the same as the list comprehension in the earlier post, but a little less typing.

Reply With Quote
  #6  
Old August 19th, 2003, 08:48 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Both do the same thing but making a list of 0 is pretty pointless . The first list compression does allow you to do allot more than the recusion though..

Code:
[[i for i in range(10)] for i in range(2)]


This will at least produce two list of numbers 0-9, but then why this would be useful I just don't know, ah maybe one day I will find a use for it

Thanks for the info though,

Take care guys,
Mark.

Reply With Quote
  #7  
Old August 19th, 2003, 09:18 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
List comprehension is most powerful when combined with a function, such as:
Code:
[(lambda i: [i, chr(i)])(i) for i in range(65, 123)]


This is of course also possible with a regular list comprehension...
Code:
[[i, chr(i)] for i in range(65, 123)]


Okay, not very useful. Anyway, it lets you do stuff in a nice way.

Last edited by percivall : August 19th, 2003 at 09:29 AM.

Reply With Quote
  #8  
Old August 19th, 2003, 04:03 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I'd be enclined to use the second . Smaller and and nicer to look at, plus since it does less (doesnt create a lambda) it should be slightly more efficent (in theory). Does give some idea of whats possible. Good for setting up lists .

good to know thanks,
Mark.

Reply With Quote
  #9  
Old September 4th, 2003, 08:08 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Quote:
Originally posted by FiveGrainJa
Another way of doing the same thing:

Code:
[ [0] * 2 ] * 2


This is essentially the same as the list comprehension in the earlier post, but a little less typing.

Actually, you don't want to do this, because of the following:

Code:
>>> a = [[0]*2]*2
>>> a
[[0, 0], [0, 0]]
>>> a[0][0] = 1
>>> a
[[1, 0], [1, 0]]
>>>


see note 2 on this page for more details: http://www.python.org/doc/current/lib/typesseq.html
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Bidimensional arrays in Python


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway