Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I assign a second value in cel of a 2D array?

I am trying to find a method to include a second variable in a 2D array.
Additionally I only want to assign a second variable to cells that allready have one.
for instance, I start with the array:

seq = [[1, 0, 0, 0],[1, 2, 3, 4],[2, 0, 0, 0]]

and I want to assign a second integer to the values that allready have one, making this:

seq = [[(1, a), 0, 0, 0],[(1, b), (2, c), (3, d), (4, e)], [(2, f), 0, 0, 0]]

in which I ideally want a loop in which i can select the particular values with a rule. I don’t know for sure I can keep the 0’s in the particular positions without assigning a second value to them, or if the array needs to be converted to a list.
I’m quite new to python and normally work with Matlab.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try:

values = list('abcdef')
result = list()
for row in seq:
    result.append([(n, values.pop(0)) if n!=0 else 0 for n in row])

>>> result
[[(1, 'a'), 0, 0, 0],
 [(1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')],
 [(2, 'f'), 0, 0, 0]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading