Vikram Ganapathineedi
Vikram Ganapathineedi

Reputation: 11

Memory efficient way to read an array of integers from single line of input in python2.7

I want to read a single line of input containing integers separated by spaces. Currently I use the following.

A = map(int, raw_input().split())

But now the N is around 10^5 and I don't need the whole array of integers, I just need to read them 1 at a time, in the same sequence as the input.

Can you suggest an efficient way to do this in Python2.7

Upvotes: 1

Views: 100

Answers (3)

Martin Evans
Martin Evans

Reputation: 46779

You could parse the data a character at a time, this would reduce memory usage:

data = "1 50 30 1000   20 4 1 2"

number = []
numbers = []

for c in data:
    if c == ' ':
        if number:
            numbers.append(int(''.join(number)))
            number = []
    else:
        number.append(c)

if number:
    numbers.append(int(''.join(number)))

print numbers

Giving you:

[1, 50, 30, 1000, 20, 4, 1, 2]

Probably quite a bit slower though.


Alternatively, you could use itertools.groupby() to read groups of digits as follows:

from itertools import groupby

data = "1 50 30 1000   20 4 1 2"
numbers = []

for k, g in groupby(data, lambda c: c.isdigit()):
    if k:
        numbers.append(int(''.join(g)))

print numbers

Upvotes: 1

Chen A.
Chen A.

Reputation: 11328

Use generators:

numbers = '1 2 5 18 10 12 16 17 22 50'
gen = (int(x) for x in numbers.split())
for g in gen:
    print g


1
5
6
8
10
12
68
13

the generator object would use one item at a time, and won't construct a whole list.

Upvotes: 1

jhpratt
jhpratt

Reputation: 7120

If you're able to destroy the original string, split accepts a parameter for the maximum number of breaks.

See docs for more details and examples.

Upvotes: 0

Related Questions