Re: arrays in python

2009-09-23 Thread Simon Forman
On Wed, Sep 23, 2009 at 10:03 PM, AggieDan04  wrote:
> On Sep 23, 3:02 pm, Simon Forman  wrote:
>> On Wed, Sep 23, 2009 at 1:14 PM, Rudolf  wrote:
>> > Can someone tell me how to allocate single and multidimensional arrays
>> > in python. I looked online and it says to do the following x =
>> > ['1','2','3','4']
>>
>> > However, I want a much larger array like a 100 elements, so I cant
>> > possibly do that. I want to allocate an array and then populate it
>> > using a for loop. Thanks for your help.
>> > --
>> >http://mail.python.org/mailman/listinfo/python-list
>>
>> In python they're called 'lists'.  There are C-style array objects but
>> you don't want to use them unless you specifically have to.
> ...
>> But if you do this:
>>
>> two_dimensional_list = [ [] for var in some_iterable]
>>
>> The list of lists you create thereby will contain multiple references
>> to the /same/ inner list object.
>
> No, that creates a list of distinct empty lists.  If you want multiple
> references to the same inner list, it's
>
> inner_list = []
> two_dimensional_list = [inner_list for var in some_iterable]
>
> or
>
> two_dimensional_list = [[]] * num_copies
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Oh, you're right.  I was thinking of the [[]] * n form.  My bad.

~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2009-09-23 Thread AggieDan04
On Sep 23, 3:02 pm, Simon Forman  wrote:
> On Wed, Sep 23, 2009 at 1:14 PM, Rudolf  wrote:
> > Can someone tell me how to allocate single and multidimensional arrays
> > in python. I looked online and it says to do the following x =
> > ['1','2','3','4']
>
> > However, I want a much larger array like a 100 elements, so I cant
> > possibly do that. I want to allocate an array and then populate it
> > using a for loop. Thanks for your help.
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> In python they're called 'lists'.  There are C-style array objects but
> you don't want to use them unless you specifically have to.
...
> But if you do this:
>
> two_dimensional_list = [ [] for var in some_iterable]
>
> The list of lists you create thereby will contain multiple references
> to the /same/ inner list object.

No, that creates a list of distinct empty lists.  If you want multiple
references to the same inner list, it's

inner_list = []
two_dimensional_list = [inner_list for var in some_iterable]

or

two_dimensional_list = [[]] * num_copies
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2009-09-23 Thread Donn
On Wednesday 23 September 2009 22:12:24 Ethan Furman wrote:
> Works great if you want 4,999,999 elements.  ;-)  Omit the '1' if you
> want all five million.
Yes. Fenceposts always get me :)
And I was just reminded that one can:
l=range(500)
\d
-- 
home: http://otherwise.relics.co.za/
2D vector animation : https://savannah.nongnu.org/projects/things/
Font manager : https://savannah.nongnu.org/projects/fontypython/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2009-09-23 Thread Ethan Furman

Donn wrote:

On Wednesday 23 September 2009 19:14:20 Rudolf wrote:


I want to allocate an array and then populate it
using a for loop. 


You don't need to allocate anything, just use the list or dictionary types.

l=[] #empty list
for x in range(1,500):
 l.append(x)

\d


Works great if you want 4,999,999 elements.  ;-)  Omit the '1' if you 
want all five million.


~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2009-09-23 Thread Simon Forman
On Wed, Sep 23, 2009 at 1:22 PM, Donn  wrote:
> On Wednesday 23 September 2009 19:14:20 Rudolf wrote:
>> I want to allocate an array and then populate it
>> using a for loop.
> You don't need to allocate anything, just use the list or dictionary types.
>
> l=[] #empty list
> for x in range(1,500):
>  l.append(x)
>

Of course, in this example you could just say,

l = range(1,500)

Or in python 3,

l = list(range(1,500))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2009-09-23 Thread Simon Forman
On Wed, Sep 23, 2009 at 1:14 PM, Rudolf  wrote:
> Can someone tell me how to allocate single and multidimensional arrays
> in python. I looked online and it says to do the following x =
> ['1','2','3','4']
>
> However, I want a much larger array like a 100 elements, so I cant
> possibly do that. I want to allocate an array and then populate it
> using a for loop. Thanks for your help.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

