parsing a string to an array

Posted by Shork on July 1, 2009, 4:18 p.m.

I am writing a program in python for a genetics professor I had for class. It takes the coordinates of a gene on DNA, and draws an arrow showing the position and direction of the gene on the genome. I thought converting the numbers to an image would be the hard part because it needed scaling and wrapping from one line to the next, but I was wrong.

When I first wrote the program, I simply hardcoded the data used to draw the image. It worked well enough for me, but if I want to make it usable for others, I will need a GUI that takes the numbers from the user and formats it for the image. This is harder. I am trying to convert a string to an array. The string follows this format:

"1-2, 3-4

5-6, 7-8"

and the array should have this format: [[[1, 2], [3,4]], [[5, 6], [7, 8]]]

Of course the numbers could be any number. I have been trying to get this to work for weeks, and I can't get it to parse right. I haven't found any built-in functions for this sort of thing, and I have tried several techniques, such as using nested for loops that search for digits, commas, or hyphens, and tries to build a number and add it to an array, but it just does not work right…

Has anyone done anything similar, or have any suggestions for making it work?

Here is what the final output looks like, in case you're interested. Sorry about screen stretch…

Comments

OL 15 years, 5 months ago

Use a string split function -

http://www.java2s.com/Code/Python/String/String-Split.htm

First split it all by "," then split each of those chunks by "-". Then loop through them all and put them into the correct array format?

sirxemic 15 years, 5 months ago

Recursion is your friend.

beam 15 years, 5 months ago

coords = "1-2, 3-4\n5-6, 7-8"

pairs = map(lambda x: x.strip(), coords.split("\n"))

result = [map(lambda x: x.strip().split("-"), pair.split(",")) for pair in pairs]

result is [[['1', '2'], ['3', '4']], [['5', '6'], ['7', '8']]]

there you go bro, fuck recursion. list comprehensions are the One True Way

Shork 15 years, 5 months ago

It works now, recursion IS my friend after all….

Anyway, now I have to figure out the GUI. Never having done one in python, I am sure I will have fun.

[deleted user] 15 years, 5 months ago

char[] delim= { thing to split with (char) };

newString = oldString.Split(delim);

beam 15 years, 5 months ago

Quote:

It works now, recursion IS my friend after all….

did you really make a dumb, non-idiomatic recursive function to do that, when you could just use a simple two-line list comprehension?

get your dumb hands out of python you're going to ruin everything

sirxemic 15 years, 5 months ago

I knew I should have shut my mouth when I don't know what python is capable of. :(

s 15 years, 5 months ago

Code golf?

>>> f=lambda x:[[[int(x) for x in x.split("-")] for x in x.split(", ")] for x in x.split("\n")]

>>> f("1-2, 3-4\n5-6, 7-8")

[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

So there's your one liner version

Shork 15 years, 5 months ago

Maybe I used a few more lines than need be, but the program doesn't use that many resources to begin with, so I figure it's ok. If I were doing it 100 times a cycle it would matter. But this does the string splitting once, so it's ok.

and besides, I love nested for loops.

[deleted user] 15 years, 5 months ago

change your avartar the asymmetry and the stupid paper artifacts are pissing me off. at the very least touch it up in photoshop its always there at every post why why why