Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99204

Re: Problem to read from array

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Problem to read from array
Date Sat, 21 Nov 2015 12:23:31 +0100
Organization None
Lines 92
Message-ID <mailman.34.1448105027.2291.python-list@python.org> (permalink)
References <2141d7ae-1064-49f3-bf36-ff663fca0ffc@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de D0kuHjBpIacmfMWgQz9liAs8TDWH63NAtedI6kSwOOdA==
Return-Path <python-python-list@m.gmane.org>
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; 'error:': 0.05; 'method.': 0.05; 'skip:p 60': 0.05; 'assignment': 0.07; 'line:': 0.07; 'closed.': 0.09; 'open()': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'throw': 0.09; 'python': 0.10; 'index': 0.13; 'bounds': 0.16; 'columns': 0.16; 'indexerror:': 0.16; 'range(0,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Problem': 0.16; 'subject:array': 0.16; 'wrote:': 0.16; 'skip:l 30': 0.18; 'load': 0.20; '(not': 0.20; 'fix': 0.21; 'parameter': 0.22; 'written': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'parameters': 0.27; 'skip:( 20': 0.28; 'away.': 0.29; 'array': 0.29; 'minimal': 0.30; 'statement': 0.32; 'run': 0.33; 'problem': 0.33; 'file': 0.34; 'lists': 0.34; 'list': 0.34; 'could': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'there': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'hi,': 0.38; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'your': 0.60; 'email addr:gmail.com': 0.62; 'further': 0.62; 'per': 0.62; 'skip:n 10': 0.62; 'more': 0.63; 'idiomatic': 0.84; 'remark': 0.84; 'subject:read': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8881.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99204

Show key headers only | View raw


vostrushka@gmail.com wrote:

> Hi,
> I have a file with one parameter per line:
> a1
> b1
> c1
> a2
> b2
> c2
> a3
> b3
> c3
> ...
> The parameters are lines of characters (not numbers)
> 
> I need to load it to 2D array for further manipulations.
> So far I managed to upload this file into 1D array:
> 
> ParametersRaw = []

No need for this assignment as you throw the value away.
> with open(file1) as fh:
>     ParametersRaw = fh.readlines()
> fh.close()

The with open() ... statement already ensures that the file is closed.

> NumberOfColumns = 7
> NumberOfRows = len(ParametersRaw)/NumberOfColumns
> 
> Parameters = [[],[]]
> i=0
> j=0
> k=0
> while (i < NumberOfRows):
>     while (j < NumberOfColumns):
>         k = (i*NumberOfColumns)+j
>         Parameters[i][j] = ParametersRaw[k]

The inner lists Parameters[i] are empty so every assignment

Parameters[i][k] = ...

will fail. While the minimal fix is to use append()

          Parameters[i].append(ParametersRaw[k])

you will run into the same problem with the rows if there are more than the 
two you prepared for in the line

> Parameters = [[],[]]

>         j = j + 1
>     i = i + 1
>     j = 0
> 
> it fails at the line "Parameters[i][j] = ParametersRaw[k]" with error:
> 
> IndexError: index 0 is out of bounds for axis 0 with size 0
> 
> In case of populating 1D array I would use append() method.
> But in case of 2D I am lost of how append() can be applied.
> Could some one help newbie.

As a general remark in idiomatic Python the loop

i = 0
while i < THRESHOLD:
   ...
   i = i + 1

is written as a for loop with range():

for i in range(THRESHOLD):
   ...

For your specific problem list slicing is a good approach:

COLUMNS = 7
with open(file1) as f:
    parameters_raw = f.readlines()

parameters = []
for row_start in range(0, len(parameters_raw), COLUMNS):
    parameters.append(parameters_raw[row_start:row_start+COLUMNS])


 


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Problem to read from array vostrushka@gmail.com - 2015-11-21 02:41 -0800
  Re: Problem to read from array Peter Otten <__peter__@web.de> - 2015-11-21 12:23 +0100
  Re: Problem to read from array Laura Creighton <lac@openend.se> - 2015-11-21 15:22 +0100
  Re: Problem to read from array Laura Creighton <lac@openend.se> - 2015-11-21 16:05 +0100
  Re: Problem to read from array BartC <bc@freeuk.com> - 2015-11-21 15:26 +0000
    Re: Problem to read from array Nathan Hilterbrand <nhilterbrand@gmail.com> - 2015-11-21 10:52 -0500
      Re: Problem to read from array Crane Ugly <vostrushka@gmail.com> - 2015-11-23 12:58 -0800
        Re: Problem to read from array sohcahtoa82@gmail.com - 2015-11-23 16:44 -0800
          Re: Problem to read from array Crane Ugly <vostrushka@gmail.com> - 2015-11-24 00:56 -0800
            Re: Problem to read from array Peter Otten <__peter__@web.de> - 2015-11-24 10:31 +0100

csiph-web