我有一些示例字符串,如:
sample_lt1 = 'CANCEL Bitcoin kev 1 96 35 2 0 5 7 5 6'
sample_lt2 = 'CANCEL Bitcoln key 1 6 3 5 20 5 7 56 Cash 2 9 00'
sample_lt3 = 'CANCEL Bitcein key 0 1 5 0 0 4 4 1 6 Cash '
我正在尝试将字符串中用空格分隔的数字连接起来,但保留大写字母字符。这些样本输入的期望输出为:
sample_out1 = 'CANCEL Bitcoin kev 19635205756'
sample_out2 = 'CANCEL Bitcoln key 1635205756 Cash 2900'
sample_out3 = 'CANCEL Bitcein key 015004416 Cash ' # Removal of last space is ok.
到目前为止,我已完成此操作:
def ReForm(lt):
lts = lt.split()
c = 0
new = []
temp = []
while c<len(lts):
if lts[c].isnumeric():
temp.append(lts[c])
else:
if temp:
s = ''.join(temp)
new.append(s)
new.append(lts[c])
c += 1
ret = ' '.join(new)
return ret
我的代码给出的输出是:
CANCEL Bitcoin kev
CANCEL Bitcoln key 1635205756 Cash
CANCEL Bitcein key 015004416 Cash
在第一种情况和第二种情况下,它无法将以空格分隔的数字相加。我在做什么错了?
答案 0 :(得分:2)
使用正则表达式。 -> re.sub
与Lookbehind & Lookahead
例如:
import re
sample_lt1 = 'CANCEL Bitcoin kev 1 96 35 2 0 5 7 5 6'
sample_lt2 = 'CANCEL Bitcoln key 1 6 3 5 20 5 7 56 Cash 2 9 00'
sample_lt3 = 'CANCEL Bitcein key 0 1 5 0 0 4 4 1 6 Cash '
data = [sample_lt1, sample_lt2, sample_lt3]
for i in data:
print(re.sub(r"(?<=\d) (?=\d)", "", i))
输出:
CANCEL Bitcoin kev 19635205756
CANCEL Bitcoln key 1635205756 Cash 2900
CANCEL Bitcein key 015004416 Cash
答案 1 :(得分:1)
以下内容可修复您的程序:
def ReForm(lt):
lts = lt.split()
c = 0
new = []
temp = []
while c<len(lts):
if lts[c].isnumeric():
temp.append(lts[c])
else:
if temp:
s = ''.join(temp)
new.append(s)
temp = []
new.append(lts[c])
c += 1
if temp:
s = ''.join(temp)
new.append(s)
ret = ' '.join(new)
return ret
当最后一个字符是数字时,缺少循环是一个后备情况。在这种情况下,您的temp
列表未附加到new
。
缺少的另一件事是捕获了将temp
添加到整个字符串,但该字符串将继续包含不同内容的情况。这可以通过在循环中重新初始化temp
来实现。
答案 2 :(得分:0)
def ReForm(lt):
lts = lt.split()
c = 0
new = []
temp = []
while c<len(lts):
if lts[c].isnumeric():
temp.append(lts[c])
else:
if temp:
s = ''.join(temp)
new.append(s)
temp = []
new.append(lts[c])
c += 1
if temp:
s = ''.join(temp)
new.append(s)
ret = ' '.join(new)
return ret
答案 3 :(得分:0)
import re
s1 = 'CANCEL Bitcoin kev 1 96 35 2 0 5 7 5 6'
print(re.sub(r"(?<=\d) (?=\d)", "", s1))
CANCEL Bitcoin kev 19635205756
s2 = 'CANCEL Bitcoln key 1 6 3 5 20 5 7 56 Cash 2 9 00'
print(re.sub(r"(?<=\d) (?=\d)", "", s2))
CANCEL Bitcoln key 1635205756 Cash 2900