In python they're called 'lists'.  There are C-style array objects but
you don't want to use them unless you specifically have to.

You can create an empty list like so:

x = []

and then put items into it with a for loop like so:

for item in some_iterable:
x.append(item)

But in simple cases like this there's also the "list comprehension"
syntax which will do it all in one step:

x = [item for item in some_iterable]

But again in a case like this, if you're simply populating a list from
an iterable source you would just say:

x = list(some_iterable)

For multidimensional 'arrays' you just put lists in lists.  (Or use
NumPy if you really want arrays and are doing lots of wild processing
on them.)

But if you do this:

two_dimensional_list = [ [] for var in some_iterable]

The list of lists you create thereby will contain multiple references
to the /same/ inner list object.  This is something that catches many
people unawares.  You have to go back to using a for loop:

x = []
for n in range(100):
x.append([(n, m) for m in range(10)])

(That will create a list of one hundred lists, each of which contains
ten tuples.)

Good luck.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2009-09-23 Thread Michel Claveau - MVP
Hi!

See:
   http://docs.python.org/tutorial

(section 5)

@+
-- 
MCI

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2009-09-23 Thread Benjamin Kaplan
On Sep 23, 2009, at 1:16 PM, Rudolf  wrote:

> Can someone tell me how to allocate single and multidimensional arrays
> in python. I looked online and it says to do the following x =
> ['1','2','3','4']
>
> However, I want a much larger array like a 100 elements, so I cant
> possibly do that. I want to allocate an array and then populate it
> using a for loop. Thanks for your help.


Python lists have a dynamic size so you don't usually do that.
Instead, we use list comprehensions to stick the for loop inside the
list declaration.

[str(x) for x in xrange(100)]


>
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2009-09-23 Thread Donn
On Wednesday 23 September 2009 19:14:20 Rudolf wrote:
> I want to allocate an array and then populate it
> using a for loop. 
You don't need to allocate anything, just use the list or dictionary types.

l=[] #empty list
for x in range(1,500):
 l.append(x)

\d
-- 
http://mail.python.org/mailman/listinfo/python-list


arrays in python

2009-09-23 Thread Rudolf
Can someone tell me how to allocate single and multidimensional arrays
in python. I looked online and it says to do the following x =
['1','2','3','4']

However, I want a much larger array like a 100 elements, so I cant
possibly do that. I want to allocate an array and then populate it
using a for loop. Thanks for your help.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using arrays in Python - problems.

2007-10-23 Thread attackwarningred
Thanks very much to those who sent me a reply to my array problem! Its 
now working brilliantly!

Best wishes,
Gareth.

-- 


[EMAIL PROTECTED]
[EMAIL PROTECTED]

665.9238429876 - Number of the Pentium Beast
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using arrays in Python - problems.

2007-10-23 Thread Duncan Smith
[EMAIL PROTECTED] wrote:
> attackwarningred napisa (a):
> 
> 
>>The array F(n) is dynamically allocated earlier on and is sized with
>>reference to shotcount, the number of iterations the model performs. The
>>problem is I can't get something like this to run in Python using numpy,
>>and for the size of the array to be sized dynamically with reference to
>>the variable shotcount. I acknowledge that my knowledge of Python is
>>still really basic (I only started learning it a few days ago) and I'm
>>trying to get out of the Fortran programming mindset but I'm stuck and
>>don't seem to be able to get any further. If anyone could help I'd be
>>really grateful. Thanks very much in advance.
> 
> 
> Hello. If you want your array to be dynamically resized at every loop
> iteration, that might be quite inefficient. How about initialising it
> with a required size?
> 
> F = numpy.array([0]*shotcount)
> for n in xrange(shotcount):
> F[n] = random.lognormvariate(F_mean, F_sd)
> 
> Hope that helps,
> Marek
> 

or,

F = numpy.random.lognormal(F_mean, F_sd, shotcount)

(assuming F_mean, F_sd are the parameters of the distribution, rather
than the actual mean and standard deviation).

Duncan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using arrays in Python - problems.

