2016-07-23 26 views
-4

"AAABBCCCDAAT"という文字列がありますので、次の出力はA3B2C3D1A2T1となります。 ランレングス符号化プログラム

data = input("enter data ") 
count =1 
for steps in range(0,len(data)-1): 
    if (str(data[steps])==str(data[steps+1])): 
     count = count+1 
    else: 
     str(data[steps])=str(data[steps+1]) 
     count=1 
print ((str(count)+str(data[steps])) 

iは、以下の出力はA3B2C3D1A2T1する必要がありますので、文字列は「AAABBCCCDAATを」と言っています。 おかげでここ

+0

私は質問が保留されていますが、これは「ランレングスエンコーディング」と呼ばれ、ここでのコードは、トリックを行う必要があるため、答えを投稿することはできませんします。http:// pastebin.com/y7fNDtQH。 – smarx

+0

ありがとうございますsmarx、私はコードを取得していない。はるかに簡単なコードを得るための助け。 – user3814582

+0

このコードを使用していましたが、特定のバグがある可能性があります。 (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

答えて

2

itertools.groupbyを使用して、一つの方法です:

In [62]: from itertools import groupby 

In [63]: data 
Out[63]: 'AAABBCCCDAAT' 

In [64]: ''.join(char + str(len(list(grp))) for char, grp in groupby(data)) 
Out[64]: 'A3B2C3D1A2T1' 
+0

ちょっと、groupbyを使ったコードを説明してもらえますか?私はそれを明確にしませんでした。またgroupby(データ)をここで説明することができました。 – user3814582

0

私が共有するためのいくつかのアプローチがありますが、私は@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'