1

I have to change a string dictionary which kind of looks like this.

result = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
          'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
          'low-L': ' ', 'low-M': ' ', 'low-R': ' '}

Now, i have to convert this into a 3x3 array.

So, far i have reached a code which do almost what i intended but still its messy.

Here is the code.

import numpy as np
names = ['left','middle']
formats = ['S3','S3']
dtype = dict(names = names, formats=formats)
array = np.fromiter(result.items(), dtype=dtype, count=len(result))
arr = np.reshape(array, (3,3))
print(repr(arr))
print (arr[0][1]) 

and the output which generated.

array('lo[[(b'top', b' '), (b'top', b' '), (b'top', b' ')],
       [(b'mid', b' '), (b'mid', b' '), (b'mid', b' ')],
       [(b'low', b' '), (b'low', b' '), (bw', b' ')]],
      dtype=[('left', 'S3'), ('middle', 'S3')])
(b'top', b' ')

note print (arr[0][1]) generates (b'top', b' ') which is not expected.

There might be something wrong with this code, any suggestions.

5
  • What's your expected output? Commented May 3, 2018 at 10:29
  • my expected should be like, i can use the dictionary key values using numpy array indexing like arr[0][0] should return "top-L" and [1][1] = ' ', also the output it kind of wrong the keys are getting wrong in the output. Commented May 3, 2018 at 10:33
  • Your input data seems a bit strange. Are all the values really strings containing a single space? Commented May 3, 2018 at 10:34
  • You're ever that dictionaries don't preserve the order (all the versions before Python-3.7), right? What's your Python version? Commented May 3, 2018 at 10:35
  • @PM2Ring the value of each dict keys will be updated at run time, i order to compare these values and keys, i thought i could use arrays, the computation will be really that way. Commented May 3, 2018 at 10:47

1 Answer 1

1

First thing that you need to consider is that dictionaries in lower versions of Python-3.7 do not preserve the order of their items. Therefore if you're using one of these versions you must not expect to get a result with your intended order.

By that being said, generally, a very optimized and handy way to preserve string items in a Numpy array is to use numpy.chararray() objects. As it's also mentioned in documentation chararrays provides a convenient view on arrays of string and unicode values.

Here is how you can get your expected array using chararray :

>>> items = list(result.items())
# By passing `itemsize=5` to the chararray function you're specifying
# the length of each array item
>>> arr = np.chararray((len(items), 2), itemsize=5)
>>> arr[:] = items
>>> arr
chararray([[b'top-L', ''],
           [b'top-M', ''],
           [b'top-R', ''],
           [b'mid-L', ''],
           [b'mid-M', ''],
           [b'mid-R', ''],
           [b'low-L', ''],
           [b'low-M', ''],
           [b'low-R', '']], dtype='|S5')
>>> arr[0]
chararray([b'top-L', ''], dtype='|S5')
>>> arr[0][1]
''

This code has been ran in a Python-3.7 interactive shell environment and that's why the array's order is the same as the dictionary's items order.

Sign up to request clarification or add additional context in comments.

3 Comments

can it be converted in 3*3 array? Easy to form a loop.
@user3814582 What kind of 3*3 array? what would be third item?
i am considering [b'top-L', ' '] as one so i have 9 elements so it can be arranged, but then i might not able to access "top-L" and " " values individually, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.