[Tutor] idiomatic way of merging two dictionaries

2009-11-03 Thread Amit Sethi
Hi , I am trying to merge(I am not sure if that is the right term)
dictionaries like this

dict1 ={'a':4,'b':3,'v':7,'h':4}
dict2={'a':5,'v':4,'k':3}
dict3 would be {'a':[4,5],'b':[3,4],'v':[7,4],'k':[0,3],'h':[4,0]}

-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Stolen thread: Bottom folk vs. toppers was trouble using2to3.py

2009-11-03 Thread Alan Gauld

"Robert Berman"  wrote


The war between bottom posters and top posters has been long, arduous,
and most often incredibly silly.


It has, I agree but there is a very real difference in that gratuitous top
posting does actually become unreadable if not done very carefully.
And it also encorages people to leave the whole previous thread in
place which is very naughty for those who pay for ther internet
access by the byte! (Or those whose mail server implements
space quotas!)

But if a general comment is being made that does not need to refer
to specific quotations from the earlier rtext then I have no problem
with top posting. Provided all but a small context quote is left below.
But please don't send me 3 pages of text to just add two sentences
at the top!!!


I simply propose that the only requirement to communications here is
that replies and questions be well formulated, courteous, and reasonably
intelligent.


Ah but there's the rub, what is "well formatted"? :-)


If there are going to be arguments pertaining to the posts, let them be
over content and not placement.


When you are trying to respond to hundreds of emails a day a
few top posted messages can cause severe headaches. Personally I
just ignore anything that has top posting that is hard to read, I don't 
have

time to unscramble it nor to waste time asking the poster to desist.
If they post scrambled mail it doesn't get answered...

So its not quite a case of the "color of the bikeshed" because this one
does make a very real difference to the usability of the medium and
to the success of the message in reaching its full audience.

In most cases it doesn't hurt much but in longer threads it does.
So, please folks, be sensitive to your readers.

Alan G.


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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Richard D. Moores
On Tue, Nov 3, 2009 at 17:21, Richard D. Moores  wrote:
> Another is, although I've seen this before in older Python docs, I
> never really understood things like
>
> for_stmt ::= "for" target_list "in" expression_list ":" suite
>             ["else" ":" suite]
>
> help> ::=
> no Python documentation found for '::='
>
> In particular, what exactly does "::=" mean?

Found it. See 

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


Re: [Tutor] Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

2009-11-03 Thread bob gailer

Robert Berman wrote:


In [69]: l1=[(0,0)] * 4

In [70]: l1
Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]

In [71]: l1[2][0]
Out[71]: 0

In [72]: l1[2][0] = 3
---
TypeError Traceback (most recent call 
last)


/home/bermanrl/ in ()

TypeError: 'tuple' object does not support item assignment

First question, is the error referring to the assignment (3) or the 
index [2][0]. I think it is the index but if that is the case why does 
l1[2][0] produce the value assigned to that location and not the same 
error message.


Second question, I do know that l1[2] = 3,1 will work. Does this mean 
I must know the value of both items in l1[2] before I change either 
value. I guess the correct question is how do I change or set the 
value of l1[0][1] when I specifically mean the second item of an 
element of a 2D array?


I have read numerous explanations of this problem thanks to Google; 
but no real explanation of setting of one element of the pair without 
setting the second element of the pair as well.


For whatever glimmers of clarity anyone can offer. I thank you.


Tuples are immutable. Replace them with lists and voila. l1=[[0,0]] * 4

But also realize that you are creating a list with 4 copies of one 
object [0,0]. Assigning to one changes all!



--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can't find modules at command line

2009-11-03 Thread Alan Gauld

"Christian Witts"  wrote

>>> import gun
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named gun
>>> 

This file is at [/Users/sam/Documents/gun.py].  What should I do to 
make it visible to Python?
  
In the console when you start it up you appear to be in your home 
directory `~` but your script is in the Documents folder so you will 
need to add Documents to your path for eg.

from sys import path
path.append('/path/to/script/to/be/imported')
import gun


Or you can create/edit your PYTHONPATH environment variable
to include /Users/sam/Documents.

You do that in your ~/.bash_profile file with

export PYTHONPATH=$PYTHONPATH:/Users/sam/Documents

Note the dot before the filename...

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Richard D. Moores
My sincere thanks to Dave Angel and Alan Gauld for their clear
explanations of .bat files and how to use them. I've already made
several useful ones.

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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Alan Gauld


"Richard D. Moores"  wrote 


My sincere thanks to Dave Angel and Alan Gauld for their clear
explanations of .bat files and how to use them. I've already made
several useful ones.


Start the cmd shell in windows and run the help command to 
get a list of things you can put in to spruce them up. It will explain 
how to use IF statements and FOR loops to make them much 
more powerful. But in practice if you know Python you will 
usually find it easier with that! :-)


Alan G

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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Alan Gauld


"Richard D. Moores"  wrote