2007-10-23 Thread Robert Kern
[EMAIL PROTECTED] wrote:
> attackwarningred napisa (a):
> 
>> The array F(n) is dynamically allocated earlier on and is sized with
>> reference to shotcount, the number of iterations the model performs. The
>> problem is I can't get something like this to run in Python using numpy,
>> and for the size of the array to be sized dynamically with reference to
>> the variable shotcount. I acknowledge that my knowledge of Python is
>> still really basic (I only started learning it a few days ago) and I'm
>> trying to get out of the Fortran programming mindset but I'm stuck and
>> don't seem to be able to get any further. If anyone could help I'd be
>> really grateful. Thanks very much in advance.
> 
> Hello. If you want your array to be dynamically resized at every loop
> iteration, that might be quite inefficient. How about initialising it
> with a required size?
> 
> F = numpy.array([0]*shotcount)

A more idiomatic version would be this:

  F = numpy.empty((shotcount,), dtype=float)

attackwarningred, you might want to ask your numpy questions on the
numpy-discussion mailing list:

  http://www.scipy.org/Mailing_Lists

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using arrays in Python - problems.

2007-10-23 Thread marek . rocki

attackwarningred napisa (a):

> The array F(n) is dynamically allocated earlier on and is sized with
> reference to shotcount, the number of iterations the model performs. The
> problem is I can't get something like this to run in Python using numpy,
> and for the size of the array to be sized dynamically with reference to
> the variable shotcount. I acknowledge that my knowledge of Python is
> still really basic (I only started learning it a few days ago) and I'm
> trying to get out of the Fortran programming mindset but I'm stuck and
> don't seem to be able to get any further. If anyone could help I'd be
> really grateful. Thanks very much in advance.

Hello. If you want your array to be dynamically resized at every loop
iteration, that might be quite inefficient. How about initialising it
with a required size?

F = numpy.array([0]*shotcount)
for n in xrange(shotcount):
F[n] = random.lognormvariate(F_mean, F_sd)

Hope that helps,
Marek

-- 
http://mail.python.org/mailman/listinfo/python-list


Using arrays in Python - problems.

2007-10-23 Thread attackwarningred
Dear All,
 Hello! I've just started to use Python and its a lovely 
language! I've previously programmed in Fortran 95 and have just began 
to use numpy. I'm having a few problems with arrays in Python though and 
wondered if someone could offer me some advice?
I wrote the following Fortran code to randomly generate numbers from 
a log-normal distribution for use in a Monte Carlo model:

do n=1,shotcount
F(n)=G05DEF(F_mean,F_sd)
enddo

The array F(n) is dynamically allocated earlier on and is sized with 
reference to shotcount, the number of iterations the model performs. The 
problem is I can't get something like this to run in Python using numpy, 
and for the size of the array to be sized dynamically with reference to 
the variable shotcount. I acknowledge that my knowledge of Python is 
still really basic (I only started learning it a few days ago) and I'm 
trying to get out of the Fortran programming mindset but I'm stuck and 
don't seem to be able to get any further. If anyone could help I'd be 
really grateful. Thanks very much in advance.

Best wishes,
Gareth.

-- 

[EMAIL PROTECTED]

665.9238429876 - Number of the Pentium Beast
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2006-02-10 Thread plahey
> Oh, don't tell me, I love playing guessing games!

Don't you mean "No no... don't tell me. I'm keen to guess."

Sorry, I couldn't resist... :-)

(for those who just went huh?, see
http://www.aldo.com/sgt/CheeseShoppeSkit.htm)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2006-02-10 Thread Steven D'Aprano
On Fri, 10 Feb 2006 17:50:21 -0500, Kermit Rose wrote:

> I want to write a program in python using integer arrays.
>  
> I wish to calculate formulas using 200 digit integers.

Must the integers have exactly 200 digits? If you multiply one of these
200-digit integers by ten, should it silently overflow, raise an
exception, or become a 201-digit integer?

[snip]

> The zeros function always gives me an error message.

Oh, don't tell me, I love playing guessing games!

Is it a SyntaxError?



-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2006-02-10 Thread Schüle Daniel
> I want to write a program in python using integer arrays.

you can :)


> I wish to calculate formulas using 200 digit integers.

no problem

> I could not find any documentation in python manual about declaring arrays.
>  
> I searched the internet

