python 如何在逗号分隔的字符串中用零替换空字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2606976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 01:03:02  来源:igfitidea点击:

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

pythonstring

提问by Kev

"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,"

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

我正在做一个编程挑战,我需要将此序列解析为我的数独脚本。需要把上面的序列变成8,5,0,1,4,7,0,0,0,7,0,1,9,3,6,0,0,8........ . 我试过重新但没有成功,感谢帮助,谢谢。

回答by unutbu

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:

data.split(',')将字符串拆分为一个列表。它在逗号字符上拆分:

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

The expression

表达方式

(int(x) if x else 0)

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

int(x)如果x为真则返回,如果x为假则返回0 。请注意,空字符串为 False。

回答by Dietrich Epp

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

Python 中通常不需要正则表达式。给定 string s,尝试:

','.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:

我假设你想用 0 填充空格。如果你想要一个整数列表而不是一个字符串,试试这个:

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

回答by Joachim Sauer

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

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

回答by John La Rooy

Simplest I can think of is

我能想到的最简单的是

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

or

或者

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

回答by mellort

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

我的解决方案使用maplambdasplit。最终代码如下所示:

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

如果您希望将输出作为列表(即[1,2,3,0,4,5,0,6]),则使用

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

The commands mapand lambdaare very useful. maptakes 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

命令maplambda非常有用。map接受一个函数和一个列表(实际上是一个可迭代的,但那是另一回事),并将该函数应用于该列表的每个元素。所以

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

returns [2,3,4,5]. lambdais a way to quickly define functions, so we can write plus_oneas

返回[2,3,4,5]lambda是一种能够快速定义功能,让我们可以写plus_one

lambda x: x+1

Lastly, splittakes 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].

最后,split接受一个字符串并通过您传递的参数“拆分”字符串来创建一个列表。所以"1 2 3 4".split(" ")产量[1,2,3,4]

回答by ghostdog74

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