Steven D'Aprano added the comment:
There is also an FAQ about this:
https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list
See also:
https://docs.python.org/3/faq/programming.html#why-did-changing-list-y-also-change-list-x
By the way, for future bug reports,
Steven D'Aprano added the comment:
This is not a bug, it is working correctly, as designed.
List multiplication does not *copy* the list, it replicates the reference to
the same list object. So [[0]]*5 does not make five different sublists, but the
same list repeated five times. Exactly the
print()
d=d-2
print(d)
print(B)
if d==0:
B[int(A/2)][int(A/2)]=data
print(B[int(A/2)][int(A/2)])
print(B)
return B
#This code is for generating spiral matrix
##if i'm using different approach to create 2d list . Then i'm getting right
ou
Change by sandeep kumar :
--
title: Bug in methods of creating the list -> Bug in methods of creating the 2d
list
___
Python tracker
<https://bugs.python.org/issu
Ezio Melotti added the comment:
Not a bug, see
https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list
and
https://docs.python.org/3/faq/programming.html#why-did-changing-list-y-also-change-list-x
--
assignee: -> ezio.melotti
nosy: +ezio.melotti
New submission from Yaowei Zhang:
#If we define a list by:
nums = [[0]*3]*3
#nums = [[0,0,0],[0,0,0],[0,0,0]]
nums[0][1] = 1
print(mums)
# the result is
[[0,1,0],[0,1,0],[0,1,0]]
#I think it is because all the line use the same list variable, but it suppose
#not.
--
components: +Build
Changes by Yaowei Zhang <yzh...@gmail.com>:
--
components: Library (Lib)
nosy: Yaowei Zhang
priority: normal
severity: normal
status: open
title: 2D list with Bugs when defined by using Multiplication
versions: Python 3.5
___
Python tracke
Hello!
I'm new here and fairly new to Python. I am attempting to read a data file into
python and adding it to a 2D list to make it easy to use further down the line.
My data file is just 7 numbers in a row seperated by commas and each bulk of
data is seperated by the sign @ to indicate
On 07/09/2013 09:30 AM, alex.ha...@gmail.com wrote:
Hello!
I'm new here and fairly new to Python. I am attempting to read a data file into
python and adding it to a 2D list to make it easy to use further down the line.
My data file is just 7 numbers in a row seperated by commas and each bulk
Replace te **start line with something like:
object_data.append([])
i += 1
This assumes a few missing lines, which must have been there or you
would have already had runtime errors. For example, you'll need i=0
before the loop.
Another
]]
I expected the 2D list:
[[ 7, 10, 13],
[21, 24, 27]]
Any ideas?
Thanks,
Rob
PS: I used Python 2.7.3
--
http://mail.python.org/mailman/listinfo/python-list
[1:5:2][::3]
[[7, 8, 9, 10, 11, 12, 13]]
I expected the 2D list:
[[ 7, 10, 13],
[21, 24, 27]]
Any ideas?
It is not really a 2D list; rather a list of lists. You cannot see the two
slices together, the slicing happens in two separate steps.
y[1:5:2] is a list containing two items
],
[21, 22, 23, 24, 25, 26, 27],
[28, 29, 30, 31, 32, 33, 34]]
y[1:5:2][::3]
[[7, 8, 9, 10, 11, 12, 13]]
I expected the 2D list:
[[ 7, 10, 13],
[21, 24, 27]]
Any ideas?
y is just a list. It happens to be a list of lists, but that doesn't make
it a 2D list. It's an important
,
29, 30, 31, 32, 33, 34]]
y[1:5:2][::3]
[[7, 8, 9, 10, 11, 12, 13]]
I expected the 2D list:[[ 7, 10, 13],
[21, 24, 27]]
Any ideas?
Thanks,
Rob
PS: I used Python 2.7.3
The explanation is rather simple, just break up your complex slicing into
its parts:
y[1:5:2] = [[7, 8, 9, 10
expected the 2D list:[[ 7, 10, 13],
[21, 24, 27]]
Any ideas?
Thanks,
Rob
PS: I used Python 2.7.3
The explanation is rather simple, just break up your complex slicing into
its parts:
y[1:5:2] = [[7, 8, 9, 10, 11, 12, 13],[21, 22, 23, 24, 25, 26, 27]]
and [::3] is asking
On Mon, Oct 11, 2010 at 1:44 PM, Tom Pacheco tomsli...@netp.org wrote:
your creating a 1d list
this creates a 2d list
a=[[[]]*5
[0]*5
[0, 0, 0, 0, 0]
[[]]*5
[[], [], [], [], []]
Don't do this. This actually just creates a list containing the same empty
list 5 times:
a = [[]] * 5
a = [0]*5
for i in range(0, 4):
for j in range(0, i):
a[i].append(j)
why the above codes show the following error. and how to overcome it.
Traceback (most recent call last):
File pyshell#10, line 3, in module
a[i].append(j)
AttributeError: 'int' object has no attribute
the declaration is wrong
if you want to create a two dimensional array try to use functions like
arange and reshape
On Mon, Oct 11, 2010 at 9:54 PM, Fasihul Kabir rrock...@yahoo.com wrote:
a = [0]*5
for i in range(0, 4):
for j in range(0, i):
a[i].append(j)
why the above
On Mon, Oct 11, 2010 at 9:24 AM, Fasihul Kabir rrock...@yahoo.com wrote:
a = [0]*5
for i in range(0, 4):
for j in range(0, i):
a[i].append(j)
why the above codes show the following error. and how to overcome it.
Traceback (most recent call last):
File pyshell#10, line 3, in
On 10/11/2010 09:24 AM, Fasihul Kabir wrote:
a = [0]*5
for i in range(0, 4):
for j in range(0, i):
a[i].append(j)
why the above codes show the following error. and how to overcome it.
Traceback (most recent call last):
File pyshell#10, line 3, in module
a[i].append(j)
)
AttributeError: 'int' object has no attribute 'append'
your creating a 1d list
this creates a 2d list
a=[[[]]*5
[0]*5
[0, 0, 0, 0, 0]
[[]]*5
[[], [], [], [], []]
- tom
--
http://mail.python.org/mailman/listinfo/python-list
Maric Michaud [EMAIL PROTECTED] writes:
Le Friday 13 June 2008 17:55:44 Karsten Heymann, vous avez écrit :
Maric Michaud [EMAIL PROTECTED] writes:
So, writing C in python, which has dictionnary as builtin type,
should be considered more elegant ?
IMO that's a bit harsh.
harsh ? Sorry,
On Jun 12, 3:48 pm, Mark [EMAIL PROTECTED] wrote:
Is this possible?
def foobar(user,score):
sums = {}
for u,s in zip(user,score):
try:
sums[u] += s
except KeyError:
sums[u] = s
return [(u, sums[u]) for u in sums].sort()
usersum = foobar(user,score)
for
On Jun 14, 4:05 pm, sturlamolden [EMAIL PROTECTED] wrote:
On Jun 12, 3:48 pm, Mark [EMAIL PROTECTED] wrote:
Is this possible?
def foobar(user,score):
sums = {}
for u,s in zip(user,score):
try:
sums[u] += s
except KeyError:
sums[u] = s
return [(u,
Hi Mark,
Mark [EMAIL PROTECTED] writes:
I have a scenario where I have a list like this:
UserScore
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4 3
4
On Fri, Jun 13, 2008 at 2:12 PM, Karsten Heymann
[EMAIL PROTECTED] wrote:
Although your problem has already been solved, I'd like to present a
different approach which can be quite a bit faster. The most common
approach seems to be using a dictionary:
summed_up={}
for user,vote in pairs:
On Thu, Jun 12, 2008 at 3:48 PM, Mark [EMAIL PROTECTED] wrote:
Hi all,
I have a scenario where I have a list like this:
UserScore
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4
BJörn Lindqvist wrote:
[...]
Here is another solution:
from itertools import groupby
from operator import itemgetter
users = [1, 1, 1, 2, 2, 3, 4, 4, 4]
scores = [0, 1, 5, 3, 1, 2, 3, 3, 2]
for u, s in groupby(zip(users, scores), itemgetter(0)):
print u, sum(y for x, y in s)
Except that
Hi Björn,
BJörn Lindqvist [EMAIL PROTECTED] writes:
On Fri, Jun 13, 2008 at 2:12 PM, Karsten Heymann
[EMAIL PROTECTED] wrote:
summed_up={}
for user,vote in pairs:
if summed_up.has_key(user):
summed_up[user]+=vote
else:
summed_up[user]=vote
You'll save even more by using:
if
On Jun 13, 1:12 pm, Karsten Heymann [EMAIL PROTECTED]
wrote:
Hi Mark,
Mark [EMAIL PROTECTED] writes:
I have a scenario where I have a list like this:
UserScore
1 0
1 1
1 5
2 3
2 1
3
Le Friday 13 June 2008 14:12:40 Karsten Heymann, vous avez écrit :
Hi Mark,
Mark [EMAIL PROTECTED] writes:
I have a scenario where I have a list like this:
User Score
1 0
1 1
1 5
2 3
2 1
Paddy [EMAIL PROTECTED] writes:
How does your solution fare against the defaultdict solution of:
d = collections.defaultdict(int)
for u,s in zip(users,score): d[u] += s
list: 0.931s
dict + in: 1.495s
defaultdict : 1.991s
dict + if: ~2s
dict + try: ~4s
I've posted the (very rough)
Hi Maric,
Maric Michaud [EMAIL PROTECTED] writes:
So, writing C in python, which has dictionnary as builtin type,
should be considered more elegant ?
IMO that's a bit harsh.
You are comparing apples with lemons, there is no such a difference
between list index access and dictionnary key
Hello,
Le Friday 13 June 2008 17:55:44 Karsten Heymann, vous avez écrit :
Maric Michaud [EMAIL PROTECTED] writes:
So, writing C in python, which has dictionnary as builtin type,
should be considered more elegant ?
IMO that's a bit harsh.
harsh ? Sorry, I'm not sure to understand.
You
Le Friday 13 June 2008 18:55:24 Maric Michaud, vous avez écrit :
approximately the double amount of memory compared to the other.
I don't see how you came to this conclusion. Are you sure the extra list
take twice more memory than the extra dictionary ?
twice less, I meant, of course...
--
Hi all,
I have a scenario where I have a list like this:
UserScore
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4 3
4 2
And I need to add up the score for
Mark wrote:
Hi all,
I have a scenario where I have a list like this:
UserScore
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4 3
4 2
And I
On Thu, Jun 12, 2008 at 9:48 AM, Mark [EMAIL PROTECTED] wrote:
Hi all,
I have a scenario where I have a list like this:
UserScore
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4
On Jun 12, 3:48 pm, Mark [EMAIL PROTECTED] wrote:
Hi all,
I have a scenario where I have a list like this:
User Score
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4
On Jun 12, 3:02 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote:
Mark wrote:
Hi all,
I have a scenario where I have a list like this:
User Score
1 0
1 1
1 5
2 3
2 1
3 2
4
Mark wrote:
Hi all,
I have a scenario where I have a list like this:
UserScore
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4 3
4 2
And I need to add up
Mark [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
On Jun 12, 3:02 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote:
Mark wrote:
---
This was my (failed) attempt:
predictions = Prediction.objects.all()
scores = []
for prediction in predictions:
i = [prediction.predictor.id, 0]
if
To be honest I'm relatively new to Python, so I don't know too much
about how all the loop constructs work and how they differ to other
languages. I'm building an app in Django and this data is coming out
of a database and it looks like what I put up there!
This was my (failed) attempt:
John, it's a QuerySet coming from a database in Django. I don't know
enough about the structure of this object to go into detail I'm
afraid.
Aidan, I got an error trying your suggestion: 'zip argument #2 must
support iteration', I don't know what this means!
Thanks to all who have answered!
Mark wrote:
John, it's a QuerySet coming from a database in Django. I don't know
enough about the structure of this object to go into detail I'm
afraid.
Aidan, I got an error trying your suggestion: 'zip argument #2 must
support iteration', I don't know what this means!
well, if we can create
Aidan wrote:
Mark wrote:
John, it's a QuerySet coming from a database in Django. I don't know
enough about the structure of this object to go into detail I'm
afraid.
Aidan, I got an error trying your suggestion: 'zip argument #2 must
support iteration', I don't know what this means!
well, if
Mark wrote:
John, it's a QuerySet coming from a database in Django. I don't know
enough about the structure of this object to go into detail I'm
afraid. [...]
Then let the database do the summing up. That's what it's there for :-)
select user, sum(score) from score_table
group by user
or
Aidan wrote:
does this work for you?
users = [1,1,1,2,2,3,4,4,4]
score = [0,1,5,3,1,2,3,3,2]
d = dict()
for u,s in zip(users,score):
if d.has_key(u):
d[u] += s
else:
d[u] = s
for key in d.keys():
print 'user: %d\nscore: %d\n' % (key,d[key])
I've recently had the very same
On Jun 12, 3:45 pm, Aidan [EMAIL PROTECTED] wrote:
Aidan wrote:
Mark wrote:
John, it's a QuerySet coming from a database in Django. I don't know
enough about the structure of this object to go into detail I'm
afraid.
Aidan, I got an error trying your suggestion: 'zip argument #2 must
On Jun 12, 4:14 pm, Gerhard Häring [EMAIL PROTECTED] wrote:
Aidan wrote:
does this work for you?
users = [1,1,1,2,2,3,4,4,4]
score = [0,1,5,3,1,2,3,3,2]
d = dict()
for u,s in zip(users,score):
if d.has_key(u):
d[u] += s
else:
d[u] = s
for key in d.keys():
# Copyright (C) 2007 Darren Lee Weber
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This
I am not sure what the right syntax is here. So please help me out (started 2
days ago).
I have a list of about 20 files that I want to read line by line into a 2D
list. So the first dimension will be each file, and the second every line in
that file.
I tried to do something like
Øyvind Østlund wrote:
I have a list of about 20 files that I want to read line by
line into a 2D list. So the first dimension will be each file,
and the second every line in that file.
I tried to do something like this:
files_and_lines = [][]
filenumber = 0
for files
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Klaus Alexander
Seistrup
Sent: Wednesday, May 11, 2005 12:14 PM
To: python-list@python.org
Subject: Re: Reading files into a 2D list.
Øyvind Østlund wrote:
I have a list of about 20 files that I want
. So please help me out (started 2
days ago).
I have a list of about 20 files that I want to read line by line into a 2D
list. So the first dimension will be each file, and the second every line in
that file.
I tried to do something like this:
files_and_lines
like
DirectX lately.
Thanks
ØØ
-Original Message-
From: Larry Bates [mailto:[EMAIL PROTECTED]
Sent: Wednesday, May 11, 2005 3:55 PM
To: Oyvind Ostlund
Cc: python-list@python.org
Subject: Re: Reading files into a 2D list.
Few observations.
1) Don't concatenate pathnames yourself use
bearphile, Is there a way I could do the floodfill rather iteratively
than recursive. It somehow breaks although I made some optimisations to
the code.
--
http://mail.python.org/mailman/listinfo/python-list
Just was wondering how to integrate the floodfill with the stack.I
guess it will get rid of recursion problem. You mean read all the
elements of the array to a stack and then push and pop based on
conditions?
--
http://mail.python.org/mailman/listinfo/python-list
In my first post you can find two URLs with better flood filling
algorithms. You can also modify the easy recursive function, using a
list-based stack intead of recursive calls.
Bye,
Bearophile
--
http://mail.python.org/mailman/listinfo/python-list
Bearophile,I have written the floodfill in python with stack. But i
think I am making something wrong I dont get what i need.Need your
opinion.the code follows
def connected(m, foreground=1, background=0):
def floodFill4(m, r,c, newCol, oldCol):
if newCol == oldCol:
print
def floodFill4(m, r, c, newCol, oldCol):
stack = [ (r, c) ]
while stack:
r, c = stack.pop()
if c =0 and c maxc and r =0 and r maxr and m[r][c]==oldCol:
m[r][c] = newCol
stack += [ (r+1, c), (r-1, c), (r, c+1), (r, c-1) ]
--
Then you can probably use something like this:
. def boxesArea(m, foreground=1, background=0):
. maxr = len(m)
. maxc = len(m[0])
. newCol = 2
. oldCol = foreground
. for r,row in enumerate(m):
. for c,e in enumerate(row):
. if e == oldCol:
.
Richter,yes what I am looking for is for cluster with rectangular
bounding boxes as you explained in the first figure.
--
http://mail.python.org/mailman/listinfo/python-list
hi Bearphile! That really gives me an idea.Thanks much for that. Yes as
you said the algorithm reaches a maximium recursion depth for larger
sets i tried.I still have a question. if
m = [[0,0,0,0],[0,1,1,0,0],[0,0,1,0,0],[0,0,0,0]]
all it does is count the number of 1's and return us the number
On 24 Apr 2005 09:44:49 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
Richter,yes what I am looking for is for cluster with rectangular
bounding boxes as you explained in the first figure.
Sorry, not your fault, but I'm still not clear on diagonal
connection/separation.
E.g., (removing
[EMAIL PROTECTED]:
hi Bearphile! That really gives me an idea.Thanks much for that. Yes
as
you said the algorithm reaches a maximium recursion depth for larger
sets i tried.
You can improve the little flood filling function, avoiding the bad
Python recursivity.
Do you see where I am heading
If I have
ex: x = [[1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0],
[1,1,1,0,1,1,0,0,0,0,0,1,1,1,1,0,0,0],
[1,1,1,0,1,1,1,0,1,1,0,1,1,1,0,0,0,0],
[0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0]]
what I want is a boundingbox over the region where we find clusters of
1's.So for instance in
I hope this is what you need, sometimes understanding the question is
one of the hardest parts :-)
If you can use a graph data structure, you can create a graph, and then
you can find the lenght of all its connected components (indentations
reduced to 2 spaces):
. def mat2graph(g, m, good=None,
On 23 Apr 2005 13:17:55 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
If I have
ex: x = [[1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0],
[1,1,1,0,1,1,0,0,0,0,0,1,1,1,1,0,0,0],
[1,1,1,0,1,1,1,0,1,1,0,1,1,1,0,0,0,0],
[0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0]]
what I want is a
69 matches
Mail list logo