Python arrays and sting formatting options

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue Sep 30 16:34:31 CEST 2008


On Tue, 30 Sep 2008 15:42:58 +0200, Ivan Reborin wrote:

> On 30 Sep 2008 07:07:52 GMT, Marc 'BlackJack' Rintsch <bj_666 at gmx.net>
> wrote:
>>=====
>>from __future__ import with_statement from functools import partial
>>from itertools import islice
>>from pprint import pprint
>>
>>
>>def read_group(lines, count):
>>    return [map(int, s.split()) for s in islice(lines, count)]
>>
>>def main():
>>    with open('test.txt') as lines:
>>        lines = (line for line in lines if line.strip())
>>        result = list(iter(partial(read_group, lines, 3), list()))
>>    pprint(result, width=30)
>>
>>if __name__ == '__main__':
>>    main()
>>=====
> 
> I'm afraid I must admit I find the code above totally uncomprehesible (I
> can't even see where the array here is mentioned - "result"?) and
> inpractical for any kind of engineering practice I had in mind.

Learn Python then to understand that code.  ;-)

There is no array.  The data type is called "list" in Python, so `result` 
is a nested list.  And in Python it quite unusual to build lists by 
creating them with the final size filled with place holder objects and 
then fill the real values in.  Instead lists are typically created by 
appending values to existing lists, using list comprehension or the
`list()` function with some iterable object.

Typical Python code tries to minimize the use of index variables.  Python 
is not Fortran (or C, or Pascal, …).

> Does python, perchance, have some wrapper functions or something, which
> would allow one to load an array in a more natural "technical" way ?
> Like something mentioned above in my post (now deleted) ?
> 
> Also, is there a way to change counter for arrays to go from 0 to 1 ?

You can write your own sequence type but that would be odd because the 
rest of the language expects zero as the first index, so you will be 
constantly fighting the language by adding or subtracting 1 all the time 
at the "border" between your custom sequence type and the the rest of 
Python.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list