read here
http://diveintopython.org/native_data_types/lists.html

maybe list are what you are looking for

> and found an example that said I must declare
>  
> from Numeric import *

yes, one can use Numeric for this taks too

> and I downloaded a numerical python  extension,
>  
> but still  have not found a way to declare an array of given length.
>  
> The zeros function always gives me an error message.

 >>> import Numeric as N, random as rand
 >>> nums = [33 ** rand.randint(10,100) for i in range(200)]
 >>> len(nums)
200
 >>> nums[0]
1666465812864030391541732975677083441749008906546726522024522041932256405404932170047036994592860856233379702595619607481259213235163454890913L
 >>>
 >>> a = N.array(nums)
 >>> len(a)
200
 >>> a[0]
1666465812864030391541732975677083441749008906546726522024522041932256405404932170047036994592860856233379702595619607481259213235163454890913L
 >>>


by the way, you know you can use interactive Python iterpreter
and there is dir and help function

 >>> dir(a)
['__copy__', '__deepcopy__', 'astype', 'byteswapped', 'copy', 
'iscontiguous', 'itemsize', 'resize', 'savespace', 'spacesaver', 
'tolist', 'toscalar', 'tostring', 'typecode']
 >>> dir(nums)
['__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']
 >>>

Regards, Daniel

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2006-02-10 Thread Steve Holden
Kermit Rose wrote:
> From: Kermit Rose
> Date: 02/10/06 17:36:34
> To: [EMAIL PROTECTED]
> Subject: Arrays
>  
>   
> Hello.
>  
> I want to write a program in python using integer arrays.
>  
> I wish to calculate formulas using 200 digit integers.
>  
> I could not find any documentation in python manual about declaring arrays.
>  
> I searched the internet
>  
> and found an example that said I must declare
>  
> from Numeric import *
>  
>  
> and I downloaded a numerical python  extension,
>  
> but still  have not found a way to declare an array of given length.
>  
> The zeros function always gives me an error message.
>  

No need to do anything special. Python allows long integers, and you can 
calculate directly with them:

  >>> big1 = int('1' + 100*'2')
  >>> big2 = int('2' + 100*'3')
  >>> big1
1222
2L
  >>> big2
2333
3L
  >>> big1 + big2
3555
5L
  >>>

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

-- 
http://mail.python.org/mailman/listinfo/python-list


arrays in python

2006-02-10 Thread Kermit Rose
From: Kermit Rose
Date: 02/10/06 17:36:34
To: [EMAIL PROTECTED]
Subject: Arrays
 
  
Hello.
 
I want to write a program in python using integer arrays.
 
I wish to calculate formulas using 200 digit integers.
 
I could not find any documentation in python manual about declaring arrays.
 
I searched the internet
 
and found an example that said I must declare
 
from Numeric import *
 
 
and I downloaded a numerical python  extension,
 
but still  have not found a way to declare an array of given length.
 
The zeros function always gives me an error message.
 
 
Kermit   <[EMAIL PROTECTED] >
 
 
 
 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Long integer arrays in Python; how? /Carl

2005-11-28 Thread Robert Kern
Carl wrote:
> I have the following problem 
> 
> import Numeric
> dim = 1
> bits = 32
> v = Numeric.zeros((dim, bits), 'l')
> for j in range(bits):
> v[0][j] = 1L << bits - j - 1
> 
> The problem is the last assignment, which is not valid, since the integer is
> on the right hand side is to large to be assigned to an array element.

Use Numeric.UnsignedInt32 as the data type.

-- 
Robert Kern
[EMAIL PROTECTED]

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter

-- 
http://mail.python.org/mailman/listinfo/python-list


Long integer arrays in Python; how? /Carl

2005-11-28 Thread Carl
I have the following problem 

import Numeric
dim = 1
bits = 32
v = Numeric.zeros((dim, bits), 'l')
for j in range(bits):
v[0][j] = 1L << bits - j - 1

The problem is the last assignment, which is not valid, since the integer is
on the right hand side is to large to be assigned to an array element.

Is there a way around this problem in Python?

Yours /Carl
-- 
http://mail.python.org/mailman/listinfo/python-list