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













1















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









share|improve this question




























    1















    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









    share|improve this question


























      1












      1








      1








      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









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 10:13









      Andras Deak

      21.1k64375




      21.1k64375










      asked Mar 8 at 10:06









      DJBlomDJBlom

      82




      82






















          1 Answer
          1






          active

          oldest

          votes


















          1














          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.






          share|improve this answer























          • Thank you so much, this works perfectly!

            – DJBlom
            Mar 8 at 10:23










          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
          );



          );













          draft saved

          draft discarded


















          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









          1














          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.






          share|improve this answer























          • Thank you so much, this works perfectly!

            – DJBlom
            Mar 8 at 10:23















          1














          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.






          share|improve this answer























          • Thank you so much, this works perfectly!

            – DJBlom
            Mar 8 at 10:23













          1












          1








          1







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 10:17









          ChrisChris

          3,341422




          3,341422












          • 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





          Thank you so much, this works perfectly!

          – DJBlom
          Mar 8 at 10:23



















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived