# Issues with partitioning data

Code:

a = 100
b = 10
p = prime_range(a)

for j in range(a/b):
fn[j] = [(i,val) for i,val in enumerate(p) if j * b < p[i] < (j+1) * b];


My code is able to partition prime numbers up to 100 in decades, but when I try to increase the prime range limit, or increase the partition then the code breaks. I am not sure why that happens? Could it be the if statement?

edit retag close merge delete

1

How exactly it breaks? Please illustrate with an example.

( 2022-04-28 00:18:50 +0200 )edit

Perhaps use a//b instead of a/b so that the argument to range is an integer?

( 2022-04-29 00:27:45 +0200 )edit

Here is an error that I get. However, I am getting this error when a = 100, and b = 10.

IndexError Traceback (most recent call last)

<ipython-input-2-80f673aa11bb> in <module>()

  5 fn = []

6 for j in range(a/b):


----> 7 fn[j] = [(i,val) for i,val in enumerate(p) if jb < p[i] < (j+Integer(1))b];

  8

9 f = [[fn[j][i][Integer(1)] for i in range(len(fn[j]))] for j in range(len(fn))]


IndexError: list assignment index out of range

I also tried the a//b, but that didn't work.

( 2022-04-30 02:39:20 +0200 )edit

Sort by » oldest newest most voted

You didn't tell us about the line fn = [] before. That makes it a list of length 0, and fn[j] = ... tries to assign to other entries of that list. You could do any of these:

a = 100
b = 10
p = prime_range(a)
fn = []

for j in range(a/b):
fn.append([(i,val) for i,val in enumerate(p) if j * b < p[i] < (j+1) * b])


or

a = 100
b = 10
p = prime_range(a)
fn = [[(i,val) for i,val in enumerate(p) if j * b < p[i] < (j+1) * b] for j in range(a/b)]


or

a = 100
b = 10
p = prime_range(a)
fn = {}

for j in range(a/b):
fn[j] = [(i,val) for i,val in enumerate(p) if j * b < p[i] < (j+1) * b]

more

So I tried using the first two examples. Both do work when a=100, and b=10. However, when a=1000,and b=10. This is when I start having issues.

So when I try calculating the average, and median I get a division by zero error.

ZeroDivisionError Traceback (most recent call last)

<ipython-input-48-5a2c6df33de7> in <module>()

----> 1 fave = [sum(fn[i])/len(fn[i]) for i in range(len(fn))]

  2 fmed = [median(fn[i]) for i in range(len(fn))]


<ipython-input-48-5a2c6df33de7> in <listcomp>(.0)

----> 1 fave = [sum(fn[i])/len(fn[i]) for i in range(len(fn))]

  2 fmed = [median(fn[i]) for i in range(len(fn))]


ZeroDivisionError: division by zero

( 2022-05-11 08:20:51 +0200 )edit

However when I check the average and median by checking an individual index of fn I get the correct number for said index.

sum(fn[0])/len(fn[0])

17/4

median(f[0])

4

( 2022-05-11 08:25:18 +0200 )edit

Have you looked at fn? There are some empty lists in there, so len(fn[i]) will be zero.

( 2022-05-11 16:41:18 +0200 )edit

No prime numbers between 200 and 210, for example, so you get an empty list for that entry.

( 2022-05-11 16:43:40 +0200 )edit

Yea I wasn't thinking about there being empty list. So I guess I will have to do an if else statement to get around that.

( 2022-05-11 17:21:46 +0200 )edit