w3resource

NumPy: Create an array using generator function that generates 15 integers

NumPy: Array Object Exercise-184 with Solution

Write a NumPy program to create an array using generator function that generates 15 integers.

Pictorial Presentation:

Python NumPy: Create an array using generator function that generates 15 integers

Sample Solution:

Python Code:

import numpy as np
def generate():
   for n in range(15):
       yield n
nums = np.fromiter(generate(),dtype=float,count=-1)
print("New array:")
print(nums)

Sample Output:

New array:
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14.]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Numpy program to test whether a given 2D array has null columns or not.
Next: Write a Numpy program to create a new vector with 2 consecutive 0 between two values of a given vector.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}