开发者

Lining up DNA base pairs in an array

开发者 https://www.devze.com 2023-04-11 16:52 出处:网络
Trying to import a series of base pairs into an array. I want it in the form [\'AA\',\'AT\',\'AG\',开发者_JAVA技巧\'AC\'...]

Trying to import a series of base pairs into an array.

I want it in the form ['AA','AT','AG',开发者_JAVA技巧'AC'...]

Here's my code:

paths = [str(x[4:7]) for x in mm_start]

paths
['A A', 'A T', 'A G', 'A C', 'T A', 'T T', 'T G', 'T C', 'G A', 'G T', 'G G', 'G C', 'C A', 'C T', 'C G', 'C C']      

I get spaces in between the letters! This strip command isn't helping either.

paths = str(paths).replace(" ","")

paths
"['AA','AT','AG','AC','TA','TT','TG','TC','GA','GT','GG','GC','CA','CT','CG','CC']"

Now I get a (") at the beginning and end of this array.

Any ideas very welcome!

The text file has the base pairs laid out

1 2 3 4 A A
1 2 3 1 A T
...

Thanks


You're converting the list into a string. You really want to say

paths = [pair.replace(" ", "") for pair in paths] 

That iterates over each pair in your list of strings and removes spaces rather than converting the entire list to a string.


paths = str(paths).replace(" ","")

Well of course it doesn't work, because you converted the list to a string (and you didn't convert it back). What you need to do is apply the replace to each element of the list, not to the string expression of the list.

paths = [str(x[4:7]).replace(" ","") for x in mm_start]

(that's only one way of many).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号