Home > Blockchain >  adding a zero at first element in a numpy array or a way to create an array keeping the first positi
adding a zero at first element in a numpy array or a way to create an array keeping the first positi

Time:11-10

I got this function to looking for some specific characters in a string and if it is found it add a value at that position in the array, ex:

def skew_array(text):
    import numpy as np
    skew = np.zeros(len(text))
    for i in range(0, len(text)):
        if text[i] == 'G':
            skew[i]  = 1
        elif text[i] == 'C':
            skew[i] -= 1
        else:
            skew[i] = 0
    return np.insert(skew, 0, 0)

>> skew_array('gacaattagcaa'.upper())
array([ 0.,  1.,  0., -1.,  0.,  0.,  0.,  0.,  0.,  1., -1.,  0.,  0.])

I am not sure if it is the right way to do it (inserting at the end) or if there are a way just to keep the first position as zero when creating the array.

I appreciate any tip! Thank you by your time.

PS - I was using a list, but it is very bad for memory in bigger strings!

CodePudding user response:

i think that functions that change the flat size of arrays are pretty slow, so i'd create skew larger from the start. and you can use a dict to avoid an elif soup

import numpy as np
def skew_array(text, dict_):
    skew = np.zeros(len(text) 1)
    for i, char in enumerate(text, start=1):
        skew[i] = dict_.get(char, 0)
    return skew

skew_dict={"G":1, "C":-1}
code='gacaattagcaa'.upper()
print(skew_array(code, skew_dict))
  • Related