Re: Passing a list into a list .append() method

2014-09-09 Thread JBB
Frank Millman frank at chagford.com writes:

 
 
 JBB jeanbigboute at gmail.com wrote in message 
 news:loom.20140909T073428-713 at post.gmane.org...
 I have a list with a fixed number of elements which I need to grow; ie. add
  rows of a fixed number of elements, some of which will be blank.
...
 I am sure that someone will give you a comprehensive answer, but here is a 
 quick clue which may be all you need.
...

[ Deletia per gmane's requirements ]

 Wrapping a list with 'list()' has the effect of making a copy of it.
 
 This is from the docs (3.4.1) -
 
 
 Lists may be constructed in several ways:
 
 - Using a pair of square brackets to denote the empty list: []
 - Using square brackets, separating items with commas: [a], [a, b, c]
 - Using a list comprehension: [x for x in iterable]
 - Using the type constructor: list() or list(iterable)
 
 The constructor builds a list whose items are the same and in the same order 
 as iterable's items.
 iterable may be either a sequence, a container that supports iteration, or 
 an iterator object.
 If iterable is already a list, a copy is made and returned, similar to 
 iterable[:]. [*]
 For example, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) 
 returns [1, 2, 3].
 If no argument is given, the constructor creates a new empty list, [].
 
 
 I marked the relevant line with [*]
 
 HTH
 
 Frank Millman


Ok, this does clear up why the list() construction worked in this context -
I wasn't aware that it would create a copy.  

I'm still a little confused by why passing the list as an argument causes
the list to change.  But, I was not aware of the id() method to see what's
equivalent to what.  I'll experiment with this and you've given me some good
ideas on other docs I need to read.

Thank you for the quick reply.


JBB


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


Re: Passing a list into a list .append() method

2014-09-09 Thread JBB
Paul Kroeger news at prz-wugen.com writes:

 
 Hello,
 
 I'm myself still learning Python, so others may please correct me, if
 I'm wrong.
...
 I hope, the above helps to understand why this behaviour.is to be
 expected.


To Peter Otten and Paul Kroeger:

Thank you both, very much.  I think I now get why the binding works as it
does in addition to why the list() approach worked.  

JBB

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


Re: Passing a list into a list .append() method

2014-09-09 Thread JBB
Peter Otten __peter__ at web.de writes:

[Deletia]

To Peter Otten and Paul Kroeger:

Thank you both, very much.  I think I now get why the binding works as it
does in addition to why the list() approach worked.

(Third attempt - priors not going through, please excuse any repetition)

JBB 


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


Passing a list into a list .append() method

2014-09-08 Thread JBB
I have a list with a fixed number of elements which I need to grow; ie. add
rows of a fixed number of elements, some of which will be blank.  

e.g. [['a','b','c','d'], ['A','B','C','D'], ['', 'aa', 'inky', ''], ['',
'bb', 'binky', ''], ... ]

This is a reduced representation of a larger list-of-lists problem that had
me running in circles today.  

I think I figured out _how_ to get what I want but I am looking to
understand why one approach works and another doesn't.

1) What does NOT work as desired:

proc_file = []
proc_file = [['a','b','c','d']]
proc_file.append(['A','B','C','D'])
blank_r = ['','','','']

qq = ['aa','bb','cc','dd']
rr = ['inky', 'binky', 'pinky', 'clyde']

for i,j in enumerate(zip(qq,rr)):
proc_file.append((blank_r))  # Add a row of blanks
proc_file[i+2][1] = j[0]
proc_file[i+2][2] = j[1]
print len(proc_file), blank_r, proc_file

print
print
proc_file

3 ['', 'aa', 'inky', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['',
'aa', 'inky', '']]
4 ['', 'bb', 'binky', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['',
'bb', 'binky', ''], ['', 'bb', 'binky', '']]
5 ['', 'cc', 'pinky', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['',
'cc', 'pinky', ''], ['', 'cc', 'pinky', ''], ['', 'cc', 'pinky', '']]
6 ['', 'dd', 'clyde', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['',
'dd', 'clyde', ''], ['', 'dd', 'clyde', ''], ['', 'dd', 'clyde', ''], ['',
'dd', 'clyde', '']]


Out[82]:

[['a', 'b', 'c', 'd'],
 ['A', 'B', 'C', 'D'],
 ['', 'dd', 'clyde', ''],
 ['', 'dd', 'clyde', ''],
 ['', 'dd', 'clyde', ''],
 ['', 'dd', 'clyde', '']]

2) What works as desired:

proc_file = []
proc_file = [['a','b','c','d']]
proc_file.append(['A','B','C','D'])
blank_r = ['','','','']

qq = ['aa','bb','cc','dd']
rr = ['inky', 'binky', 'pinky', 'clyde']

for i,j in enumerate(zip(qq,rr)):
proc_file.append(list(blank_r))  # Change it to list(blank_r) and it works
proc_file[i+2][1] = j[0]
proc_file[i+2][2] = j[1]
print len(proc_file), blank_r, proc_file

print
print
proc_file

3 ['', '', '', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['', 'aa',
'inky', '']]
4 ['', '', '', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['', 'aa',
'inky', ''], ['', 'bb', 'binky', '']]
5 ['', '', '', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['', 'aa',
'inky', ''], ['', 'bb', 'binky', ''], ['', 'cc', 'pinky', '']]
6 ['', '', '', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['', 'aa',
'inky', ''], ['', 'bb', 'binky', ''], ['', 'cc', 'pinky', ''], ['', 'dd',
'clyde', '']]


Out[83]:

[['a', 'b', 'c', 'd'],
 ['A', 'B', 'C', 'D'],
 ['', 'aa', 'inky', ''],
 ['', 'bb', 'binky', ''],
 ['', 'cc', 'pinky', ''],
 ['', 'dd', 'clyde', '']]

 Due diligence

I've read extensively on how arguments are passed to functions but I don't
think they are completely applicable here (though interesting nevertheless)

http://www.jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/


https://www.udacity.com/wiki/common-python-pitfalls

and others.

It looks like .append binds blank_r to proc_file in 1).  I change proc_file
and blank_r changes along with it.  Should I expect this? I understand lists
are mutable but I didn't expect that they could be associated/bound in this
manner.

I first tried protecting blank_r by passing it as a tuple.  That caused an
expected mismatch error.

Next, I tried passing it as list(tuple(blank_r)) which worked.  Then, I
finally settled on 2) where I dispensed with the tuple conversion.







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