Or the way I do it, have python31.bat and python26.bat, which launch the
exact pythons that I want.


Thanks, Dave. I'm not familiar with .bat files. Could you give me a
look at one of them?


A bat file is just a file of "DOS" commands and ends in .bat

so python31.bat

will contain the single line

C:\path\ro\python\31\python.exe %*



The %* is a placeholder for any commandline arguments you give
when calling the batch file -  like sys.argv in python.

You then call it with

python31 myscript.py


There is a brief discussion and example of a bat file in the intro
to my tutor, in the What is Programming topic.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Dave Angel

Tim Golden wrote:

Richard D. Moores wrote:

On Tue, Nov 3, 2009 at 05:30, Dave Angel  wrote:


Or the way I do it, have python31.bat and python26.bat, which launch 
the

exact pythons that I want.


Thanks, Dave. I'm not familiar with .bat files. Could you give me a
look at one of them?


As an alternative, I've created python26.exe, python31.exe etc. in my 
c:\tools directory (which is on my PATH wherever I am) and which are 
hardlinks to their corresponding c:\python26\python.exe etc.

Same as the batch file, really, only without a batch file!

TJG
I've thought of using hard links (using fsutil.exe, in case anyone else 
is interested), but I keep my scripts and batch files, as well as small 
utilities on a separate drive  partition from the one that has my OS and 
installed programs.  And hard links don't work across separate partitions.


But tell me, how does python.exe find its "home" directory, to set 
initial sys.path and suchlike?  I assumed it was done relative to the 
python.exe's full path location.  But if you hard link, nobody knows 
where the real executable image resides (and in fact the concept has no 
meaning, since the two directory entries equally own the content).


I once downloaded a program (which worked on many python scripts, but 
not all) which was written as a standin for python.exe, loading the 
pythonNN.dll and executing the script.  The intentional difference was 
that instead of getting the script name from the commandline, it used 
its own name as the name of the script, and searched in the same 
directory for that script.  So effectively if you made a copy of this 
program and called it  doit.exe, when it ran it would find doit.py and 
run that script.


One advantage it conferred is that the tasklist now identifies your 
program by name, instead of just having an anonymous python.exe running 
one (or six) instances.


I don't remember what it was called, but I'd love to play with it 
further.  And now I'd like to know how it could know where the lib files 
and suchlike are, as well.


DaveA

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


[Tutor] Stolen thread: Bottom folk vs. toppers was trouble using 2to3.py

2009-11-03 Thread Robert Berman
The war between bottom posters and top posters has been long, arduous,
and most often incredibly silly. There have been written group rules
mandating one over the other. Oft times these rules have doomed the
group. 

I have used both approaches most often favoring top posting only because
my mail program is set to reply from the top. True, it can be set to
reply from the bottom and I can pick and choose as I see fit, but I
usually let the program dictate.

The reality is, bottom posters are not blessed by god and top posters
are not bottom feeders. 

I simply propose that the only requirement to communications here is
that replies and questions be well formulated, courteous, and reasonably
intelligent. We are not 'Facebooks' and we hopefully do not condone cute
rudeness and sophmoric attempts at vast intellect. 

I must say that the answers to my questions have almost always been
incredibly well constructed  and concisely and clearly presented. I
almost never notice their placement within the post. 

If there are going to be arguments pertaining to the posts, let them be
over content and not placement.

Thanks to all who participate in this group.

Robert Berman


On Tue, 2009-11-03 at 12:10 -0600, Luke Paireepinart wrote:
> 
> 
> On Tue, Nov 3, 2009 at 7:30 AM, Dave Angel  wrote:
> (Erasing entire history, since you both top-posted, and it's
> too confusing)
> I was under the impression that you were supposed to top-post unless
> you were referring to a specific thing someone said, in which case you
> quoted it, much like you do on a forum.
> Is that not how it works?
> I'd personally rather people top-posted unless they're specifically
> referencing something, otherwise there's a huge tree of quotiness that
> I have to scroll past before I can see what they're trying to say.
> Maybe that's just me, though.  No one else seems to mind having to hit
> "reply to all" to send a message to the list either.
> Hopefully Google will succeed in their mission to replace e-mail with
> Wave and all messages will be in context and there will be no need for
> quoting one way or the other.
> -Luke
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Dave Angel

Richard D. Moores wrote:

On Tue, Nov 3, 2009 at 05:30, Dave Angel  wrote:
  



Easiest way to know which python is being used is to run it same way:

e:\python31\temp> python 2to3.py -w mycalc.py

Or the way I do it, have python31.bat and python26.bat, which launch the
exact pythons that I want.



Thanks, Dave. I'm not familiar with .bat files. Could you give me a
look at one of them?

Dick

  
A batch file is Windows (much dummyfied) version of Unix script files.  
It's a "program" interpreted by CMD.EXE, the shell processor for a 
"Command Prompt".   Anyway, it's usually not much more than a list of 
programs to run, with their arguments.  In our case, we just want to run 
a particular python.exe in a particular path, with whatever arguments 
were passed to the batch file.  So mine are one line each, and look like:


