associative array

2010-03-31 Thread Javier Montoya
Dear all,

I'm a newbie in python and would be acknowledge if somebody could shed
some light on associative arrays.
More precisely, I would like to create a multi-dimensional associative
array. I have for example a list of students which are identified
uniquely by their student IDs. Additionally, for each student I have
some information: FirstName, LastName, etc.

The array would have then the following form:
[StudentID] => [FirstName][LastName][Telephone]...[ ... ]

I would like to be able to access a field directly by using a
StudentID
[StudentID][FirstName]
[StudentID][LastName]

How could I manipulate such an array (create the array, add elements,
access data)?

Best wishes


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


Re: associative array

2010-04-01 Thread Javier Montoya
On Mar 31, 7:36 pm, Gary Herron  wrote:
> JavierMontoyawrote:
> > Dear all,
>
> > I'm a newbie in python and would be acknowledge if somebody could shed
> > some light on associative arrays.
> > More precisely, I would like to create a multi-dimensional associative
> > array. I have for example a list of students which are identified
> > uniquely by their student IDs. Additionally, for each student I have
> > some information: FirstName, LastName, etc.
>
> > The array would have then the following form:
> > [StudentID] => [FirstName][LastName][Telephone]...[ ... ]
>
> > I would like to be able to access a field directly by using a
> > StudentID
> > [StudentID][FirstName]
> > [StudentID][LastName]
>
> > How could I manipulate such an array (create the array, add elements,
> > access data)?
>
> > Best wishes
>
> Create a class for student with attributes for ID, FirstName, LastName, etc.
>
>   class Student:
>       def __init__(self, id, FirstName, ...):
>           self.id = id
>           self.FirstName = FirstName
>           ...
>
> then whenever you create a student object, use a dictionary to associate
> the object with its is
>   AA = {} # An empty dictionary
>   s = Student(...)
>   AA[s.id] = s
>   ... and repeat for many students...
>
> Then to access a student's object given an id:
>   s = AA[id]
>   print s.id, s.FirstName, s.LastName, ...
>
> I'd *not* call this a multi-dimension association, but rather just an
> association between student objects and their ids.
>
> Hope that helps,
>
> Gary Herron
>
> --
> Gary Herron, PhD.
> Department of Computer Science
> DigiPen Institute of Technology
> (425) 895-4418

Dear all,

Thanks for your suggestions, it worked! As Gary suggested, I created a
'student' class and mapped its objects to a dictionary.
Is it possible to sort the dictionary by the student's grades in
descending order?

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


remove elements incrementally from a list

2010-05-19 Thread Javier Montoya
Dear all,

I've a list of float numbers and I would like to delete incrementally
a set of elements in a given range of indexes, sth. like:

for j in range(beginIndex, endIndex+1):
   print ("remove [%d] => val: %g" % (j, myList[j]))
   del myList[j]

However, since I'm iterating over the same list, the indexes (range)
are not valid any more for the new list.
Does anybody has some suggestions on how to delete the elements
properly?

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


Re: remove elements incrementally from a list

2010-05-19 Thread Javier Montoya
On May 19, 4:06 pm, Steven D'Aprano  wrote:
> On Wed, 19 May 2010 03:53:44 -0700, Javier Montoya wrote:
> > Dear all,
>
> > I've a list of float numbers and I would like to delete incrementally a
> > set of elements in a given range of indexes, sth. like:
>
> > for j in range(beginIndex, endIndex+1):
> >    print ("remove [%d] => val: %g" % (j, myList[j])) del myList[j]
>
> > However, since I'm iterating over the same list, the indexes (range) are
> > not valid any more for the new list. Does anybody has some suggestions
> > on how to delete the elements properly?
>
> Just delete the slice:
>
> del myList[beginIndex:endIndex+1]
>
> For small lists where you are deleting small chunks, this is the
> simplest, most straight-forward way.
>
> Alternatively, create a new list excluding the bits you would have
> deleted, and assign it in place:
>
> myList[:] = myList[:beginIndex] + myList[endIndex+1:]
>
> Note carefully that you aren't just re-binding the name "myList", but
> assigning to the slice myList[:].
>
> Then there is the old-fashioned way: iterate over the list backwards,
> deleting from the end towards the front.
>
> for j in range(endIndex, beginIndex-1, -1):
>     del myList[j]
>
> If your list is HUGE and you have very little memory, this is probably
> the least worst way. It might be slow, but it will work.
>
> Finally, we have this:
>
> myList[beginIndex:] = myList[endIndex+1:]
>
> --
> Steven

Hi guys,

A big thanks to everybody, it's amazing!

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


numpy arrays to python compatible arrays

2010-06-10 Thread Javier Montoya
Dear all,

I'm new to python and have been working with the numpy package. I have
some numpy float arrays (obtained from np.fromfile and np.cov
functions) and would like to convert them to simple python arrays.
I was wondering which is the best way to do that? Is there any
function to do that?

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


Re: numpy arrays to python compatible arrays

2010-06-12 Thread Javier Montoya
On Jun 11, 12:29 am, Martin  wrote:
> On Jun 10, 9:02 pm, Philip Semanchuk  wrote:
>
>
>
> > On Jun 10, 2010, at 9:58 AM,JavierMontoyawrote:
>
> > > Dear all,
>
> > > I'm new to python and have been working with the numpy package. I have
> > > some numpy float arrays (obtained from np.fromfile and np.cov
> > > functions) and would like to convert them to simple python arrays.
> > > I was wondering which is the best way to do that? Is there any
> > > function to do that?
>
> > HiJavier,
> > Since you are new to Python I'll ask whether you want to convert Numpy  
> > arrays to Python arrays (as you stated) or Python lists. Python lists  
> > are used very frequently; Python arrays see very little use outside of  
> > numpy.
>
> > If you can use a Python list, the .tolist() member of the numpy array  
> > object should do the trick.
>
> > bye
> > P
>
> as Philip said...though I very much doubt you really want to do this?
> Why wouldn't you just keep it in a numpy array?

