Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '[0,': 0.09; 'data):': 0.09; 'data:': 0.09; 'here?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'changes': 0.15; '0],': 0.16; 'array.': 0.16; 'entries.': 0.16; 'likewise': 0.16; 'link,': 0.16; 'mutable': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'shortcut': 0.16; 'simplified': 0.16; 'index': 0.16; 'modification': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'shape': 0.19; 'example': 0.22; 'header :User-Agent:1': 0.23; 'entries': 0.24; 'header:X-Complaints-To:1': 0.27; 'array': 0.29; "doesn't": 0.30; '(like': 0.30; 'reply.': 0.31; '>>>>': 0.31; 'values.': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'problem': 0.35; 'shows': 0.36; 'list': 0.37; 'thank': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'determine': 0.67; 'six': 0.68; 'subject:Sets': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Numpy Array of Sets Date: Sun, 25 May 2014 15:26:42 +0200 Organization: None References: <38836877-cd87-44ce-b9df-1eda702e7164@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9f71.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401024422 news.xs4all.nl 2968 [2001:888:2000:d::a6]:54004 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72006 LJ wrote: > Wolfgang, thank you very much for your reply. > > Following the example in the link, the problem appears: > >>>> A = [[0]*2]*3 You can see this as a shortcut for value = 0 inner = [value, value] A = [inner, inner, inner] When the value is mutable (like your original set) a modification of the value shows in all six entries. Likewise if you change the `inner` list the modification shows in all three rows. >>>> A > [[0, 0], [0, 0], [0, 0]] >>>> A[0][0] = 5 >>>> A > [[5, 0], [5, 0], [5, 0]] > > Now, if I use a numpy array: > >>>> d=array([[0]*2]*3) >>>> d > array([[0, 0], > [0, 0], > [0, 0]]) >>>> d[0][0]=5 >>>> d > array([[5, 0], > [0, 0], > [0, 0]]) > > > What is the difference here? Basically a numpy array doesn't reference the lists, it uses them to determine the required shape of the array. A simplified implementation might be class Array: def __init__(self, data): self.shape = (len(data), len(data[0])) self._data = [] for row in data: self._data.extend(row) def __getitem__(self, index): y, x = index return self._data[y * self.shape[1] + x] With that approach you may only see simultaneous changes of multiple entries when using mutable values.