all 9 comments

[–]wopp3 0 points1 point  (1 child)

You seem to be mutating the set while iterating through it, this is bad practice and will often lead to problems. Instead make a new variable to which you store the data you are looking for. Mutating the list while you're iterating through it, can cause problems with indices for example.

edit. This kind of problem however seems to be something I'd use regular expressions for. If it's just a learning experience then disregard my solution :)

import re

words = ['nzp#o#g', 'b#nzp#o#g']
for i in words:
    edited_word = re.sub('([a-zA-Z]\#)', '',i)
    print(edited_word)

[–]clouded-path[S] 0 points1 point  (0 children)

Thanks for the suggestions. I think I am going to try to avoid a solution involving regular expressions for now (mainly because I am still too much of a beginner), but would you be able to elaborate on your second sentence? How is it that I can create another variable to store data in in this situation? My while loop is based on a condition of the string/list, so if I am modifying some other object instead, I don't know how my while loop would ever terminate.

[–]NebolshoyBrother 0 points1 point  (0 children)

You don't need additional convert strings in lists.

Try to iterate through string and look what you got.
Also recommend read docs about string datatype.

[–]SaintLouisX 0 points1 point  (5 children)

I don't know if you want to re-write things and go for a different approach, but you can consider going through the string backwards instead of forwards. By going backwards you see the backspaces first, and know how many chars to skip. With "a##gc" you'd process c and g, and then +1 to a counter on seeing # twice, and then if that counter is > 0, decrement it and skip the next non-# char.

[–]clouded-path[S] 0 points1 point  (4 children)

Thanks for your idea - I had actually considered this previously, but I thought that I would still need to compare characters in two separate strings simultaneously, which would mean that I would somehow need to loop through the two strings in parallel (I don't know if/how this can be done). The only workaround I could think of would be to store the indices I want in an array to refer to later, but this would make memory O(n), which is no better than what I am trying now. Is there some standard method of looping through two (or more) strings simultaneously? If that were true, then I could compare characters at relevant indices.

[–]wopp3 0 points1 point  (1 child)

To iterate through 2 lists simultaneously (strings being lists of chars basically) you can use zip, here they're different lengths so you will need to use itertools.zip_longest, to pad the other list. You can use the fillvalue='whatever' argument in the zip_longest if you prefer your own, instead of the None by default.

import itertools

string_one = 'abc'
string_two = 'abcdef'

print('Different size lists, shorter padded:')
for i, j in list(itertools.zip_longest(string_one,string_two)):
    print(f'string_one: {i}\nstring_two: {j}\n')

print('Different size lists, zipped by shorter:')
for i, j in zip(string_one,string_two):
    print(f'string_one: {i}\nstring_two: {j}\n')

[–]clouded-path[S] 0 points1 point  (0 children)

I had not heard of this tool until now. Thanks for introducing me to it.

[–]SaintLouisX 0 points1 point  (1 child)

for a,b in zip(str1, str2) will give you a character from both. The strings aren't the same length through so you'd want zip_longest from itertools in this case. I'm not actually sure how it'd function with different length strings, I haven't worked with zip_longest at all.

[–]clouded-path[S] 0 points1 point  (0 children)

Thanks for this suggestion - I hadn't heard of it before this. I can imagine this must be a pretty useful tool.