Thanks for the suggestions! The main reason not to use a numpy array
is because I'm
using a package that doesn't work with numpy arrays.
With the .tolist() conversion it's now working fine, thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


non-uniform distribution

2010-06-12 Thread Javier Montoya
Dear all,

I need to generate a vector of random float numbers between [0,1] such
that their sum equals 1 and that are distributed non-uniformly.
Is there any python function that generates such a vector?

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


Re: non-uniform distribution

2010-06-12 Thread Javier Montoya
On Jun 12, 1:08 pm, Etienne Rousee  wrote:
> Le 12/06/2010 12:05, Javier Montoya a écrit :
>
> > I need to generate a vector of random float numbers between [0,1] such
> > that their sum equals 1 and that are distributed non-uniformly.
> > Is there any python function that generates such a vector?
>
> Let f any function (injective is better).
> Let a1,...,an n numbers uniformly distributed.
> Let s the sum of f(a1),...,f(an).
>
> f(a1)/s, ... , f(an)/s is what you want.
>
> You have to choose f to obtain the distribution you prefer.
>
> --
>
> Etienne

Hi Etienne,

Thanks for your suggestion. I ended with following code:
N=5
a=np.random.random_integers(2*N, size=(1.,N))
a=a/float(sum(a))
However, in some cases, the random numbers are even repeated, for
example, I obtained the following sequence:
[0.03846154,  0.03846154,  0.23076923,  0.34615385,  0.34615385]
This is mainly because in random_integers the integers generated might
be repeated.
One solution, would be to set the first parameter in random_integers
to a larger number.
What do you think? Do you suggest any other function instead of
random_integers?

Best wishes

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


Re: non-uniform distribution

2010-06-12 Thread Javier Montoya
On Jun 12, 1:08 pm, Etienne Rousee  wrote:
> Le 12/06/2010 12:05, Javier Montoya a écrit :
>
> > I need to generate a vector of random float numbers between [0,1] such
> > that their sum equals 1 and that are distributed non-uniformly.
> > Is there any python function that generates such a vector?
>
> Let f any function (injective is better).
> Let a1,...,an n numbers uniformly distributed.
> Let s the sum of f(a1),...,f(an).
>
> f(a1)/s, ... , f(an)/s is what you want.
>
> You have to choose f to obtain the distribution you prefer.
>
> --
>
> Etienne

Hi Etienne,

Thanks for your suggestion. I ended with following code:
N=5
a=np.random.random_integers(2*N, size=(1.,N))
a=a/float(sum(a))
However, in some cases, the random numbers are even repeated, for
example, I obtained the following sequence:
[0.03846154,  0.03846154,  0.23076923,  0.34615385,  0.34615385]
This is mainly because in random_integers the integers generated might
be repeated.
One solution, would be to set the first parameter in random_integers
to a larger number.
What do you think? Do you suggest any other function instead of
random_integers?

Best wishes

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


Re: non-uniform distribution

2010-06-12 Thread Javier Montoya
On Jun 12, 2:09 pm, Steven D'Aprano  wrote:
> On Sat, 12 Jun 2010 03:05:43 -0700, Javier Montoya wrote:
> > Dear all,
>
> > I need to generate a vector of random float numbers between [0,1] such
> > that their sum equals 1 and that are distributed non-uniformly. Is there
> > any python function that generates such a vector?
>
> You haven't explained your requirements in anywhere near enough detail.
> Any of these match your description:
>
> [1.0]
> [0.0, 0.0, 0.0, 0.0, 1.0]
> [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.3]
> [0.01, 0.01, 0.01, 0.01, 0.02, 0.02, 0.02, 0.03, 0.03, 0.84]
>
> and many, many more. What do you mean by "non-uniformly"? Do you have any
> specific distribution in mind? Do you have to have a particular number of
> items?
>
> For instance, you could do this:
>
> L = []
> total = 0.0
> while total < 1:
>     x = random.uniform(0, 1-total)
>     L.append(x)
>     total += x
>
> assert sum(L) == total == 1.0
>
> --
> Steven

Hi Steven,

The number of items in the vector is usually between [4,15]. I was
having in mind sth. like:
[0.25, 0.23, 0.30, 0.22] for the 4 elems case.

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


Re: non-uniform distribution

2010-06-12 Thread Javier Montoya
On Jun 12, 3:21 pm, Ian  wrote:
> On 12/06/10 11:05, Javier Montoya wrote:> Dear all,
>
> > I need to generate a vector of random float numbers between [0,1] such
> > that their sum equals 1 and that are distributed non-uniformly.
> > Is there any python function that generates such a vector?
>
> > Best wishes
>
> Hi Javier,
>
> The answer to your question is "No", and the reason is that your
> requirement is impossible.
>
> Whatever distribution you choose - and you have not said what you
> require - will be altered by any scaling to meet the constraint that the
> total is 1.
>
> What exactly are you trying to achieve?
>
> Regards
>
> Ian

Hi Ian, I found that the Dirichlet distribution is suitable for my
case.
Regards
-- 
http://mail.python.org/mailman/listinfo/python-list