user4093955
user4093955

Reputation:

Script returns "Killed"

I have this rather simple script which generates 1000000000 (nine zeroes) numbers and then store in a file the generated numbers a how many times they were generated.

import random
import csv

dic = {}

for i in range(0, 1000000000):
    n = random.randint(0, 99999)
    if n in dic:
        dic[n] += 1
    else:
        dic[n] = 1
writer = csv.writer(open('output', 'w'))
for key, value in dic.iteritems():
    writer.writerow([key, value])
writer.close()

The script is exiting with a Killed message. According to this question What does 'killed' mean?, using dic.iteritems() should be enough for preventing such issue, but that's not the case.

So how could I proceed to accomplish such task?

Upvotes: 1

Views: 1166

Answers (1)

user2357112
user2357112

Reputation: 281757

It doesn't look like your problem is the dict. Your problem is here:

for i in range(0, 1000000000):
         ^^^^^^^^^^^^^^^^^^^^

On Python 2, that's a list 1000000000 items long, more than your system can handle. You want xrange, not range. xrange generates numbers on demand. (On Python 3, range does what xrange used to and xrange is gone.)

Oh, and if you think 11 GB should be enough for that list: not in Python. Try sys.getsizeof(0) to see how many bytes an int takes.

Upvotes: 5

Related Questions