python “IndexError: index 8 is out of bounds for axis 0 with size 8”Finding the index of an item given a list containing it in PythonHow do I remove an element from a list by index in Python?How to check file size in python?Pythonic way to check if two list of list of arrays are equalIndexError: index is out of bounds for axis 0 with sizeIndexError: index 1 is out of bounds for axis 0 with size 1 // PythonIndexError: index 1 is out of bounds for axis 0 with size 1 PythonIndexError: index 21 is out of bounds for axis 0 with size 0Checking if prime number then printing factorssingle positional indexer is out-of-bounds index error
Can I use my Chinese passport to enter China after I acquired another citizenship?
Is there a good way to store credentials outside of a password manager?
Everything Bob says is false. How does he get people to trust him?
There is only s̶i̶x̶t̶y one place he can be
What would be the benefits of having both a state and local currencies?
Modify casing of marked letters
How to be diplomatic in refusing to write code that breaches the privacy of our users
Can criminal fraud exist without damages?
Is expanding the research of a group into machine learning as a PhD student risky?
Where in the Bible does the greeting ("Dominus Vobiscum") used at Mass come from?
What's a natural way to say that someone works somewhere (for a job)?
What is the oldest known work of fiction?
Opposite of a diet
Implement the Thanos sorting algorithm
Time travel short story where a man arrives in the late 19th century in a time machine and then sends the machine back into the past
Failed to fetch jessie backports repository
Was the picture area of a CRT a parallelogram (instead of a true rectangle)?
Ways to speed up user implemented RK4
How do I define a right arrow with bar in LaTeX?
How does it work when somebody invests in my business?
Why did Kant, Hegel, and Adorno leave some words and phrases in the Greek alphabet?
What to do with wrong results in talks?
Coordinate position not precise
Increase performance creating Mandelbrot set in python
python “IndexError: index 8 is out of bounds for axis 0 with size 8”
Finding the index of an item given a list containing it in PythonHow do I remove an element from a list by index in Python?How to check file size in python?Pythonic way to check if two list of list of arrays are equalIndexError: index is out of bounds for axis 0 with sizeIndexError: index 1 is out of bounds for axis 0 with size 1 // PythonIndexError: index 1 is out of bounds for axis 0 with size 1 PythonIndexError: index 21 is out of bounds for axis 0 with size 0Checking if prime number then printing factorssingle positional indexer is out-of-bounds index error
The problem requires me to write a function that compiles a list of prime numbers from the output of a given function that gives 2 lists, one list of numbers from 2 to an arbitrary number and a second list that matches the numbers in the first list with "true" or "False" values depending on whether the numbers in the first list are prime or not.
I have no idea whether my code is just fundamentally wrong to answer the question, or if I am on the right track and just made an error...
Any help would be immensely appreciated.
The question:
Write a function (called primes_list) that takes a single input N. This function must make use of the prime_sieve function to compute and return an array (or list) of only prime numbers less than or equal to N+1.
For example, if N=8 this function should return [2,3,5,7]
the given code:
import numpy as np
def prime_sieve(N):
nums = np.arange(2, N+2, 1)
mask = []
for n in nums:
mask.append(True)
for n in nums:
for i in np.arange(2*n-2, N, n):
mask[i] = False
return nums, np.array(mask)
numbers, mask = prime_sieve(8)
print(numbers)
print(mask)
[2 3 4 5 6 7 8 9]
[ True True False True False True False False]
My Code:
import numpy as np
def primes_list(N):
numbers, mask = prime_sieve(N)
primes = []
for n in numbers:
if mask[n] == "true":
primes.append(numbers[n])
return primes
print(primes_list(8))
but this gives an error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-60-4ea4d2f36734> in <module>
----> 2 print(primes_list(8))
<ipython-input-59-a5080837c5c8> in primes_list(N)
6 primes = []
7 for n in numbers:
----> 8 if mask[n] == "true":
9 primes.append(numbers[n])
10 return primes
IndexError: index 8 is out of bounds for axis 0 with size 8
python numpy index-error
add a comment |
The problem requires me to write a function that compiles a list of prime numbers from the output of a given function that gives 2 lists, one list of numbers from 2 to an arbitrary number and a second list that matches the numbers in the first list with "true" or "False" values depending on whether the numbers in the first list are prime or not.
I have no idea whether my code is just fundamentally wrong to answer the question, or if I am on the right track and just made an error...
Any help would be immensely appreciated.
The question:
Write a function (called primes_list) that takes a single input N. This function must make use of the prime_sieve function to compute and return an array (or list) of only prime numbers less than or equal to N+1.
For example, if N=8 this function should return [2,3,5,7]
the given code:
import numpy as np
def prime_sieve(N):
nums = np.arange(2, N+2, 1)
mask = []
for n in nums:
mask.append(True)
for n in nums:
for i in np.arange(2*n-2, N, n):
mask[i] = False
return nums, np.array(mask)
numbers, mask = prime_sieve(8)
print(numbers)
print(mask)
[2 3 4 5 6 7 8 9]
[ True True False True False True False False]
My Code:
import numpy as np
def primes_list(N):
numbers, mask = prime_sieve(N)
primes = []
for n in numbers:
if mask[n] == "true":
primes.append(numbers[n])
return primes
print(primes_list(8))
but this gives an error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-60-4ea4d2f36734> in <module>
----> 2 print(primes_list(8))
<ipython-input-59-a5080837c5c8> in primes_list(N)
6 primes = []
7 for n in numbers:
----> 8 if mask[n] == "true":
9 primes.append(numbers[n])
10 return primes
IndexError: index 8 is out of bounds for axis 0 with size 8
python numpy index-error
add a comment |
The problem requires me to write a function that compiles a list of prime numbers from the output of a given function that gives 2 lists, one list of numbers from 2 to an arbitrary number and a second list that matches the numbers in the first list with "true" or "False" values depending on whether the numbers in the first list are prime or not.
I have no idea whether my code is just fundamentally wrong to answer the question, or if I am on the right track and just made an error...
Any help would be immensely appreciated.
The question:
Write a function (called primes_list) that takes a single input N. This function must make use of the prime_sieve function to compute and return an array (or list) of only prime numbers less than or equal to N+1.
For example, if N=8 this function should return [2,3,5,7]
the given code:
import numpy as np
def prime_sieve(N):
nums = np.arange(2, N+2, 1)
mask = []
for n in nums:
mask.append(True)
for n in nums:
for i in np.arange(2*n-2, N, n):
mask[i] = False
return nums, np.array(mask)
numbers, mask = prime_sieve(8)
print(numbers)
print(mask)
[2 3 4 5 6 7 8 9]
[ True True False True False True False False]
My Code:
import numpy as np
def primes_list(N):
numbers, mask = prime_sieve(N)
primes = []
for n in numbers:
if mask[n] == "true":
primes.append(numbers[n])
return primes
print(primes_list(8))
but this gives an error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-60-4ea4d2f36734> in <module>
----> 2 print(primes_list(8))
<ipython-input-59-a5080837c5c8> in primes_list(N)
6 primes = []
7 for n in numbers:
----> 8 if mask[n] == "true":
9 primes.append(numbers[n])
10 return primes
IndexError: index 8 is out of bounds for axis 0 with size 8
python numpy index-error
The problem requires me to write a function that compiles a list of prime numbers from the output of a given function that gives 2 lists, one list of numbers from 2 to an arbitrary number and a second list that matches the numbers in the first list with "true" or "False" values depending on whether the numbers in the first list are prime or not.
I have no idea whether my code is just fundamentally wrong to answer the question, or if I am on the right track and just made an error...
Any help would be immensely appreciated.
The question:
Write a function (called primes_list) that takes a single input N. This function must make use of the prime_sieve function to compute and return an array (or list) of only prime numbers less than or equal to N+1.
For example, if N=8 this function should return [2,3,5,7]
the given code:
import numpy as np
def prime_sieve(N):
nums = np.arange(2, N+2, 1)
mask = []
for n in nums:
mask.append(True)
for n in nums:
for i in np.arange(2*n-2, N, n):
mask[i] = False
return nums, np.array(mask)
numbers, mask = prime_sieve(8)
print(numbers)
print(mask)
[2 3 4 5 6 7 8 9]
[ True True False True False True False False]
My Code:
import numpy as np
def primes_list(N):
numbers, mask = prime_sieve(N)
primes = []
for n in numbers:
if mask[n] == "true":
primes.append(numbers[n])
return primes
print(primes_list(8))
but this gives an error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-60-4ea4d2f36734> in <module>
----> 2 print(primes_list(8))
<ipython-input-59-a5080837c5c8> in primes_list(N)
6 primes = []
7 for n in numbers:
----> 8 if mask[n] == "true":
9 primes.append(numbers[n])
10 return primes
IndexError: index 8 is out of bounds for axis 0 with size 8
python numpy index-error
python numpy index-error
edited Mar 8 at 10:13
Andras Deak
21.1k64375
21.1k64375
asked Mar 8 at 10:06
DJBlomDJBlom
82
82
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your n
, that you use to slice your list mask
is a list of numbers that are not suitable for index (since it will always contain N, N+1, while last index of mask
is N-1).
Also, the second list mask
contains Bool
not str
, so your comparison of mask[n] == 'true'
will always return False
.
With above points in mind, your primes_list
can be:
def primes_list(N):
numbers, mask = prime_sieve(N)
primes = []
for i, n in enumerate(numbers): # <<< added enumerate
if mask[i]: # <<< removed unnecessary comparison
primes.append(n) # <<< append n directly
return primes
which returns:
[2, 3, 5, 7]
as it should be.
Thank you so much, this works perfectly!
– DJBlom
Mar 8 at 10:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55060891%2fpython-indexerror-index-8-is-out-of-bounds-for-axis-0-with-size-8%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your n
, that you use to slice your list mask
is a list of numbers that are not suitable for index (since it will always contain N, N+1, while last index of mask
is N-1).
Also, the second list mask
contains Bool
not str
, so your comparison of mask[n] == 'true'
will always return False
.
With above points in mind, your primes_list
can be:
def primes_list(N):
numbers, mask = prime_sieve(N)
primes = []
for i, n in enumerate(numbers): # <<< added enumerate
if mask[i]: # <<< removed unnecessary comparison
primes.append(n) # <<< append n directly
return primes
which returns:
[2, 3, 5, 7]
as it should be.
Thank you so much, this works perfectly!
– DJBlom
Mar 8 at 10:23
add a comment |
Your n
, that you use to slice your list mask
is a list of numbers that are not suitable for index (since it will always contain N, N+1, while last index of mask
is N-1).
Also, the second list mask
contains Bool
not str
, so your comparison of mask[n] == 'true'
will always return False
.
With above points in mind, your primes_list
can be:
def primes_list(N):
numbers, mask = prime_sieve(N)
primes = []
for i, n in enumerate(numbers): # <<< added enumerate
if mask[i]: # <<< removed unnecessary comparison
primes.append(n) # <<< append n directly
return primes
which returns:
[2, 3, 5, 7]
as it should be.
Thank you so much, this works perfectly!
– DJBlom
Mar 8 at 10:23
add a comment |
Your n
, that you use to slice your list mask
is a list of numbers that are not suitable for index (since it will always contain N, N+1, while last index of mask
is N-1).
Also, the second list mask
contains Bool
not str
, so your comparison of mask[n] == 'true'
will always return False
.
With above points in mind, your primes_list
can be:
def primes_list(N):
numbers, mask = prime_sieve(N)
primes = []
for i, n in enumerate(numbers): # <<< added enumerate
if mask[i]: # <<< removed unnecessary comparison
primes.append(n) # <<< append n directly
return primes
which returns:
[2, 3, 5, 7]
as it should be.
Your n
, that you use to slice your list mask
is a list of numbers that are not suitable for index (since it will always contain N, N+1, while last index of mask
is N-1).
Also, the second list mask
contains Bool
not str
, so your comparison of mask[n] == 'true'
will always return False
.
With above points in mind, your primes_list
can be:
def primes_list(N):
numbers, mask = prime_sieve(N)
primes = []
for i, n in enumerate(numbers): # <<< added enumerate
if mask[i]: # <<< removed unnecessary comparison
primes.append(n) # <<< append n directly
return primes
which returns:
[2, 3, 5, 7]
as it should be.
answered Mar 8 at 10:17
ChrisChris
3,341422
3,341422
Thank you so much, this works perfectly!
– DJBlom
Mar 8 at 10:23
add a comment |
Thank you so much, this works perfectly!
– DJBlom
Mar 8 at 10:23
Thank you so much, this works perfectly!
– DJBlom
Mar 8 at 10:23
Thank you so much, this works perfectly!
– DJBlom
Mar 8 at 10:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55060891%2fpython-indexerror-index-8-is-out-of-bounds-for-axis-0-with-size-8%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown