Re: [Tutor] new to programming and wondering about an IDE for Python on Linux (Robert Sjoblom)

2012-02-28 Thread Joaquim Santos
--

 Message: 1
 Date: Mon, 27 Feb 2012 20:10:42 +0100
 From: Robert Sjoblom robert.sjob...@gmail.com
 To: Alan Gauld alan.ga...@btinternet.com
 Cc: tutor@python.org
 Subject: Re: [Tutor] new to programming and wondering about an IDE for
Python on Linux
 Message-ID:
CAJKU7g03k2e+mJU8SZAmQnEjEEtXnk0x87Df9hhLoscq00=p...@mail.gmail.com
 
 Content-Type: text/plain; charset=ISO-8859-1

  I'd appreciate any feedback on this and good tutorials or books on
  Python 3 and the IDEs suggested. There are many available and I'm
  wondering what you as users find effective.

 I fiddled a bit with the Eric Python IDE; Eric5 for Python3 and Eric4
 for Python2; overall I'd say that Eclipse was a better experience, but
 Eric was by no means bad. I guess it comes down to user preferences.
 As for books, Dive Into Python 3 is one of the better books I've come
 across.

 http://eric-ide.python-projects.org/

 --
 best regards,
 Robert S.




 - My 2 cents on that (being also a beginner...).

A very interesting and customizable IDE for Linux is the Spyder project.
http://code.google.com/p/spyderlib/
It was previously known as Pydee and has lot's of features that I, as a
beginner, find good, like the calltips, function browser, console, online
help... and so on!

Best regards!

Joaquim Santos
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] initialising all elements of a matrix

2012-02-28 Thread Sivaram Neelakantan

I was wondering whether there is a faster/better/cleaner way of
element wise operations of arbitrarily nested list.  I wrote something
like this for 1 level nested lists and am wondering whether there are
any good idioms in python.  I did this after a ridiculous amount of
bad thinking/missteps in python for the simplest of cases.

def init_p (arr):
# input is always 2D matrix; init to uniform probability dist.
q = []
row = len(arr)
col = len(arr[0])
uni_dist = 1.0/(row *col)
q = [ [uni_dist] * col for i in range(row)]
return q

Of course, without using external packages like numpy or any other
 scientific packages.

 sivaram
 -- 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] initialising all elements of a matrix

2012-02-28 Thread Peter Otten
Sivaram Neelakantan wrote:

 
 I was wondering whether there is a faster/better/cleaner way of
 element wise operations of arbitrarily nested list.  I wrote something
 like this for 1 level nested lists and am wondering whether there are
 any good idioms in python.  I did this after a ridiculous amount of
 bad thinking/missteps in python for the simplest of cases.
 
 def init_p (arr):
 # input is always 2D matrix; init to uniform probability dist.
 q = []
 row = len(arr)
 col = len(arr[0])
 uni_dist = 1.0/(row *col)
 q = [ [uni_dist] * col for i in range(row)]
 return q
 
 Of course, without using external packages like numpy or any other
  scientific packages.

Here's how I would break up the problem:

# untested
def nested_list(shape, value):
if len(shape) == 1:
return [value] * shape[0]
return [nested_list(shape[1:], value) for _ in range(shape[0])]

def get_shape(arr):
shape = []
while isinstance(arr, list):
shape.append(len(arr))
arr = arr[0]
return shape

def product(factors, product=1):
for factor in factors:
product *= factor
return product

def init_p(arr):
shape = get_shape(arr)
value = 1.0 / product(shape)
return nested_list(shape, value)

Now you can proceed to improve the parts independently...

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] initialising all elements of a matrix

2012-02-28 Thread bob gailer

On 2/28/2012 11:40 AM, Peter Otten wrote:

def product(factors, product=1):
for factor in factors:
product *= factor
return product

can be simplified

def product(factors):
  import operator
  return reduce(operator.mul, factors)

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] initialising all elements of a matrix

2012-02-28 Thread Peter Otten
bob gailer wrote:

 On 2/28/2012 11:40 AM, Peter Otten wrote:
 
 def product(factors, product=1):
  for factor in factors:
  product *= factor
  return product
 
 can be simplified
 
 def product(factors):
import operator
return reduce(operator.mul, factors)

If I had used the variant with reduce() would you have posted

 def product(factors):
return reduce(operator.mul, factors)

 can be complexified
 
 def product(factors, product=1):
  for factor in factors:
  product *= factor
  return product
 
(Just a pointless thought experiment)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python questions about dictionary loops

2012-02-28 Thread justin fargus
Hello,


I'm new to Python. Just started a few weeks ago and I've been learning some
basic things that I would like to put together but I don't even know where
to began. I am trying to do the following:

Make a program using two sentences of about 8 words (total between the two
sentences).  I would then like to create a dictionary {} and split the
words of each sentence using one sentence as a dictionary key and using the
other sentence for the dictionary value.  I would then like to use a loop
(while or for) that will write out a file that has the dictionary key and
dictionary value to it.  How can I go about doing this? I've noticed with
Python there is more than one way to do most things. I would like the
simplest so that I can practice over and over again on my own until I
understand it. Thanks in advance

