Re: Python arrays and sting formatting options



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@xxxxxxx>
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
.



Relevant Pages

  • Re: [QUIZ] Port a Library (#64)
    ... # left and right lists differ from the original list. ... this function returns a reference to such an array. ... def Merge::diff3 ...
    (comp.lang.ruby)
  • Re: Death to tuples!
    ... one of the questions I have is why python people whould consider ... > Of course, if you wanted just the expression value as now at def time, you could write ... it means for lists, strings and a lot of other things. ... impractical effects can arise when you drop purity for practicallity. ...
    (comp.lang.python)
  • Few questions
    ... Python interpreter in Assembly for Pentium /AMD, ... I've done a little comparison of the speed of Python lists and arrays: ... from array import array ... version of the small "pslist" program, and I've put a "del v" command ...
    (comp.lang.python)
  • Re: Python 3.0 - is this true?
    ... How often do you care about equality ignoring order for lists containing ... Otherwise the following comp function in Python 2.X does ... solve the problem assuming only that the items support equality: ... def unordered_equals2: ...
    (comp.lang.python)
  • Re: C API: array of floats/ints from python to C and back
    ... take two equal length lists of integers and the C function will ... compute their sum and return the result as a python tuple. ... PyObject *list1, *list2; ... using a Numpy array or an array.array object would ...
    (comp.lang.python)