python26.bat
---
c:\progfiles\python26\python.exe %*


python31.bat
---
c:\progfiles\python31\python.exe %*


Obviously, yours will be different, unless you happened to pick the same 
install path as I did.  The %* magic just means copy the rest of the 
original command line.


You run these something like:

m:\myscripts\ready >  python31  myprogram.py

to explicitly run python31 on the python script, myprogram.py

DaveA

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


Re: [Tutor] java for python programmers

2009-11-03 Thread Alan Gauld


"Amit Sethi"  wrote

Hi their is a book , python for java programmers is their a book on java 
for

python programmers for some one who wants to move to java from python


Not that I know of but you could try the Jython book for starters to play 
with

the Java class library which is half the battle of  using Java.

Then try Bruce Eckel's Thinking in Java or the O'Reilly Learning Java,
both are good basic intros if you already know a language - like Python
say - IMHO. And they are not too basic - ala Java for Dummies...

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


Re: [Tutor] Retrieving information from a plain text file(WinXP/py2.6.2/Beginner)

2009-11-03 Thread Alan Gauld


"Serdar Tumgoren"  wrote 


Also, on a side note, you can greatly improve the readability of your
code by using the triple-quote style for multi-line docstrings inside
functions (rather than the hash comment marks). I tend to use hash
marks for one-line/inline comments, since they can really become an
eyesore (at least IMHO) when used too liberally.


I'd second that suggestion with the added benefit that if you use 
docstrings they will be detected by Pythons help() function.



--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Kent Johnson
On Tue, Nov 3, 2009 at 1:10 PM, Luke Paireepinart
 wrote:
> I was under the impression that you were supposed to top-post unless you
> were referring to a specific thing someone said, in which case you quoted
> it, much like you do on a forum.
> Is that not how it works?

Presumably you are replying to *something* in the previous post, yes?

> I'd personally rather people top-posted unless they're specifically
> referencing something, otherwise there's a huge tree of quotiness that I
> have to scroll past before I can see what they're trying to say.

You (or whoever) should delete the part of the "huge tree of
quotiness" that is not relevant to the reply.

> Hopefully Google will succeed in their mission to replace e-mail with Wave
> and all messages will be in context and there will be no need for quoting
> one way or the other.

Yes, that would help.

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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Richard D. Moores
On Tue, Nov 3, 2009 at 16:46, Alan Gauld  wrote:
>
> "Richard D. Moores"  wrote
>>
>> My sincere thanks to Dave Angel and Alan Gauld for their clear
>> explanations of .bat files and how to use them. I've already made
>> several useful ones.
>
> Start the cmd shell in windows and run the help command to get a list of
> things you can put in to spruce them up.

Sorry, put in where?

> It will explain how to use IF
> statements and FOR loops to make them much more powerful. But in practice if
> you know Python you will usually find it easier with that! :-)

Wow, the "online help utility" for 3.1 looks really useful and
informative. One question. Many of the articles are LONG. When I've
got what wanted out of one, I want to quit, but don't know how to quit
More. How to?

Another is, although I've seen this before in older Python docs, I
never really understood things like

for_stmt ::= "for" target_list "in" expression_list ":" suite
 ["else" ":" suite]

help> ::=
no Python documentation found for '::='

In particular, what exactly does "::=" mean?

Thanks,

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


Re: [Tutor] Stolen thread: Bottom folk vs. toppers was trouble using 2to3.py

2009-11-03 Thread Modulok
[snip]
> The war between bottom posters and top posters has been long, arduous,
> and most often incredibly silly. There have been written group rules
> mandating one over the other. Oft times these rules have doomed the
> group.
>

Two words come to mind: Bikeshed color.

> I simply propose that the only requirement to communications here is
> that replies and questions be well formulated, courteous, and reasonably
> intelligent.

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


Re: [Tutor] PyWin32 - Library of functions to interact with windows?

2009-11-03 Thread Scott Nelson
Scott Nelson  wrote:
>
>  It *is* possible to color console text with Python and pywin.  But, it is
> tricky and not obvious.  I've been wondering how to do this myself and I
> recently found some C code on the web [2] that does this and I translated
> that into to Python and pywin.  It can be done in about 4 lines of Python.
>

For the sake of posterity (and the archives), I figured I'd list the "4
lines of Python" I mentioned above:


import win32console
handle = win32console.GetStdHandle(win32console.STD_OUTPUT_HANDLE)
handle.SetConsoleTextAttribute(win32console.FOREGROUND_BLUE)
print 'blue text'


Beginning with this snippet, there are a number of other things you can do,
such as set the cursor position (handle.SetConsoleCursorPosition) and get
the properties of the console itself (handle.GetConsoleScreenBufferInfo).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Luke Paireepinart
On Tue, Nov 3, 2009 at 7:30 AM, Dave Angel  wrote:

