[issue43157] Bug in methods of creating the 2d list

2021-02-07 Thread Steven D'Aprano
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,

[issue43157] Bug in methods of creating the 2d list

2021-02-07 Thread Steven D'Aprano
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

[issue43157] Bug in methods of creating the 2d list

2021-02-07 Thread sandeep kumar
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

[issue43157] Bug in methods of creating the 2d list

2021-02-07 Thread sandeep kumar
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

[issue29584] 2D list with Bugs when defined by using Multiplication

2017-02-16 Thread Ezio Melotti
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

[issue29584] 2D list with Bugs when defined by using Multiplication

2017-02-16 Thread Yaowei Zhang
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

[issue29584] 2D list with Bugs when defined by using Multiplication

2017-02-16 Thread Yaowei Zhang
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

Reading File Into 2D List

2013-07-09 Thread alex . hanga
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

Re: Reading File Into 2D List

2013-07-09 Thread Dave Angel
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

Re: Reading File Into 2D List

2013-07-09 Thread alex . hanga
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

Strange behavior for a 2D list

2013-04-18 Thread Robrecht W. Uyttenhove
]] 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

Re: Strange behavior for a 2D list

2013-04-18 Thread Peter Otten
[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

Re: Strange behavior for a 2D list

2013-04-18 Thread John Gordon
], [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

Re: Strange behavior for a 2D list

2013-04-18 Thread Wolfgang Maier
, 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

Re: Strange behavior for a 2D list

2013-04-18 Thread Wim R. Cardoen
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

Re: 2D List

2010-10-12 Thread Ian Kelly
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

2D List

2010-10-11 Thread Fasihul Kabir
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

Re: 2D List

2010-10-11 Thread Nitin Pawar
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

Re: 2D List

2010-10-11 Thread Chris Rebert
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

Re: 2D List

2010-10-11 Thread Gary Herron
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)

Re: 2D List

2010-10-11 Thread Tom Pacheco
) 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

Re: Summing a 2D list

2008-06-14 Thread Karsten Heymann
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,

Re: Summing a 2D list

2008-06-14 Thread sturlamolden
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

Re: Summing a 2D list

2008-06-14 Thread MRAB
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,

Re: Summing a 2D list

2008-06-13 Thread Karsten Heymann
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

Re: Summing a 2D list

2008-06-13 Thread BJörn Lindqvist
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:

Re: Summing a 2D list

2008-06-13 Thread BJörn Lindqvist
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

Re: Summing a 2D list

2008-06-13 Thread Gerhard Häring
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

Re: Summing a 2D list

2008-06-13 Thread Karsten Heymann
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

Re: Summing a 2D list

2008-06-13 Thread Paddy
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

Re: Summing a 2D list

2008-06-13 Thread Maric Michaud
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

Re: Summing a 2D list

2008-06-13 Thread Karsten Heymann
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)

Re: Summing a 2D list

2008-06-13 Thread Karsten Heymann
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

Re: Summing a 2D list

2008-06-13 Thread Maric Michaud
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

Re: Summing a 2D list

2008-06-13 Thread Maric Michaud
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... --

Summing a 2D list

2008-06-12 Thread Mark
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

Re: Summing a 2D list

2008-06-12 Thread Diez B. Roggisch
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

Re: Summing a 2D list

2008-06-12 Thread Benjamin Kaplan
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

Re: Summing a 2D list

2008-06-12 Thread Chris
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                

Re: Summing a 2D list

2008-06-12 Thread Mark
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  

Re: Summing a 2D list

2008-06-12 Thread Aidan
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

Re: Summing a 2D list

2008-06-12 Thread John Salerno
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

Re: Summing a 2D list

2008-06-12 Thread Diez B. Roggisch
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:

Re: Summing a 2D list

2008-06-12 Thread Mark
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!

Re: Summing a 2D list

2008-06-12 Thread Aidan
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

Re: Summing a 2D list

2008-06-12 Thread Aidan
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

Re: Summing a 2D list

2008-06-12 Thread Gerhard Häring
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

Counting things fast - was Re: Summing a 2D list

2008-06-12 Thread Gerhard Häring
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

Re: Summing a 2D list

2008-06-12 Thread Mark
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

Re: Counting things fast - was Re: Summing a 2D list

2008-06-12 Thread Paddy
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():

native python matrix class (2D list), without inverse

2007-06-13 Thread DarrenWeber
# 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

Reading files into a 2D list.

2005-05-11 Thread Oyvind Ostlund
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

Re: Reading files into a 2D list.

2005-05-11 Thread Klaus Alexander Seistrup
Ø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

RE: Reading files into a 2D list.

2005-05-11 Thread Oyvind Ostlund
-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

Re: Reading files into a 2D list.

2005-05-11 Thread Larry Bates
. 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

RE: Reading files into a 2D list.

2005-05-11 Thread Oyvind Ostlund
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

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread [EMAIL PROTECTED]
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

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread [EMAIL PROTECTED]
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

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread bearophileHUGS
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

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread [EMAIL PROTECTED]
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

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread Lonnie Princehouse
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) ] --

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread bearophileHUGS
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: .

Re: Bounding box on clusters in a 2D list

2005-04-24 Thread [EMAIL PROTECTED]
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

Re: Bounding box on clusters in a 2D list

2005-04-24 Thread [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.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

Re: Bounding box on clusters in a 2D list

2005-04-24 Thread Bengt Richter
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

Re: Bounding box on clusters in a 2D list

2005-04-24 Thread bearophileHUGS
[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

Bounding box on clusters in a 2D list

2005-04-23 Thread [EMAIL PROTECTED]
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

Re: Bounding box on clusters in a 2D list

2005-04-23 Thread bearophileHUGS
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,

Re: Bounding box on clusters in a 2D list

2005-04-23 Thread Bengt Richter
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