Replace empty string python

How to replace empty string with zero in comma-separated string?

«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.

6 Answers 6

[(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:

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

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(',')] 
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 

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 😉

>>> 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] >>> 

Simplest I can think of is

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

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

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] .

Источник

How to replace an empty string with 0, but leave it alone if not empty

I can get the results I want by other means, but I wondered if anybody understands why python produces these results.

Perhaps you want to define a function that does something like def myreplace(string): return string if string.strip() else '0'

5 Answers 5

returns a copy of s with every occurrence of the string old replaced with new , so for example:

In [7]: "abracadabra".replace("a","4") Out[7]: '4br4c4d4br4' 

As a special case, if old is the empty string, it inserts new at the start and end of the string and between every pairs of characters:

In [8]: "12345678".replace("","_") Out[8]: '~6_7_8_' 

The rationale is that there's a "empty string" before the first character, between each pair of characters, and after the last character, and that's what's being replaced.

So, replace isn't doing what you were thinking.

To do what you what, you can use one of the solutions already proposed, or something like this if you're feeling clever:

This is related to how replace works.

Think of it as looking through every index of the string, and creating a substring starting from each index. Then it checks if any of the substrings start with the string to look for. It then records the index of the substrings that start with the string to look for, removes the your string to look for, and inserts your replacement string at those indices.

For '' , there is only one index to look through, and the substring starting from that index is '' , which starts with '' , so a 0 is inserted.

For 50 , there are 3 indices to look through. The substrings starting from the indices are:

All of these substrings start with '' , so a 0 is inserted in all the indices, creating '05000' .

Источник

Читайте также:  Sinatra Single-Serve
Оцените статью