0

I can't make the iterator shorter while I'm running on it.

I want to write a function which gets a string and deletes repeating sequences in it. for example: if a have the string aaaaabbbbbbbcccccccDDDDDDaaaaa I should get in return abcDa.

I tried to run over the string with a for loop and every time I see a new letter I will save the letter in a variable which adds up to be the fixed string.

def string_sequence_fixing(string):
    c = ''
    for char in my_str:
        if c != char:
            c = char
        else:
            my_str = my_str.replace(c, '', my_str.count(c) - 1)
    return my_str

The problem I want to avoid is too many iterations. When I see a new character I want to delete all the other sequences of it, but the second line from the end does not update the "condition" in the for a loop.

Jhon Margalit
  • 451
  • 4
  • 12
  • 1
    Possible duplicate of [Scope of python variable in for loop](https://stackoverflow.com/questions/15363138/scope-of-python-variable-in-for-loop) –  Aug 21 '19 at 23:17

2 Answers2

0

Short Answer: loops don't work that way.
Longer answer:
Here is some simple pseudo code, for your perusal:
j=99 print "J is " j for j=0;20;5 print j \t end print "Now J is " j

The output may surprise you.
run J is 99 0 5 10 15 20 Now J is 99

The reason is: the variable j in the loop is NOT the as the j variable outside the loop.
I like to use the term "stack" (some languages claim they don't use a stack. In those cases I call it a "not-stack stack). The stack simple means a temporary storage space in memory. The initial variable "j" goes into the "program data space". The loop variable "j" goes into the "stack data space."

Using a variable doesn't 'really' mean you are using a variable, it's just a mnemonic to a memory space. Let's have another look at that sample code:
pointer-to-program-space-variable-named-j = 99 (poke into memory location 1:4500) pointer-to-stack-space-variable-named-j = 0 (poke into memory location 87:300) print pointer-to-stack-space-variable-named-j followed by tab increment pointer-to-stack-space-variable-named-j by 5 repeat until pointer-to-stack-space-variable-named-j = 20 print pointer-to-program-space-variable-named-j

With this knowledge, let's look at your code to see what is going on:
def string_sequence_fixing(string): c = '' for char in *STACK*.my_str: if c != char: c = char else: my_str = my_str.replace(c, '', *PROGRAM*.my_str.count(c) - 1) return my_str

See how they are different variables? NEVER assume that a loop variable and a program variable are the same. You need to redo you algorithm to accomplish what you want to do.

Also, see the link provided by @David Cullen.

Scottie H
  • 324
  • 1
  • 7
  • So you say that the variable that i use in the loop is some kind of a temporary variable copied from the original `my_str`. So how can I change the original `my_str`? – Jhon Margalit Aug 22 '19 at 00:56
  • I'm not a C guy. My suggestion would be something like this:
    else: my_return_str = my_str.replace(c, '', my_str.count(c) - 1) return my_return_str This way, you are not changing the value you are looping on.
    – Scottie H Aug 22 '19 at 15:49
0

You can use groupby() from itertools. Code is like:

data = 'aaabbbcccDDDDDEfggghiij'

from itertools import groupby
dataN = ''
for d in groupby(data):
    dataN += d[0]

print(dataN)

output:

abcDEfghij
ToughMind
  • 987
  • 1
  • 10
  • 28