私が共有するためのいくつかのアプローチがありますが、私は@Warren Weckesserの答えは優れていると思います。 :-)
# Regular expression approach:
import re
def run_length_encode(text):
# When the match is for AAA:
# match.group(1) == 'A'
# match.group(0) == 'AAA'
# len(match.group(0)) == 3
def encode_single_run(match):
return '{}{}'.format(match.group(1), len(match.group(0)))
# Replace each occurrence of a letter followed by any number of identical letters with the
# output of encode_single_run on that match
return re.sub(r'(.)\1*', encode_single_run, text)
assert run_length_encode('AAABBCCCDAAT') == 'A3B2C3D1A2T1'
# While loop approach:
def run_length_encode(text):
result = []
position = 0
# Until we pass the end of the string
while position < len(text):
# Grab the current letter
letter = text[position]
# Advance our current position
position += 1
# Start at a count of 1 (how many times the letter occurred)
count = 1
# While we haven't fallen off the array and the letter keeps repeating
while position < len(text) and text[position] == letter:
# Move forward
count += 1
position += 1
# Append the letter and count to the result
result.append((letter, count))
# At this point, result looks like [('A', 3), ('B', 2), ...]
# This next line combines the letters and counts and then puts everything into a single string
return ''.join('{}{}'.format(letter, count) for letter, count in result)
assert run_length_encode('AAABBCCCDAAT') == 'A3B2C3D1A2T1'
# State machine approach:
def run_length_encode(text):
result = []
# Keeps track of the letter we're in the midst of a run of
current_letter = None
# Keeps track of how many times we've seen that letter
count = 0
# Iterate over each letter in the string
for letter in text:
# If it's the same letter we're already tracking
if letter == current_letter:
count += 1
else:
# Unless it's the very start of the process
if count > 0:
result.append((current_letter, count))
# Start tracking the new letter
current_letter = letter
count = 1
# Without this, we'd fail to return the last letter
result.append((current_letter, count))
# At this point, result looks like [('A', 3), ('B', 2), ...]
# This next line combines the letters and counts and then puts everything into a single string
return ''.join('{}{}'.format(letter, count) for letter, count in result)
assert run_length_encode('AAABBCCCDAAT') == 'A3B2C3D1A2T1'
私は質問が保留されていますが、これは「ランレングスエンコーディング」と呼ばれ、ここでのコードは、トリックを行う必要があるため、答えを投稿することはできませんします。http:// pastebin.com/y7fNDtQH。 – smarx
ありがとうございますsmarx、私はコードを取得していない。はるかに簡単なコードを得るための助け。 – user3814582
このコードを使用していましたが、特定のバグがある可能性があります。 (str(data [steps])== str(data [steps + 1])の範囲内のステップについては、data = input( "データ入力") count = 1 範囲内のステップ(0、len(data)-1): \t )): #\t \t STR(データ[ステップ])= STR(データ[ステップ+ 1]) \t \tカウント= 1 #print(カウント) プリント:\t \tカウント=カウント+他の1 \t (str(count)+ str(data [steps])) – user3814582