如何修改我的代码以获得所需的格式?
How can I modify my code for the desired format?
def rle_encode(data):
encoding = ''
prev_char = ''
count = 1
if not data: return ''
for char in data:
# If the prev and current characters
# don't match...
if char != prev_char:
# ...then add the count and character
# to our encoding
if prev_char:
encoding += str(count) + prev_char
count = 1
prev_char = char
else:
# Or increment our counter
# if the characters do match
count += 1
else:
# Finish off the encoding
encoding += str(count) + prev_char
return encoding
encoded_val = rle_encode(“aaaaBBbbCooa”)
print(encoded_val)
对于上面的字符串,我得到的输出是:“4a2B2b1C2o1a”
但预期的输出是:“4a2B2bC2oa”
这意味着对于单个字符我不需要输入数字,只需要字符。
如果有人告诉我在哪里更改代码以获得预期的输出格式,那就太好了。
您可以使用 itertools.groupby
来实现。如果组的长度大于 1,则在前面添加计数,否则只添加字符本身。
from itertools import groupby
def rle_encode(data):
out = ''
for key, group in groupby(data):
run = len(list(group))
if run > 1:
out += str(run) + key
else:
out += key
return out
例子
>>> rle_encode("aaaaBBbbCooa")
'4a2B2bC2oa'
这是您的固定代码。
def rle_encode(data):
encoding = ''
prev_char = ''
count = 1
if not data: return ''
for char in data:
# If the prev and current characters
# don't match...
if char != prev_char:
# ...then add the count and character
# to our encoding
if prev_char:
if count > 1:
encoding += str(count) + prev_char
else:
encoding += prev_char
count = 1
prev_char = char
else:
# Or increment our counter
# if the characters do match
count += 1
else:
# Finish off the encoding
if count > 1:
encoding += str(count) + prev_char
else:
encoding += prev_char
return encoding
显然,@Cory 的解决方案更好,它甚至大大简化了您的功能。但是如果你仍然想要你的解决方案,我认为它就像删除'1'
一样简单
def rle_encode(data):
encoding = ''
prev_char = ''
count = 1
if not data: return ''
for char in data:
# If the prev and current characters
# don't match...
if char != prev_char:
# ...then add the count and character
# to our encoding
if prev_char:
encoding += str(count) + prev_char
count = 1
prev_char = char
else:
# Or increment our counter
# if the characters do match
count += 1
# Finish off the encoding
encoding += str(count) + prev_char
return encoding.replace('1', '')
encoded_val = rle_encode('aaaaBBbbCooa')
print(encoded_val)
顺便说一句,我认为你的最后一个“else”没有必要。
def rle_encode(data):
encoding = ''
prev_char = ''
count = 1
if not data: return ''
for char in data:
# If the prev and current characters
# don't match...
print(char," ", count)
if char != prev_char:
# ...then add the count and character
# to our encoding
if prev_char:
if count != 1:
encoding += str(count) + prev_char
else:
encoding += prev_char
count = 1
prev_char = char
else:
# Or increment our counter
# if the characters do match
count += 1
else:
# Finish off the encoding
if count != 1:
encoding += str(count) + prev_char
else:
encoding += prev_char
return encoding
encoded_val = rle_encode('aaaaBBbbCooa')
print(encoded_val)
Hope this will help you.
def rle_encode(data):
encoding = ''
prev_char = ''
count = 1
if not data: return ''
for char in data:
# If the prev and current characters
# don't match...
if char != prev_char:
# ...then add the count and character
# to our encoding
if prev_char:
encoding += str(count) + prev_char
count = 1
prev_char = char
else:
# Or increment our counter
# if the characters do match
count += 1
else:
# Finish off the encoding
encoding += str(count) + prev_char
return encoding
encoded_val = rle_encode(“aaaaBBbbCooa”)
print(encoded_val)
对于上面的字符串,我得到的输出是:“4a2B2b1C2o1a”
但预期的输出是:“4a2B2bC2oa”
这意味着对于单个字符我不需要输入数字,只需要字符。
如果有人告诉我在哪里更改代码以获得预期的输出格式,那就太好了。
您可以使用 itertools.groupby
来实现。如果组的长度大于 1,则在前面添加计数,否则只添加字符本身。
from itertools import groupby
def rle_encode(data):
out = ''
for key, group in groupby(data):
run = len(list(group))
if run > 1:
out += str(run) + key
else:
out += key
return out
例子
>>> rle_encode("aaaaBBbbCooa")
'4a2B2bC2oa'
这是您的固定代码。
def rle_encode(data):
encoding = ''
prev_char = ''
count = 1
if not data: return ''
for char in data:
# If the prev and current characters
# don't match...
if char != prev_char:
# ...then add the count and character
# to our encoding
if prev_char:
if count > 1:
encoding += str(count) + prev_char
else:
encoding += prev_char
count = 1
prev_char = char
else:
# Or increment our counter
# if the characters do match
count += 1
else:
# Finish off the encoding
if count > 1:
encoding += str(count) + prev_char
else:
encoding += prev_char
return encoding
显然,@Cory 的解决方案更好,它甚至大大简化了您的功能。但是如果你仍然想要你的解决方案,我认为它就像删除'1'
一样简单def rle_encode(data):
encoding = ''
prev_char = ''
count = 1
if not data: return ''
for char in data:
# If the prev and current characters
# don't match...
if char != prev_char:
# ...then add the count and character
# to our encoding
if prev_char:
encoding += str(count) + prev_char
count = 1
prev_char = char
else:
# Or increment our counter
# if the characters do match
count += 1
# Finish off the encoding
encoding += str(count) + prev_char
return encoding.replace('1', '')
encoded_val = rle_encode('aaaaBBbbCooa')
print(encoded_val)
顺便说一句,我认为你的最后一个“else”没有必要。
def rle_encode(data):
encoding = ''
prev_char = ''
count = 1
if not data: return ''
for char in data:
# If the prev and current characters
# don't match...
print(char," ", count)
if char != prev_char:
# ...then add the count and character
# to our encoding
if prev_char:
if count != 1:
encoding += str(count) + prev_char
else:
encoding += prev_char
count = 1
prev_char = char
else:
# Or increment our counter
# if the characters do match
count += 1
else:
# Finish off the encoding
if count != 1:
encoding += str(count) + prev_char
else:
encoding += prev_char
return encoding
encoded_val = rle_encode('aaaaBBbbCooa')
print(encoded_val)
Hope this will help you.