Three dumb questions (ordered by dumbness descending)

Steven Feil sfeil at io.com
Mon Sep 23 21:45:29 EDT 2002


On Mon, 23 Sep 2002 16:03:21 -0500, Thorsten Kampe wrote:

> Okay, here they are:

Ok, I'll take a stab at the first two questions. I do think that a
little more clarity on your part could make it a bit easer.

The first thing to remember is that Python is a dynamically typed
language. It is typed and each object has a type, It would be a little
easer if you would disclose the type for each object you are referring
to.


> 1. Why is 'zip(zip(x)) != x' (should be the same because it's the
> transposed)

Humm, what is is zip(), a function in the standard Python library or
is it something you or someone else made up? I did a quick search in
the Python docs and the only zip I found referred to zip
compression. If this is something from the standard Python libraries
please state the library it comes from.

If zip is some general function, then in general, zip(zip(x)) is not
equal to x. This comes from fundamental abstract algebra. In trig
sin(sin(x)) is not x. There do exist functions who are there own
inverse and if zip is one of them then zip(zip(x)) is equal to x.

You mentioned transpose. If x is a matrix and the function zip only
transposes x, then zip(zip(x)) is equal to x. This is because a
transpose function is it's own inverse. I'm not sure where you got
this zip function, but zip is suppose to transpose x and zip(zip(x))
!= x then there is a bug in zip. Also if zip is a transposition
function it can only act on arrays and it should raise an exception if
x is not of the required type.

I will need to know more about where you got zip from.


> 2. Peter Norvig mentions in "Python for Lisp Programmers" some
> "don'ts": "[x] + y" and "x[1:]". Are there more things to avoid

humm, "[x] + y", again it comes down to type. [x] will always be a
list. It will contain a single element x of whatever type x is. y on
the other hand is whatever type y is (you haven't told me). In Python
when I use "[x] + y" y must be another list, because when used with a
list the + operated must have a second list to concatenate to the
first. It is the way + is defined. If you wish to appended the list
[x] with y you must have a reference to [x] such as z=[x] then append
to it with z.append(y). A quicker way would be to start with [x,y]
which would be equal to the z after you appended to it.

z=[x]
z.append(y)
z == [x,y]

The statement "x[1:]" this is legal if x is a list, or if x is some
type in which index slicing has been defined. basically x[1:] means
something that has every element that x has except for the first
element. So if x = "steve" then x[1"] is "teve".

========================================================================
 Steven Feil               | Gram-pa, back at the turn of the      .~.  
 Programmer/Developer      | century, why did people use an        /V\  
 sfeil at io.com              | operating system, when they were not // \\ 
                           | allowed to see the source code?      (X_X) 
========================================================================



More information about the Python-list mailing list