Python Forum
Single line intermitant inputs to an array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Single line intermitant inputs to an array
#1
I am having trouble trying to input values into an array in which I want to change only some of them. (the array shown is a very much abbreviated version of over 100 values for purposes of illustration)
I can make the changes conventionally in a series of vertical adjustments, but I want to save space by inputting he changes in a single line.
I am still very new to array structures, so would appreciate some help.

import numpy as np

   
results = np.array([['A','B','C','D','E','F','G'],
                    [24,41,67,'Twomy',3,11,'Plc',]])
   
print('Original Results:')
print(results)
print()
print()
results[1,0] = 16
results[1,3] = 22
results[1,4] = 18
results[1,5] = 'Smalley'

print('Vertically modified  Results:')
print(results)
print()
print()

#two points: 1. how to make 'Small' print out in full  as 'Smalley'


#and point 2: is it possible to create the changes in a single line such that B,C and G values are unchanged:

#I have tried a few variations, but none work:

#results[1,0] == 17, results[1,3] == 32, results[1,4] == 'Smalley', results[1,5] == 10
#results[[1,0] == 17, results[1,3] == 32, results[1,4] == 'Smalley', results[1,5] == 10]
#results[[1,0] == '17', [1,3] == '32', [1,4] == 'Smalley', [1,5] == '10']
#results[1,0] == 17, results[1,3] == 32, results[1,4] == 'Smalley', results[1,5] == 10

#np.array([1,0] == 17, results[1,3] == 32, results[1,4] == 'Smalley', results[1,5] == 10)
#np.array([1,0] == '17', [1,3] == '32', [1,4] == 'Smalley', [1,5] == '10')
#np.array([1,0] == '17', [1,3] == '32', [1,4] == 'Smalley', [1,5] == '10')
#np.array([1,0] == '17', [1,3] == '32', [1,4] == 'Smalley', [1,5] == '10')
print('Atempts to horizontally modify results does not work:')
print(results)
print()
 
print('Which is the same as for Vertically modified  Results')
As you will see, I have tried lots of simple options, but none work.
Any advice appreciated!


Astrikor
Reply
#2
I am not a numpy guru, but AFAIK, numpy array is the closest thing to the standard C array containing uniform values - thus it defines a type for its elements to accommodate the largest value entered. Size uniformity is important for processing speed.

In you example, each array element contains up to 5 unicode characters

Output:
In [13]: results = np.array([['A','B','C','D','E','F','G'], ...: [24,41,67,'Twomy',3,11,'Plc',]]) ...: In [14]: results Out[14]: array([['A', 'B', 'C', 'D', 'E', 'F', 'G'], ['24', '41', '67', 'Twomy', '3', '11', 'Plc']], dtype='<U5') In [15]: results[1,5] = 'Smalley' In [16]: results[1,5] Out[16]: 'Small'
As you can see,
  1. all the original values are implicitly converted to the common type of unicode string of up to 5 characters
  2. The newly entered value is trimmed to fit into a predefined size.

In order to insert a longer string into the array - any replaced value will be implicitly converted
Output:
In [20]: results[1, 4] = 1014435435435435465456465465456 In [21]: results[1, 4] Out[21]: '1014435435'
increase the size of individual element

Output:
In [17]: results = results.astype('<U10') In [18]: results[1,5] = 'Smalley' In [19]: results Out[19]: array([['A', 'B', 'C', 'D', 'E', 'F', 'G'], ['24', '41', '67', 'Twomy', '3', 'Smalley', 'Plc']], dtype='<U10')
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Many thanks Volcano63.

That's sorted the string length.

How about my point 2: inputting a one-line dataset to the array where some values remain unchanged (skipped over?)?

Is it possible?

Thanks
Astrikor
Reply
#4
(Sep-04-2018, 06:35 PM)Astrikor Wrote: How about my point 2: inputting a one-line dataset to the array where some values remain unchanged (skipped over?)?

You may use slicing - like with lists,
Output:
In [38]: results[1, ::2] = list('KLMN') In [39]: results Out[39]: array([['A', 'B', 'C', 'D', 'E', 'F', 'G'], ['K', '41', 'L', 'Twomy', 'M', 'Smalley', 'N']], dtype='<U10')
but otherwise - I doubt that

PS I stay corrected - you can (see my signature Cool )

Output:
In [45]: results[0, [1, 3, 4, 6]] = list('KLMN') In [46]: results Out[46]: array([['A', 'K', 'C', 'L', 'M', 'F', 'N'], ['K', 'K', 'L', 'L', 'M', 'Smalley', 'N']], dtype='<U10')
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
Thanks again Volcano63,


My input array was

results = np.array([['A','B','C','D','E','F','G'],
[24,41,67,'Twomy',3,11,'Plc',]])

As you know, I am looking to create a single line of code to give an updated array as follows (where not all values are changed):

results = np.array([['A','B','C','D','E','F','G'],
[16,41,67,22,18,'Smalley','Plc',]])

I may have misunderstood your suggestions when coding it, but it gives an array length error.

Try running the following:

import numpy as np
import sys
    
results = np.array([['A','B','C','D','E','F','G'],
                    [24,41,67,'Twomy',3,11,'Plc',]])
print(results)
print()
results[0, [0,3,4,6]] = list('16,22,18,Smalley')
print(results)
It does not like the apparently different array lengths.

What am I doing wrong ??

Astrikor
Reply
#6
running the code
:
import numpy as np
import sys
     
results = np.array([['A','B','C','D','E','F','G'],
                    [24,41,67,'Twomy',3,11,'Plc',]])
print(results)
print()
results[0, [0,3,4,6]] = list('16,22,18,Smalley')
print(results)
gives as follows:
Output:
[['A' 'B' 'C' 'D' 'E' 'F' 'G'] ['24' '41' '67' 'Twomy' '3' '11' 'Plc']] Traceback (most recent call last): File "C:\Users\MY\Desktop\forum one line array.py", line 8, in <module> results[0, [0,3,4,6]] = list('16,22,18,Smalley') ValueError: shape mismatch: value array of shape (16,) could not be broadcast to indexing result of shape (4,)
Can someone please help find a one-line solution which gives the output:

Output:
results = np.array([['A','B','C','D','E','F','G'], [16,41,67,22,18,'Smalley','Plc',]])
Many thanks

Astrikor
Reply
#7
In the absence of any replies to my last request for help I got into lateral thinking and have found my own relatively simple solution to my problem.

I present it to benefit others looking to solve a similar problem:

My original array :

import numpy as np
     
results = np.array([['A','B','C','D','E','F','G'],
                    [24,41,67,'Twomy',3,11,'Plc',]], dtype='<U10')
print(results)
print()
Add a new serial line to change only the values of the array cells [0, [0,3,4,6]] to ('16,22,18,Smalley') while leaving the other cell values unchanged (This was the tricky bit to which I was partly inspired by Volcano63) :

results[1, 0]=16 ; results[1, 3]=22 ; results[1, 4]=18 ; results[1, 6]='Smalley'
print(results)
Which when appended to the array code and run provides exactly the one-liner result I was looking for (see my previous post - Yesterday, 02:11 PM)

Thanks to all those who viewed my thread and especially to those who replied, Volcano63 in particular.
Of course it could be further simplified by reducing "results" to just a single character.
This will take up much less coding space and simplify readability for maintenance.

e.g.

import numpy as np
r = np.array([['A','B','C','D','E','F','G'],
                    [24,41,67,'Twomy',3,11,'Plc',]], dtype='<U10')
r[1, 0]=16 ; r[1, 3]=22 ; r[1, 4]=18 ; r[1, 6]='Smalley'
print(r)
With blessings to all

Astikor

Never give up when experts are defeated - think laterally.
Reply
#8
(Sep-06-2018, 12:48 AM)Astrikor Wrote:
Output:
[['A' 'B' 'C' 'D' 'E' 'F' 'G'] ['24' '41' '67' 'Twomy' '3' '11' 'Plc']] Traceback (most recent call last): File "C:\Users\MY\Desktop\forum one line array.py", line 8, in <module> results[0, [0,3,4,6]] = list('16,22,18,Smalley') ValueError: shape mismatch: value array of shape (16,) could not be broadcast to indexing result of shape (4,)
Can someone please help find a one-line solution which gives the output:

Output:
results = np.array([['A','B','C','D','E','F','G'], [16,41,67,22,18,'Smalley','Plc',]])
Many thanks

Astrikor

Simple experiment would have shown what is wrong with that code

Output:
In [1]: list('16,22,18,Smalley') Out[1]: ['1', '6', ',', '2', '2', ',', '1', '8', ',', 'S', 'm', 'a', 'l', 'l', 'e', 'y']
When I used the example of list('KLMN') - I was just saving on typing to fill four elements.

Numpy is very non-trivial package; too many rush into it without learning "vanilla" Python
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#9
This is the thing though Volcano63:

You know what you are doing and I am a 76 yr old Python novice without the time (literally) to achieve a decent understanding, so while I do understand the point you have just made, it would not have helped me to find my solution.

I am very grateful for your advice, without which I would have had to abandon my Python project and try learning Mandarin instead (it's all to do with brain training to ward off the dreaded dementia).

But I have to say, I much prefer having a go at Python simply because my queries are usually easily resolved by you guys with the knowledge.

It's a bit like trying self-diagnosis when you're unwell - you can simply message a GP for the professional solution.

Thanks again
With blessings
Astrikor
Reply
#10
(Sep-10-2018, 03:01 PM)Astrikor Wrote: This is the thing though Volcano63:

You know what you are doing and I am a 76 yr old Python novice without the time (literally) to achieve a decent understanding, so while I do understand the point you have just made, it would not have helped me to find my solution.

I am very grateful for your advice, without which I would have had to abandon my Python project and try learning Mandarin instead (it's all to do with brain training to ward off the dreaded dementia).

Dear Astrikor,

I was not sure about your circumstances - and I am - alas! - too used to users who refuse to listen Doh (and sometimes even complain about good advice Wall ). My point was that going into numpy ( a complex package which I myself does not know) without learning vanilla Python is not effective.

As for your question - if you provide 4 indices - as in my example - you need to provide a list - or tuple - of 4 elements.

results[0, [0,3,4,6]] = 16, 22,18, 'Smalley'
or

results[0, [0,3,4,6]] = '16,22,18,Smalley'.split(',')
I just took a shortcut - applying list function to a string yields a list of strings with individual characters as elements.

The gist of my advice - use an interactive Python shell, e.g. iPython or Jupyter - any of which may be installed as a Python package (below) to practice. This helps to learn and also test ideas "on the fly" (I do it all the time). It is easier than fixing and re-running the code. In either shell, you may execute scripts commands one-by-one

To install -
pip install jupyter ipython

Jupyter is also available online

Good luck and enjoy it!
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NumPy array; selecting the a single column econmajor 0 3,060 Oct-24-2017, 04:25 PM
Last Post: econmajor

Forum Jump:

User Panel Messages

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