Python Forum
filling and printing numpy arrays of str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
filling and printing numpy arrays of str
#1
I am new to python but experienced in several other languages. I do not understand yet why numpy.full does not fill an array of strings with the string value I supply in the code.

Example code and output follows. OS is Win10-64 Pro, Python version is 3.8.3.

#!/usr/bin/env python
import numpy as np

class arstr:
    def __init__(self, asize, aval):
        self.strs = np.full([asize, asize, asize], aval, dtype=str)

Mystrs = arstr(3, " ? ")

for s in Mystrs.strs:
    print (s)

exit()
Output in a cmd.exe window follows.

Output:
C:\Testdir>python atest.py [[' ' ' ' ' '] [' ' ' ' ' '] [' ' ' ' ' ']] [[' ' ' ' ' '] [' ' ' ' ' '] [' ' ' ' ' ']] [[' ' ' ' ' '] [' ' ' ' ' '] [' ' ' ' ' ']]
Why are each of the array elements a single blank character and not the three-character string " ? " that I specified in the invocation of the class?

Thank you in advance for any help you can offer.

Peter
Reply
#2
dtype=strmeans a fixed-size string. And without other information, it defaults to a one-character string. So your info is being truncated to the first character.

You can instead pass a tuple, with the padded size given as the second field.

>>> np.full([3], "ABC", dtype=(str))
array(['A', 'A', 'A'], dtype='<U1')
>>> np.full([3], "ABC", dtype=(str, 3))
array(['ABC', 'ABC', 'ABC'], dtype='<U3')
Reply
#3
Thanks for that clarification. That cures at least a little my ignorance.

Is there a way to populate a numpy.ndarray with strings of arbitrary size?

Peter
Reply
#4
It is possible to use the object type instead. Now instead of storing the data within the array, the array only stores references to external objects.

>>> x=np.full([3], "ABC")
>>> x[1] = "Longerstring"
>>> x
array(['ABC', 'Lon', 'ABC'], dtype='<U3') #note truncation

>>> x=np.full([3], "ABC", dtype=object)
>>> x[1] = "Longerstring"
>>> x
array(['ABC', 'Longerstring', 'ABC'], dtype=object)
Reply
#5
Thank you, that helps me a lot.

I will mark this thread as solved now.

Peter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas : problem with conditional filling of a column Xigris 2 594 Jul-22-2023, 11:44 AM
Last Post: Xigris
  problem adding two numpy arrays djf123 2 2,051 Aug-09-2022, 08:31 PM
Last Post: deanhystad
  how to join by stack multiple types in numpy arrays caro 1 1,110 Jun-20-2022, 05:02 PM
Last Post: deanhystad
  Numpy error while filling up matrix with Characters august 4 1,799 Apr-13-2022, 10:28 PM
Last Post: august
  numpy.dot() result different with classic computation for large-size arrays geekgeek 5 1,831 Jan-25-2022, 09:45 PM
Last Post: Gribouillis
  Two numpy arrays Sandra2312 1 1,777 Jan-18-2021, 06:10 PM
Last Post: paul18fr
  [SOLVED] Filling multidict from CSV file? Winfried 3 1,910 Oct-24-2020, 08:26 PM
Last Post: Winfried
  numpy in1d with two simple arrays claw91 3 2,527 Sep-21-2020, 12:43 PM
Last Post: scidam
  Type coercion with Numpy arrays Mark17 2 2,489 Jul-24-2020, 02:04 AM
Last Post: scidam
  printing class properties from numpy.ndarrays of objects pjfarley3 2 1,909 Jun-08-2020, 05:30 AM
Last Post: pjfarley3

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020