> (Erasing entire history, since you both top-posted, and it's too confusing)
>
I was under the impression that you were supposed to top-post unless you
were referring to a specific thing someone said, in which case you quoted
it, much like you do on a forum.
Is that not how it works?
I'd personally rather people top-posted unless they're specifically
referencing something, otherwise there's a huge tree of quotiness that I
have to scroll past before I can see what they're trying to say.
Maybe that's just me, though.  No one else seems to mind having to hit
"reply to all" to send a message to the list either.
Hopefully Google will succeed in their mission to replace e-mail with Wave
and all messages will be in context and there will be no need for quoting
one way or the other.
-Luke
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

2009-11-03 Thread Robert Berman
Thank you for your explanations and especially your clear examples of a
phenomenon(when list elements are tuples) which takes a few moments of
study to truly grasp.

Robert

On Tue, 2009-11-03 at 09:53 -0600, Wayne Werner wrote:
> On Tue, Nov 3, 2009 at 9:20 AM, Robert Berman 
> wrote:
> 
> 
> 
> In [69]: l1=[(0,0)] * 4
> 
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
> 
> In [71]: l1[2][0]
> Out[71]: 0
> 
> 
> 
> This calls the element at index 2 which is:
> (0,0) - a tuple, then calls element [0] from that tuple, which is 0
> 
> 
> when you try to assign an item into a tuple, you get the same problem:
> 
> 
> In [1]: x = (1,2,3)
> 
> 
> In [2]: type(x)
> Out[2]: 
> 
> 
> In [3]: x[0]
> Out[3]: 1
> 
> 
> In [4]: x[0] = 0
> ---
> TypeError Traceback (most recent call
> last)
> 
> 
> /home/wayne/Desktop/ in ()
> 
> 
> TypeError: 'tuple' object does not support item assignment
> 
> 
> And from your example:
> In [6]: l1 = [(0,0)] *4
> 
> 
> In [7]: type(l1[2])
> Out[7]: 
> 
> 
> 
> In [72]: l1[2][0] = 3
> 
> ---
> TypeError Traceback (most
> recent call last)
> 
> /home/bermanrl/ in ()
> 
> TypeError: 'tuple' object does not support item assignment
> 
> First question, is the error referring to the assignment (3)
> or the index [2][0]. I think it is the index but if that is
> the case why does l1[2][0] produce the value assigned to that
> location and not the same error message.
> 
> Second question, I do know that l1[2] = 3,1 will work. Does
> this mean I must know the value of both items in l1[2] before
> I change either value. I guess the correct question is how do
> I change or set the value of l1[0][1] when I specifically mean
> the second item of an element of a 2D array?
> 
> 
> 
> When you use l1[2] = 3,1 it converts the right hand side to a tuple by
> implication - putting a comma between values:
> 
> 
> In [8]: x = 3,1
> 
> 
>  In [9]: type(x)
> Out[9]: 
> 
> 
> so when you say l1[2] = 3,1 you are saying "assign the tuple (3,1) to
> the list element at l1[2]" which is perfectly fine, because lists are
> mutable and tuples are not. 
> 
> 
>  
> 
> I have read numerous explanations of this problem thanks to
> Google; but no real explanation of setting of one element of
> the pair without setting the second element of the pair as
> well.
> 
> For whatever glimmers of clarity anyone can offer. I thank
> you.
> 
> 
> Hopefully this helps,
> Wayne
> 
> 
> p.s. If you want to be able to change individual elements, you can try
> this:
> In [21]: l1 = [[0,0] for x in xrange(4)]
> 
> 
> In [22]: l1
> Out[22]: [[0, 0], [0, 0], [0, 0], [0, 0]]
> 
> 
> In [23]: l1[2][0] = 3
> 
> 
> In [24]: l1
> Out[24]: [[0, 0], [0, 0], [3, 0], [0, 0]]
> 
> 
> 
> 
> I don't know if there's a better way to express line 21, but you can't
> do it the other way or you'll just have the same list in your list 4
> times:
> 
> 
> In [10]: l1 = [[0,0]]*4
> 
> 
> In [11]: l1
> Out[11]: [[0, 0], [0, 0], [0, 0], [0, 0]]
> 
> 
> In [12]: l1[2][0] = 3
> 
> 
> In [13]: l1
> Out[13]: [[3, 0], [3, 0], [3, 0], [3, 0]]
> 
> 
> 
> 
> 
> 
> 
> 
>  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

2009-11-03 Thread Mark Tolonen


"Robert Berman"  wrote in message 
news:1257261606.29483.23.ca...@bermanrl-desktop...


In [69]: l1=[(0,0)] * 4

In [70]: l1
Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]

In [71]: l1[2][0]
Out[71]: 0

In [72]: l1[2][0] = 3
---
TypeError Traceback (most recent call
last)

/home/bermanrl/ in ()

TypeError: 'tuple' object does not support item assignment

First question, is the error referring to the assignment (3) or the
index [2][0]. I think it is the index but if that is the case why does
l1[2][0] produce the value assigned to that location and not the same
error message.

Second question, I do know that l1[2] = 3,1 will work. Does this mean I
must know the value of both items in l1[2] before I change either value.
I guess the correct question is how do I change or set the value of
l1[0][1] when I specifically mean the second item of an element of a 2D
array?

I have read numerous explanations of this problem thanks to Google; but
no real explanation of setting of one element of the pair without
setting the second element of the pair as well.


Tuples are read-only, so you can't change just one element of a tuple:

   >>> x=0,0
   >>> x
   (0, 0)
   >>> x[0]=1
   Traceback (most recent call last):
 File "", line 1, in 
   TypeError: 'tuple' object does not support item assignment

You can, however, replace the whole thing, as you found:

   >>> x=1,1
   >>> x
   (1, 1)

To do what you want, you a list of lists, not a list of tuples, but there is 
a gotcha.  This syntax:


   >>> L=[[0,0]]*4
   >>> L
   [[0, 0], [0, 0], [0, 0], [0, 0]]

Produces a list of the *same* list object, so modifying one modifies all:

   >>> L[2][0]=1
   >>> L
   [[1, 0], [1, 0], [1, 0], [1, 0]]

Use a list comprehension to create lists of lists, where each list is a 
*new* list:


   >>> L = [[0,0] for i in range(4)]
   >>> L
   [[0, 0], [0, 0], [0, 0], [0, 0]]
   >>> L[2][0] = 1
   >>> L
   [[0, 0], [0, 0], [1, 0], [0, 0]]

-Mark


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


Re: [Tutor] Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

2009-11-03 Thread Andre Engels
On Tue, Nov 3, 2009 at 4:20 PM, Robert Berman  wrote:

>
>   In [69]: l1=[(0,0)] * 4
>
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
>
> In [71]: l1[2][0]
> Out[71]: 0
>
> In [72]: l1[2][0] = 3
> ---
> TypeError Traceback (most recent call last)
>
> /home/bermanrl/ in ()
>
> TypeError: 'tuple' object does not support item assignment
>
> First question, is the error referring to the assignment (3) or the index
> [2][0]. I think it is the index but if that is the case why does l1[2][0]
> produce the value assigned to that location and not the same error message.
>
> Second question, I do know that l1[2] = 3,1 will work. Does this mean I
> must know the value of both items in l1[2] before I change either value. I
> guess the correct question is how do I change or set the value of l1[0][1]
> when I specifically mean the second item of an element of a 2D array?
>
> I have read numerous explanations of this problem thanks to Google; but no
> real explanation of setting of one element of the pair without setting the
> second element of the pair as well.
>
> For whatever glimmers of clarity anyone can offer. I thank you.
>

Tuples are immutable types. Thus it is not possible to change one of the
values of a tuple (or even of changing both of them). The only thing you can
do, is create a new tuple, and put that in the same place in the list. In
your example, when you do l1[2][0] = 3, you try to change the tuple l1[2],
which is impossible.

To do what you want to do, you have to create a new array with the same
second but different first value, and put that array in l1[2], that is:

l1[2] = (3, l1[2,1])




-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

2009-11-03 Thread Emile van Sebille

On 11/3/2009 7:20 AM Robert Berman said...


In [69]: l1=[(0,0)] * 4

In [70]: l1
Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]

In [71]: l1[2][0]
Out[71]: 0

In [72]: l1[2][0] = 3
---
TypeError Traceback (most recent call last)

/home/bermanrl/ in ()

TypeError: 'tuple' object does not support item assignment


Start out slower...

a = (1,2)
a[1]=3

You can't change a tuple.  You can create a new tuple, or you can change 
the content of an item help in a tuple, but you can't change the content 
of the tuple itself.  Try the following then see if it helps answer your 
questions...


b = [1]

a = (1,b)

b[0]=2

print a

a[1][0]=3

print a

Emile



First question, is the error referring to the assignment (3) or the 
index [2][0]. I think it is the index but if that is the case why does 
l1[2][0] produce the value assigned to that location and not the same 
error message.


Second question, I do know that l1[2] = 3,1 will work. Does this mean I 
must know the value of both items in l1[2] before I change either value. 
I guess the correct question is how do I change or set the value of 
l1[0][1] when I specifically mean the second item of an element of a 2D 
array?


I have read numerous explanations of this problem thanks to Google; but 
no real explanation of setting of one element of the pair without 
setting the second element of the pair as well.


For whatever glimmers of clarity anyone can offer. I thank you.

Robert




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


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


Re: [Tutor] Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

2009-11-03 Thread Wayne Werner
On Tue, Nov 3, 2009 at 9:20 AM, Robert Berman  wrote:

>
>   In [69]: l1=[(0,0)] * 4
>
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
>
> In [71]: l1[2][0]
> Out[71]: 0
>

