references/addrresses in imperative languages
hancock at anansispaceworks.com
Sun Jun 19 23:25:13 EDT 2005
On Sunday 19 June 2005 05:34 pm, Xah Lee wrote:
> in coding Python yesterday, i was quite stung by the fact that lists
> appened to another list goes by as some so-called "reference". e.g.
> print t
Day one in learning Python, yes --- "names bind to objects" NOT
"variables are filled with values". This is one case where prior
experience with C warps your brain.
> in the following code, after some 1 hour, finally i found the solution
> of h[:]. (and that's cheating thru a google search)
> def parti(l,j):
> '''parti(l,j) returns l partitioned with j elements per group. If j
> is not a factor of length of l, then the reminder elements are dropped.
> Example: parti([1,2,3,4,5,6],2) returns [[1,2],[3,4],[5,6]]
> Example: parti([1,2,3,4,5,6,7],3) returns [[1,2,3],[4,5,6]]'''
> r= # result list
> h=range(j) # temp holder for sublist
> for n1 in range(n):
> for j1 in range(j):
> r.append( h[:] )
> return r
Too bulky? How about:
def parti(L, j):
return [L[k*j:(k+1)*j] for k in range(len(L)/j)]
[[1, 2, 3], [4, 5, 6]]
[[1, 2], [3, 4], [5, 6]]
> PS is there any difference between
is quite different from
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com
More information about the Python-list