Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.04; 'interpreter': 0.05; 'element': 0.07; 'error:': 0.07; 'string': 0.09; '3.0,': 0.09; 'answering': 0.09; 'data:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'strings.': 0.09; 'way:': 0.09; 'python': 0.11; '6.0': 0.16; '6.0,': 0.16; 'array.': 0.16; 'indexerror:': 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; 'simulate': 0.16; 'subject:array': 0.16; 'two,': 0.16; 'index': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'library': 0.18; 'subject:] ': 0.20; 'seems': 0.21; 'feb': 0.22; '>>>': 0.22; 'import': 0.22; 'putting': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'entries': 0.24; "shouldn't": 0.24; 'looks': 0.24; 'question': 0.24; 'sort': 0.25; 'script': 0.25; 'task': 0.26; 'pass': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'point': 0.28; 'function': 0.29; 'array': 0.29; 'specified': 0.30; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'lines': 0.31; '-0700,': 0.31; 'cells': 0.31; 'inspect': 0.31; 'jean': 0.31; 'steven': 0.31; 'yes.': 0.31; 'anyone': 0.31; 'file': 0.32; 'lists': 0.32; 'run': 0.32; 'another': 0.32; 'text': 0.33; 'running': 0.33; 'guess': 0.33; 'skip:# 10': 0.33; 'totally': 0.33; 'comment': 0.34; 'problem': 0.35; "can't": 0.35; 'common': 0.35; 'tool': 0.35; 'convert': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'doing': 0.36; 'thanks': 0.36; "i'll": 0.36; 'should': 0.36; 'wrong': 0.37; 'so,': 0.37; 'list': 0.37; 'step': 0.37; 'e.g.': 0.38; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'previous': 0.38; 'rather': 0.38; 'little': 0.38; 'explain': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; '2nd': 0.60; 'most': 0.60; 'tell': 0.60; 'break': 0.61; 'numbers': 0.61; 'first': 0.61; "you'll": 0.62; 'address': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'different': 0.65; 'dear': 0.65; 'fire': 0.65; 'here': 0.66; 'mar': 0.68; 'invalid': 0.68; "'2',": 0.84; '2014,': 0.84; '4.0,': 0.84; 'dimensional': 0.84; 'replicate': 0.84; 'try,': 0.84; 'friend!': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: [newbie] confusion concerning fetching an element in a 2d-array Date: Tue, 25 Mar 2014 17:12:12 +0100 Organization: None References: <82a05fdc-1f5c-4c9b-9718-09930977268c@googlegroups.com> <53316211$0$29994$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8774.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: 157 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395763950 news.xs4all.nl 2942 [2001:888:2000:d::a6]:43371 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69040 Jean Dubois wrote: > Op dinsdag 25 maart 2014 12:01:37 UTC+1 schreef Steven D'Aprano: >> On Tue, 25 Mar 2014 03:26:26 -0700, Jean Dubois wrote: >> >> > I'm confused by the behaviour of the following python-script I wrote: >> > >> > #!/usr/bin/env python >> > #I first made a data file 'test.dat' with the following content >> > #1.0 2 3 >> > #4 5 6.0 >> > #7 8 9 >> > import numpy as np >> > lines=[line.strip() for line in open('test.dat')] >> > #convert lines-list to numpy-array >> > array_lines=np.array(lines) >> > #fetch element at 2nd row, 2nd column: >> > print array_lines[1, 1] >> > >> > >> > When running the script I always get the following error: IndexError: >> > invalid index >> > >> > Can anyone here explain me what I am doing wrong and how to fix it? >> >> Yes. Inspect the array by printing it, and you'll see that it is a one- >> dimensional array, not two, and the entries are strings: >> >> >> py> import numpy as np >> py> # simulate a text file >> ... data = """1.0 2 3 >> ... 4 5 6.0 >> ... 7 8 9""" >> py> lines=[line.strip() for line in data.split('\n')] >> py> # convert lines-list to numpy-array >> ... array_lines = np.array(lines) >> py> print array_lines >> ['1.0 2 3' '4 5 6.0' '7 8 9'] >> >> >> The interactive interpreter is your friend! You never need to guess what >> the problem is, Python has powerful introspection abilities, one of the >> most powerful is also one of the simplest: print. Another powerful tool >> in the interactive interpreter is help(). >> >> So, what to do about it? Firstly, convert your string read from a file >> into numbers, then build your array. Here's one way: >> >> py> values = [float(s) for s in data.split()] >> py> print values >> [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] >> py> array_lines = np.array(values) >> py> array_lines = array_lines.reshape(3, 3) >> py> print array_lines >> [[ 1. 2. 3.] >> [ 4. 5. 6.] >> [ 7. 8. 9.]] >> > Dear Steve, > Thanks for answering my question but unfortunately now I'm totally > confused. > Above I see parts from different programs which I can't > assemble together to one working program (I really tried hard). > Can I tell from your comment I shouldn't use numpy? > I also don't see how to get the value an element specified by (row, > column) from a numpy_array like "array_lines" in my original code > > All I need is a little python-example reading a file with e.g. three lines > with three numbers per line and putting those numbers as floats in a > 3x3-numpy_array, then selecting an element from that numpy_array using > it's row and column-number. I'll try, too, but be warned that I'm using the same methology as Steven. Try to replicate every step in the following exploration. First let's make sure we start with the same data: $ cat test.dat 1.0 2 3 4 5 6.0 7 8 9 Then fire up the interactve interpreter: $ python Python 2.7.5+ (default, Feb 27 2014, 19:37:08) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy >>> lines = [line.strip() for line in open("test.dat")] >>> lines ['1.0 2 3', '4 5 6.0', '7 8 9'] As you can see lines is a list of three strings. Let's break these strings into parts: >>> cells = [line.split() for line in lines] >>> cells [['1.0', '2', '3'], ['4', '5', '6.0'], ['7', '8', '9']] We now have a list of lists of strings and you can address individual items with >>> cells[1][2] '6.0' What happens when pass this list of lists of strings to the numpy.array() constructor? >>> a = numpy.array(cells) >>> a array([['1.0', '2', '3'], ['4', '5', '6.0'], ['7', '8', '9']], dtype='|S3') >>> a[1,2] '6.0' It sort of works, but the array entries are strings rather than floating point numbers. Let's fix that: >>> a = numpy.array(cells, dtype=float) >>> a array([[ 1., 2., 3.], [ 4., 5., 6.], [ 7., 8., 9.]]) >>> a[1,2] 6.0 OK, now we can put the previous steps into a script: $ cat tmp.py import numpy cells = [line.split() for line in open("test.dat")] a = numpy.array(cells, dtype=float) print a[1, 2] Run it: $ python tmp.py 6.0 Seems to work. But reading a 2D array from a file really looks like a common task -- there should be a library function for that: $ python Python 2.7.5+ (default, Feb 27 2014, 19:37:08) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy >>> numpy.loadtxt("test.dat") array([[ 1., 2., 3.], [ 4., 5., 6.], [ 7., 8., 9.]])