Regards,

Justin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python dictionary and loop

2012-02-28 Thread justin fargus
Hello,

I am trying to do the following:

Make a program using two sentences of about 8 words (total between the two
sentences).  I would then like to create a dictionary {} and split the
words of each sentence using one sentence as a dictionary key and using the
other sentence for the dictionary value.  I would then like to use a loop
(while or for) that will write out a file that has the dictionary key and
dictionary value to it.  How can I go about doing this? I've noticed with
Python there is more than one way to do most things. I would like the
simplest so that I can practice over and over again on my own until I
understand it. Thanks in advance


I forgot to add in my first email that I am using Windows Vista 64x and
running Python 3.2.2.

The two sentences I would like to use in the program is the following:

This is line one\nThis is line two! so I write: text_message = This is
line one\nThis is line two!

The dictionary name and key/value pairs will be:

my_sentences {'This':'This','is':'is','line':'line','one':'two'}   # Does
this code split the words of each sentence so that the words in sentence 1
are keys, and the words in sentence 2 are values?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python questions about dictionary loops

2012-02-28 Thread Prasad, Ramit
Justin wrote:


I'm new to Python. Just started a few weeks ago and I've been learning some 
basic things that I would like to put together but I don't even know where to 
began. I am trying to do the following:
Make a program using two sentences of about 8 words (total between the two 
sentences).  I would then like to create a dictionary {} and split the words 
of each sentence using one sentence as a dictionary key and using the other 
sentence for the dictionary value.  I would then like to use a loop (while or 
for) that will write out a file that has the dictionary key and dictionary 
value to it.  How can I go about doing this? I've noticed with Python there is 
more than one way to do most things. I would like the simplest so that I can 
practice over and over again on my own until I understand it. Thanks in advance


Steps:
1. Do you know how to input / retrieve the two sentences? (Look at open() or 
raw_input() if you do not )
2. Do you know how to split strings? Look at the string operations (the python 
web API will show you if you read it)
3. Do you know how to create a dictionary and set values? Play around on the 
interactive prompt if you do not.
4. How do you want to write this out? You have lots of options, look at open(), 
pickle, shelve for just a few options.

Now try and code something together and if you get stuck we can help. If you 
manage to finish it we can help improve it as well.


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python questions about dictionary loops

2012-02-28 Thread Steven D'Aprano

justin fargus wrote:

Hello,


I'm new to Python. Just started a few weeks ago and I've been learning some
basic things that I would like to put together but I don't even know where
to began. I am trying to do the following:

Make a program using two sentences of about 8 words (total between the two
sentences).  I would then like to create a dictionary {} and split the
words of each sentence using one sentence as a dictionary key and using the
other sentence for the dictionary value.  I would then like to use a loop
(while or for) that will write out a file that has the dictionary key and
dictionary value to it.  How can I go about doing this?


You don't tell us what parts of the problem are causing you trouble. Also, I 
don't know if this is homework or not, but a dictionary does not sound like 
the right data structure to use here. What happens if the first sentence (the 
keys) contains duplicate words?



Here are some examples to get you started. Run them in Python, and see what 
they do. They won't solve your problem exactly, but if you study them and see 
what they are doing, you should be able to adapt them to solve your problem. 
If anything is unclear, please ask.



paragraph = This line has one sentence.\nAnd so does this one.
first_line, second_line = paragraph.split('\n')
print(first_line)
print(second_line)

sentence = this is a sentence of seven words
words = sentence.split()
ordinals = 1st 2nd 3rd 4th 5th 6th 7th.split()
for ordinal, word in zip(ordinals, words):
print(%s ::  %s % (ordinal, word))

d = {}
for key, value in zip(['a', 'bb', 'ccc', ''], [1, 2, 3, 4]):
d[key] = value

for k, v in d.items():
print(%s ::  %s % (k, v))



Do you need any of this code explained, or is it clear what it does?


--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python dictionary and loop

2012-02-28 Thread Prasad, Ramit
The two sentences I would like to use in the program is the following:
This is line one\nThis is line two! so I write: text_message = This is line 
one\nThis is line two!
The dictionary name and key/value pairs will be:
my_sentences {'This':'This','is':'is','line':'line','one':'two'}   # Does this 
code split the words of each sentence so that the words in sentence 1 are 
keys, and the words in sentence 2 are values? 

That code does not split anything; it manually hard codes the results you want!
Although the dictionary you show is where each word in sentence one is the key
for the corresponding word in sentence two. Based on your description, I was 
thinking
you wanted something more like my_sentences = { 'This is line one': 'This is 
line two' }

What happens for 1 sentence or 3 sentences? What if the sentences are not the 
same 
length? This type of manipulation makes sense for handling tabular data, but
not sure exactly what your use case is...

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor