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 fill an array with 0 (or Nan) using other array as a reference?

I have an array like this:

array(['ENST00000003084', 'ENST00000426809', 'ENST00000454343'], 
['ENST00000003084', 'ENST00000426809', 'ENST00000446805', 'ENST00000454343'], 
['ENST00000003084', 'ENST00000454343'],
['ENST00000003084', 'ENST00000426809', 'ENST00000454343', 'ENST00000600166'])

but I want to all follow the same reference, because they have to maintain this order:

array(['ENST00000003084', 'ENST00000426809', 'ENST00000446805',
       'ENST00000454343', 'ENST00000468795', 'ENST00000600166'])

I don’t want just to complete with zeros in the end, I want to fill with 0 the missing values according to the reference array, so they all end up with 6 elements

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

For example, for the array

['ENST00000003084', 'ENST00000426809', 'ENST00000454343']

the final output should be:

['ENST00000003084', 'ENST00000426809',0, 'ENST00000454343', 0, 0]

>Solution :

this should solve your problem.

ref = np.array(['ENST00000003084', 'ENST00000426809', 'ENST00000446805',
       'ENST00000454343', 'ENST00000468795', 'ENST00000600166'])
a = np.array(['ENST00000003084', 'ENST00000426809', 'ENST00000454343'])
b = [ref[k] if ref[k] in a else 0 for k in range(len(ref))]

The output of this example is

['ENST00000003084', 'ENST00000426809', 0, 'ENST00000454343', 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