Bird
0
0

You have a list of strings representing numbers: ['1', '2', '3']. How do you create a NumPy array of integers from this list specifying dtype?

hard📝 Application Q8 of 15
NumPy - Array Data Types
You have a list of strings representing numbers: ['1', '2', '3']. How do you create a NumPy array of integers from this list specifying dtype?
Anp.array(['1', '2', '3'], dtype='int')
Bnp.array(['1', '2', '3'], dtype='str')
Cnp.array(['1', '2', '3'], dtype='float')
Dnp.array(['1', '2', '3'])
Step-by-Step Solution
Solution:
  1. Step 1: Convert string numbers to integers

    Specifying dtype='int' converts string elements to integers during array creation.
  2. Step 2: Check other options

    Using dtype='str' keeps strings; dtype='float' converts to floats; no dtype keeps strings.
  3. Final Answer:

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

    Use dtype='int' to convert strings to integers = C [OK]
Quick Trick: dtype='int' converts numeric strings to integers [OK]
Common Mistakes:
  • Not specifying dtype and getting string array
  • Using dtype='str' expecting integers
  • Using dtype='float' instead of int

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes