Python Forum
Numpy error while filling up matrix with Characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Numpy error while filling up matrix with Characters
#1
I'm new to numpy. I've a string of characters. I'm truncating it one character at a time till only one character is left.
Then I'm trying to fill up a numpy array with these individual substrings in a column wise fashion.

import numpy as np
mystr = 'ASDFL'
bs = [mystr[:x+1] for x in range( len( mystr)) ]
bs = sorted(bs, key=len, reverse=True )
fm = np.empty(shape=( len(mystr), len(bs) ), dtype='str')
for j in range(len(bs)):
    fm[:, j] = list(bs[j])
However I'm getting the following error:
"ValueError: cannot copy sequence with size 4 to array axis with dimension 5"

Can anybody help me solve this problem? Thanks
Reply
#2
Why are you using numpy? Do you need a 5x5 array for some reason?

Is this what you are looking for?
import numpy as np
mystr = 'ASDFL'
n = len(mystr)
bs = [mystr[:x] for x in range(n, 0, -1)]
fs = np.empty((n, n), dtype=str)
for index, values in enumerate(bs):
    fs[index,:len(values)] = list(values)
print(fs)
Output:
[['A' 'S' 'D' 'F' 'L'] ['A' 'S' 'D' 'F' ''] ['A' 'S' 'D' '' ''] ['A' 'S' '' '' ''] ['A' '' '' '' '']]
Reply
#3
Hello deanhystad,
Thank you very much. Actually this is a part of the problem. I've to do this for many string like ASDFL, QWERTY etc and sub-strings can start from any place inside the string , hence it would be n X m matrix. If possible can you point out where I was going wrong?
was it in the declaration of the array ?
fm = np.empty(shape=( len(mystr), len(bs) ), dtype='str')
Or
fm[:, j] = list(bs[j]
or somewhere else?

Thanks again
Reply
#4
You cannot use ":" by itself unless len(bs[j]) == len(fm[:,j]. You could do like I did and copy bs[j] to a slice in f[], or you can pad everything in bs[] to be the same length, or you can copy characters over one at a time. Your choice.

You didn't answer the question about why you are using numpy. This would be simple if you could have a list of lists.
Reply
#5
Thanks. Later on I want to make a plot with colors depending on the letters, thats why I'm trying to use numpy
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas : problem with conditional filling of a column Xigris 2 592 Jul-22-2023, 11:44 AM
Last Post: Xigris
  Check if two matrix are equal and of not add the matrix to the list quest 3 778 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Create a 2-channel numpy file from two cvs file containing a 9x9 matrix silvialecc 1 1,616 Oct-26-2021, 07:59 AM
Last Post: Gribouillis
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,301 May-03-2021, 06:30 AM
Last Post: Gribouillis
  NumPy Matrix help ntailor97 1 1,786 Apr-08-2021, 03:23 AM
Last Post: ntailor97
Bug Error while importing numpy Erfan 3 3,219 Nov-28-2020, 07:49 AM
Last Post: bowlofred
  Matrix Operations Without Numpy or Incorporating Python into Webpage ebryski 1 2,846 Nov-26-2020, 12:50 PM
Last Post: jefsummers
  [SOLVED] Filling multidict from CSV file? Winfried 3 1,908 Oct-24-2020, 08:26 PM
Last Post: Winfried
  filling and printing numpy arrays of str pjfarley3 4 3,205 Jun-07-2020, 09:09 PM
Last Post: pjfarley3
  Odd numpy error with subtraction DreamingInsanity 5 2,662 Jun-01-2020, 02:49 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

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