This calls the element at index 2 which is:
(0,0) - a tuple, then calls element [0] from that tuple, which is 0

when you try to assign an item into a tuple, you get the same problem:

In [1]: x = (1,2,3)

In [2]: type(x)
Out[2]: 

In [3]: x[0]
Out[3]: 1

In [4]: x[0] = 0
---
TypeError Traceback (most recent call last)

/home/wayne/Desktop/ in ()

TypeError: 'tuple' object does not support item assignment

And from your example:
In [6]: l1 = [(0,0)] *4

In [7]: type(l1[2])
Out[7]: 


> In [72]: l1[2][0] = 3
> ---
> TypeError Traceback (most recent call last)
>
> /home/bermanrl/ in ()
>
> TypeError: 'tuple' object does not support item assignment
>
> First question, is the error referring to the assignment (3) or the index
> [2][0]. I think it is the index but if that is the case why does l1[2][0]
> produce the value assigned to that location and not the same error message.
>
> Second question, I do know that l1[2] = 3,1 will work. Does this mean I
> must know the value of both items in l1[2] before I change either value. I
> guess the correct question is how do I change or set the value of l1[0][1]
> when I specifically mean the second item of an element of a 2D array?
>

When you use l1[2] = 3,1 it converts the right hand side to a tuple by
implication - putting a comma between values:

In [8]: x = 3,1

 In [9]: type(x)
Out[9]: 

so when you say l1[2] = 3,1 you are saying "assign the tuple (3,1) to the
list element at l1[2]" which is perfectly fine, because lists are mutable
and tuples are not.



>
> I have read numerous explanations of this problem thanks to Google; but no
> real explanation of setting of one element of the pair without setting the
> second element of the pair as well.
>
> For whatever glimmers of clarity anyone can offer. I thank you.
>

Hopefully this helps,
Wayne

p.s. If you want to be able to change individual elements, you can try this:
In [21]: l1 = [[0,0] for x in xrange(4)]

In [22]: l1
Out[22]: [[0, 0], [0, 0], [0, 0], [0, 0]]

In [23]: l1[2][0] = 3

In [24]: l1
Out[24]: [[0, 0], [0, 0], [3, 0], [0, 0]]


I don't know if there's a better way to express line 21, but you can't do it
the other way or you'll just have the same list in your list 4 times:

In [10]: l1 = [[0,0]]*4

In [11]: l1
Out[11]: [[0, 0], [0, 0], [0, 0], [0, 0]]

In [12]: l1[2][0] = 3

In [13]: l1
Out[13]: [[3, 0], [3, 0], [3, 0], [3, 0]]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Tim Golden

Richard D. Moores wrote:

On Tue, Nov 3, 2009 at 05:30, Dave Angel  wrote:

(Erasing entire history, since you both top-posted, and it's too confusing)

When you run a xxx.py on Windows, it locates the python.exe using the
registry entries set up by  assoc and ftype.  But when you run "python" it
looks for python.exe (or python.bat, or ...) on the PATH.  So there's not
necessarily any conection between your tests.  You're probably running two
different versions.

Easiest way to know which python is being used is to run it same way:

e:\python31\temp> python 2to3.py -w mycalc.py

Or the way I do it, have python31.bat and python26.bat, which launch the
exact pythons that I want.


Thanks, Dave. I'm not familiar with .bat files. Could you give me a
look at one of them?


As an alternative, I've created python26.exe, python31.exe etc. in 
my c:\tools directory (which is on my PATH wherever I am) and which 
are hardlinks to their corresponding c:\python26\python.exe etc.

Same as the batch file, really, only without a batch file!

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


[Tutor] Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

2009-11-03 Thread Robert Berman

In [69]: l1=[(0,0)] * 4

In [70]: l1
Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]

In [71]: l1[2][0]
Out[71]: 0

In [72]: l1[2][0] = 3
---
TypeError Traceback (most recent call
last)

/home/bermanrl/ in ()

TypeError: 'tuple' object does not support item assignment

First question, is the error referring to the assignment (3) or the
index [2][0]. I think it is the index but if that is the case why does
l1[2][0] produce the value assigned to that location and not the same
error message.

Second question, I do know that l1[2] = 3,1 will work. Does this mean I
must know the value of both items in l1[2] before I change either value.
I guess the correct question is how do I change or set the value of
l1[0][1] when I specifically mean the second item of an element of a 2D
array?

I have read numerous explanations of this problem thanks to Google; but
no real explanation of setting of one element of the pair without
setting the second element of the pair as well.

For whatever glimmers of clarity anyone can offer. I thank you.

