Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77712 > unrolled thread
| Started by | JBB <jeanbigboute@gmail.com> |
|---|---|
| First post | 2014-09-09 05:50 +0000 |
| Last post | 2014-09-09 05:07 -0700 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
Passing a list into a list .append() method JBB <jeanbigboute@gmail.com> - 2014-09-09 05:50 +0000
Re: Passing a list into a list .append() method Paul Kroeger <news@prz-wugen.com> - 2014-09-09 09:12 +0200
Re: Passing a list into a list .append() method JBB <jeanbigboute@gmail.com> - 2014-09-09 07:37 +0000
Re: Passing a list into a list .append() method Rustom Mody <rustompmody@gmail.com> - 2014-09-09 05:07 -0700
| From | JBB <jeanbigboute@gmail.com> |
|---|---|
| Date | 2014-09-09 05:50 +0000 |
| Subject | Passing a list into a list .append() method |
| Message-ID | <mailman.13886.1410242108.18130.python-list@python.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.
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.
[toc] | [next] | [standalone]
| From | Paul Kroeger <news@prz-wugen.com> |
|---|---|
| Date | 2014-09-09 09:12 +0200 |
| Message-ID | <1410246769.28830.23.camel@pcom01.wugen> |
| In reply to | #77712 |
Hello,
I'm myself still learning Python, so others may please correct me, if
I'm wrong.
Consider the following sentence of your link "jeffknupp.com/...":
"some_guy and first_names[0] both refer to the same object"
This is what is going on here.
Am Dienstag, den 09.09.2014, 05:50 +0000 schrieb JBB:
> [...]
>
> for i,j in enumerate(zip(qq,rr)):
> proc_file.append((blank_r)) # Add a row of blanks
At this point, the last "row" of "proc_file" and the variable "blank_r"
both refer to the list object [blank_r].
> proc_file[i+2][1] = j[0]
> proc_file[i+2][2] = j[1]
The object under "proc_file[...]" is changed now. This object is the
list object [blank_r]! So "proc_file[-1]" and "blank_r" both refer to
[blank_r] = ["", j[0], j[1], ""], which is added do the list object
[proc_file] at the beginning of the next iteration.
Thus, all entries of [proc_file] with index greater 1 are bound to
[blank_r], which is itself modified in each iteration to the
corresponding j:
proc_file[0] -> ["a", "b,", "c", "d"]
proc_file[1] -> ["A", "B,", "C", "D"]
proc_file[2] -> [blank_r]
proc_file[3] -> [blank_r]
proc_file[4] -> [blank_r]
proc_file[5] -> [blank_r]
...
Thus, printing proc_file will always print the values of the last j for
all rows greater than 1.
Maybe, this will help (although I think you got it already:
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
print "proc_file at loop entry:", proc_file
print "current blank_r:", blank_r
proc_file[i+2][1] = j[0]
proc_file[i+2][2] = j[1]
print "proc_file at loop end:", proc_file, "\n\n"
And maybe thinking of lists as objects and of variables "containing"
lists (somehow) as "pointers" may also help.
> [...] 2) What works as desired:
> proc_file.append(list(blank_r)) # Change it to list(blank_r) and it works
The list() function returns a copy of [blank_r]. So with this code,
"proc_file[-1]" refers not the same list object as "blank_r". This leads
to the desired behaviour of your program.
> 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 hope, the above helps to understand why this behaviour.is to be
expected.
So long,
Paul
[toc] | [prev] | [next] | [standalone]
| From | JBB <jeanbigboute@gmail.com> |
|---|---|
| Date | 2014-09-09 07:37 +0000 |
| Message-ID | <mailman.13890.1410248972.18130.python-list@python.org> |
| In reply to | #77715 |
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
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-09-09 05:07 -0700 |
| Message-ID | <6426e4b9-5d55-4901-99a4-027b8352d229@googlegroups.com> |
| In reply to | #77712 |
On Tuesday, September 9, 2014 11:25:29 AM UTC+5:30, JBB wrote: > 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', '']] Dont quite know what you are trying to do. Does this serve your purpose? >>> proc_file = [['a','b','c','d']] >>> proc_file.append(['A','B','C','D']) >>> blank_r = ['','','',''] >>> [[i,j0,j1, ""] for (i, (j0, j1)) in enumerate(zip(qq,rr))] [[0, 'aa', 'inky', ''], [1, 'bb', 'binky', ''], [2, 'cc', 'pinky', ''], [3, 'dd', 'clyde', '']] Or if you prefer: >>> proc_file + [[i,j0,j1, ""] for (i, (j0, j1)) in enumerate(zip(qq,rr))] [['a', 'b', 'c', 'd'], [0, 'aa', 'inky', ''], [1, 'bb', 'binky', ''], [2, 'cc', 'pinky', ''], [3, 'dd', 'clyde', '']]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web