7

"8,5,,1,4,7,,,,7,,1,9,3,6,,,8,6,3,9,,2,5,4,,,,,3,2,,,7,4,1,1,,4,,6,9,,5,,,,5,,,1,,6,3,,,6,5,,,,7,4,,1,7,6,,,,8,,5,,,7,1,,3,9,"

I'm doing a programming challenge where i need to parse this sequence into my sudoku script. Need to get the above sequence into 8,5,0,1,4,7,0,0,0,7,0,1,9,3,6,0,0,8......... I tried re but without success, help is appreciated, thanks.

1
  • Fun question and great opportunity to write really idiomatic Python. Commented Jan 19, 2012 at 0:54

6 Answers 6

11

You could use

[(int(x) if x else 0) for x in data.split(',')]

data.split(',') splits the string into a list. It splits on the comma character:

['8', '5', '', '1', '4', '7', '', '', '', ...]

The expression

(int(x) if x else 0)

returns int(x) if x is True, 0 if x is False. Note that the empty string is False.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help, its really clear the way you and Dietrich Epp explained it.
5

Regular expressions are often unnecessary in Python. Given string s, try:

','.join(x or '0' for x in s.split(','))

I am assuming you want to fill the blanks with 0. If you want a list of integers instead of a string, try this:

[(x and int(x)) or 0 for x in s.split(',')]

Comments

4
s = "8,5,,1,4,7,,,,7,,1,9,3,6,,,8,6,3,9,,2,5,4,,,,,3,2,,,7,4,1,1,,4,,6,9,,5,,,,5,,,1,,6,3,,,6,5,,,,7,4,,1,7,6,,,,8,,5,,,7,1,,3,9,"
s = re.sub('((?<=,)|^)(?=,|$)', '0', s)
print s

Prints:

8,5,0,1,4,7,0,0,0,7,0,1,9,3,6,0,0,8,6,3,9,0,2,5,4,0,0,0,0,3,2,0,0,7,4,1,1,0,4,0,6,9,0,5,0,0,0,5,0,0,1,0,6,3,0,0,6,5,0,0,0,7,4,0,1,7,6,0,0,0,8,0,5,0,0,7,1,0,3,9,0

2 Comments

Wow thats insane, your a genius dude.
I agree with the "insane" part: I like to try writing those reges in order to test my skills, but for real programs, I'd rather use one of the other solutions posted here ;-)
0
>>> s="8,5,,1,4,7,,,,7,,1,9,3,6,,,8,6,3,9,,2,5,4,,,,,3,2,,,7,4,1,1,,4,,6,9,,5,,,,5,,,1,,6,3,,,6,5,,,,7,4,,1,7,6,,,,8,,5,,,7,1,,3,9,"
>>> s=s.split(",")
>>> for n,i in enumerate(s):
...     if i=="" : s[n]=0
...
>>> s
['8', '5', 0, '1', '4', '7', 0, 0, 0, '7', 0, '1', '9', '3', '6', 0, 0, '8', '6', '3', '9', 0, '2', '5', '4', 0, 0, 0, 0, '3', '2', 0, 0, '7', '4', '1', '1', 0, '4', 0, '6', '9', 0, '5', 0, 0, 0, '5', 0, 0, '1', 0, '6', '3', 0, 0, '6', '5', 0, 0, 0, '7', '4', 0, '1', '7', '6', 0, 0, 0, '8', 0, '5', 0, 0, '7', '1', 0, '3', '9', 0]
>>>

Comments

0

Simplest I can think of is

[int(x or 0) for x in s.split(',')]

or

[int('0'+x) for x in s.split(',')]

Comments

0

My solution uses map,lambda, and split. The final code looks like this:

sudoku_string = "1,2,3,,4,5,,6"
output_string = map(lambda x: '0' if x=='' else x, sudoku_string.split(","))

If you want the output as a list (i.e. [1,2,3,0,4,5,0,6]), then use

output_list = map(lambda x: 0 if x=='' else int(x), sudoku_string.split(",")

The commands map and lambda are very useful. map takes in a function and a list (really an iterable, but that's another story), and applies the function to every element of this list. So

def plus_one(x):
    return x+1
map(plus_one, [1,2,3,4])

returns [2,3,4,5]. lambda is a way to quickly define functions, so we can write plus_one as

lambda x: x+1

Lastly, split takes a string and creates a list by 'splitting' the string by the argument you pass. So "1 2 3 4".split(" ") yields [1,2,3,4].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.