Robert

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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Richard D. Moores
On Tue, Nov 3, 2009 at 05:30, Dave Angel  wrote:
> (Erasing entire history, since you both top-posted, and it's too confusing)
>
> When you run a xxx.py on Windows, it locates the python.exe using the
> registry entries set up by  assoc and ftype.  But when you run "python" it
> looks for python.exe (or python.bat, or ...) on the PATH.  So there's not
> necessarily any conection between your tests.  You're probably running two
> different versions.
>
> Easiest way to know which python is being used is to run it same way:
>
> e:\python31\temp> python 2to3.py -w mycalc.py
>
> Or the way I do it, have python31.bat and python26.bat, which launch the
> exact pythons that I want.

Thanks, Dave. I'm not familiar with .bat files. Could you give me a
look at one of them?

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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Richard D. Moores
After much fiddling around, I got 2to3 to work! And it did a fine, if
not perfect job on mycalc.py (which has about 120 functions and 1993
lines).

What I did that worked, was to use the E:\Python26\Tools\Scripts, and
not Python31. E:\Python26\Tools\Scripts has 2to3.py. I added the
folder lib2to3\ and mycalc.py to E:\Python26\Tools\Scripts, and did a

E:\Python26\Tools\Scripts>2to3 -w mycalc.py

The -w switch first backed up mycalc.py as mycalc.py.bak, and then
2to3.py went to work. The only problems it couldn't handle are shown
below. They are all of the same sort, involving a unicode error. I
simply remmed out the problem causing functions, and now it seems
mycalc.py is good to go with Python 3.1.  I'll look into the "unicode"
errors later -- much later.

>>> from mycalc import *
Traceback (most recent call last):
  File "", line 1, in 
from mycalc import *
  File "E:\Python31\lib\site-packages\mycalc.py", line 172
"""
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 33-34: malformed \N character escape
>>> from mycalc import *
Traceback (most recent call last):
  File "", line 1, in 
from mycalc import *
  File "E:\Python31\lib\site-packages\mycalc.py", line 186
"""
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 198-199: malformed \N character escape
>>> from mycalc import *
Traceback (most recent call last):
  File "", line 1, in 
from mycalc import *
  File "E:\Python31\lib\site-packages\mycalc.py", line 207
file = open('E:\PythonWork\Nutshell\\words.txt', 'r')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 13-14: malformed \N character escape
>>> from mycalc import *
>>>

I also ran 2to3 again and this time captured the output as what
appears to be a diff file:

==
E:\Python26\Tools\Scripts>2to3 -w mycalc.py > 2to3_at_work.txt
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Files that were modified:
RefactoringTool: mycalc.py

E:\Python26\Tools\Scripts>
==

You can see the file produced at 

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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Dave Angel

(Erasing entire history, since you both top-posted, and it's too confusing)

When you run a xxx.py on Windows, it locates the python.exe using the 
registry entries set up by  assoc and ftype.  But when you run "python" 
it looks for python.exe (or python.bat, or ...) on the PATH.  So there's 
not necessarily any conection between your tests.  You're probably 
running two different versions.


Easiest way to know which python is being used is to run it same way:

e:\python31\temp> python 2to3.py -w mycalc.py

Or the way I do it, have python31.bat and python26.bat, which launch the 
exact pythons that I want.


DaveA

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


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Richard D. Moores
And here's the same try (without the -w switch, to keep it simple),
but this time on my laptop (64-bit Vista SP1). The result is a bit
different:

=
C:\Python31\temp>2to3.py mycalc.py
Traceback (most recent call last):
  File "C:\Python31\temp\2to3.py", line 2, in 
from lib2to3.main import main
  File "C:\Python31\temp\lib2to3\main.py", line 121
print("At least one file or directory argument required.", file=sys.stderr)
   ^
SyntaxError: invalid syntax

C:\Python31\temp>python
Python 3.1.1 (r311:74483, Aug 17 2009, 16:45:59) [MSC v.1500 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/4
0.25
>>>
==

Dick

On Tue, Nov 3, 2009 at 02:15, Richard D. Moores  wrote:
> Well, here's the same try again, followed by starting the shell:
>
> =
> E:\Python31\temp>2to3.py -w mycalc.py
> Traceback (most recent call last):
>  File "E:\Python31\temp\2to3.py", line 2, in 
>    from lib2to3.main import main
>  File "E:\Python31\temp\lib2to3\main.py", line 33
>    except os.error as err:
>                     ^
> SyntaxError: invalid syntax
>
> E:\Python31\temp>python
> Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.

> ==
>
> Otherwise, how can I tell?
>
> BTW I'm doing this on my old desktop, which runs XP SP2 32-bit.
>
> And thanks, Luke, I took some time off to get back to my piano playing
> with a new teacher, my 6th IIRC. Now its time to do both.
>
> Dick
>
> On Tue, Nov 3, 2009 at 01:50, Luke Paireepinart  
> wrote:
>> Are you sure you're using the 3.1 version of Python to run the script?
>> Welcome back, btw.  haven't heard from you in a while.
>>
>> On Tue, Nov 3, 2009 at 2:09 AM, Richard D. Moores 
>> wrote:
>>>
>>> I'm in the process to learning Python 3.1, and need to convert a bunch
>>> of handy functions I wrote and stole over several years. They are all
>>> written in 2.x, and are together in one file, mycalc.py.
>>>
>>> I created a folder E:\Python31\temp\ (my OS is Vista SP1 64-bit), and
>>> in it I placed copies of mycalc.py, 2to3.py, and a folder, lib2to3\  .
>>>
>>> Then I tried
>>> E:\Python31\temp>2to3.py -w mycalc.py
>>>
>>> and got
>>>
>>> Traceback (most recent call last):
>>>  File "E:\Python31\temp\2to3.py", line 2, in 
>>>    from lib2to3.main import main
>>>  File "E:\Python31\temp\lib2to3\main.py", line 33
>>>    except os.error as err:
>>>                     ^
>>> SyntaxError: invalid syntax
>>>
>>> How have others dealt with this?
>>>
>>> Dick Moores
>>> ___
>>> Tutor maillist  -  tu...@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Richard D. Moores
Well, here's the same try again, followed by starting the shell:

=
E:\Python31\temp>2to3.py -w mycalc.py
Traceback (most recent call last):
  File "E:\Python31\temp\2to3.py", line 2, in 
from lib2to3.main import main
  File "E:\Python31\temp\lib2to3\main.py", line 33
except os.error as err:
 ^
SyntaxError: invalid syntax

E:\Python31\temp>python
Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
==

Otherwise, how can I tell?

BTW I'm doing this on my old desktop, which runs XP SP2 32-bit.

And thanks, Luke, I took some time off to get back to my piano playing
with a new teacher, my 6th IIRC. Now its time to do both.

Dick

On Tue, Nov 3, 2009 at 01:50, Luke Paireepinart  wrote:
> Are you sure you're using the 3.1 version of Python to run the script?
> Welcome back, btw.  haven't heard from you in a while.
>
> On Tue, Nov 3, 2009 at 2:09 AM, Richard D. Moores 
> wrote:
>>
>> I'm in the process to learning Python 3.1, and need to convert a bunch
>> of handy functions I wrote and stole over several years. They are all
>> written in 2.x, and are together in one file, mycalc.py.
>>
>> I created a folder E:\Python31\temp\ (my OS is Vista SP1 64-bit), and
>> in it I placed copies of mycalc.py, 2to3.py, and a folder, lib2to3\  .
>>
>> Then I tried
>> E:\Python31\temp>2to3.py -w mycalc.py
>>
>> and got
>>
>> Traceback (most recent call last):
>>  File "E:\Python31\temp\2to3.py", line 2, in 
>>    from lib2to3.main import main
>>  File "E:\Python31\temp\lib2to3\main.py", line 33
>>    except os.error as err:
>>                     ^
>> SyntaxError: invalid syntax
>>
>> How have others dealt with this?
>>
>> Dick Moores
>> ___
>> Tutor maillist  -  tu...@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] java for python programmers

2009-11-03 Thread Amit Sethi
Hi their is a book , python for java programmers is their a book on java for
python programmers for some one who wants to move to java from python

-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trouble using 2to3.py

2009-11-03 Thread Luke Paireepinart
Are you sure you're using the 3.1 version of Python to run the script?
Welcome back, btw.  haven't heard from you in a while.

On Tue, Nov 3, 2009 at 2:09 AM, Richard D. Moores wrote:

> I'm in the process to learning Python 3.1, and need to convert a bunch
> of handy functions I wrote and stole over several years. They are all
> written in 2.x, and are together in one file, mycalc.py.
>
> I created a folder E:\Python31\temp\ (my OS is Vista SP1 64-bit), and
> in it I placed copies of mycalc.py, 2to3.py, and a folder, lib2to3\  .
>
> Then I tried
> E:\Python31\temp>2to3.py -w mycalc.py
>
> and got
>
> Traceback (most recent call last):
>  File "E:\Python31\temp\2to3.py", line 2, in 
>from lib2to3.main import main
>  File "E:\Python31\temp\lib2to3\main.py", line 33
>except os.error as err:
> ^
> SyntaxError: invalid syntax
>
> How have others dealt with this?
>
> Dick Moores
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] trouble using 2to3.py

2009-11-03 Thread Richard D. Moores
I'm in the process to learning Python 3.1, and need to convert a bunch
of handy functions I wrote and stole over several years. They are all
written in 2.x, and are together in one file, mycalc.py.

I created a folder E:\Python31\temp\ (my OS is Vista SP1 64-bit), and
in it I placed copies of mycalc.py, 2to3.py, and a folder, lib2to3\  .

Then I tried
E:\Python31\temp>2to3.py -w mycalc.py

and got

Traceback (most recent call last):
  File "E:\Python31\temp\2to3.py", line 2, in 
from lib2to3.main import main
  File "E:\Python31\temp\lib2to3\main.py", line 33
except os.error as err:
 ^
SyntaxError: invalid syntax

How have others dealt with this?

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