Bird
0
0

You want to create a NumPy array from a list of strings representing numbers, like ['1', '2', '3'], but want the array to have integer type. Which code achieves this?

hard📝 Application Q9 of 15
NumPy - Creating Arrays
You want to create a NumPy array from a list of strings representing numbers, like ['1', '2', '3'], but want the array to have integer type. Which code achieves this?
Anp.array(['1', '2', '3'], dtype=object)
Bnp.array(['1', '2', '3']).astype(str)
Cnp.array(['1', '2', '3'], dtype=int)
Dnp.array(['1', '2', '3'], dtype=float)
Step-by-Step Solution
Solution:
  1. Step 1: Convert string numbers to integers

    Using dtype=int converts string elements to integers during array creation.
  2. Step 2: Confirm correct dtype usage

    np.array(['1', '2', '3'], dtype=int) correctly specifies dtype=int to get integer array.
  3. Final Answer:

    np.array(['1', '2', '3'], dtype=int) -> Option C
  4. Quick Check:

    dtype=int converts string numbers to integers [OK]
Quick Trick: Use dtype=int to convert string numbers to integers [OK]
Common Mistakes:
  • Using astype(str) instead of int
  • Using dtype=object keeps strings
  • Using dtype=float when integers needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes