Ask Your Question
1

Issues with partitioning data

asked 2022-04-27 17:44:00 +0200

nooniensoong97 gravatar image

updated 2022-04-27 20:14:04 +0200

FrédéricC gravatar image

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 flag offensive close merge delete

Comments

1

How exactly it breaks? Please illustrate with an example.

Max Alekseyev gravatar imageMax Alekseyev ( 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?

John Palmieri gravatar imageJohn Palmieri ( 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.

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

1 Answer

Sort by » oldest newest most voted
0

answered 2022-04-30 05:39:49 +0200

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]
edit flag offensive delete link more

Comments

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

nooniensoong97 gravatar imagenooniensoong97 ( 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

nooniensoong97 gravatar imagenooniensoong97 ( 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.

John Palmieri gravatar imageJohn Palmieri ( 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.

John Palmieri gravatar imageJohn Palmieri ( 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.

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-04-27 17:44:00 +0200

Seen: 137 times

Last updated: Apr 30 '22