基本上是这样的:输入:
"0101101000101011011110" #0 is the character we are splitting after
输出:
["0", "10", "110", "10", "0", "0", "10", "10", "110", "11110"]
yxyvkwin1#
每次看到'0'时,请尝试以下操作:将tmp附加到result中,然后重置tmp:
'0'
tmp
result
s = "0101101000101011011110" res = [] tmp = '' for c in s: tmp += c if c == '0': res.append(tmp) tmp = '' print(res)
['0', '10', '110', '10', '0', '0', '10', '10', '110', '11110']
eni9jsuy2#
您可以使用正则表达式:
import re s = "0101101000101011011110" out = re.findall('(1*0|1+0?)', s)
regex demo或者:
out = re.split('(?<=0)(?=.)', s)
regex demo输出:
5n0oy7gb3#
x = str1.split("0") output = [ "0"+i for i in x]
3条答案
按热度按时间yxyvkwin1#
每次看到
'0'
时,请尝试以下操作:将tmp
附加到result
中,然后重置tmp
:输出:
eni9jsuy2#
您可以使用正则表达式:
regex demo
或者:
regex demo
输出:
5n0oy7gb3#