Bird
0
0

You have a list of mixed Unicode and byte strings: ['hello', b'world', 'test']. How can you create a NumPy array that stores all as Unicode strings?

hard📝 Application Q8 of 15
NumPy - Array Data Types
You have a list of mixed Unicode and byte strings: ['hello', b'world', 'test']. How can you create a NumPy array that stores all as Unicode strings?
Anp.array(['hello', b'world', 'test'], dtype=object)
Bnp.array(['hello', b'world', 'test'], dtype='S')
Cnp.array(['hello', b'world', 'test'], dtype='U')
Dnp.array(['hello', b'world', 'test'])
Step-by-Step Solution
Solution:
  1. Step 1: Understand mixed string types

    List contains Python Unicode strings and byte strings.
  2. Step 2: Convert all to Unicode strings

    Using dtype='U' forces conversion of all elements to Unicode strings.
  3. Final Answer:

    np.array(['hello', b'world', 'test'], dtype='U') -> Option C
  4. Quick Check:

    dtype='U' converts bytes to Unicode strings [OK]
Quick Trick: Use dtype='U' to unify mixed string types as Unicode [OK]
Common Mistakes:
  • Using dtype='S' which keeps byte strings
  • Using object dtype which keeps mixed types
  • Omitting dtype and getting object array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes