user506710
user506710

Reputation:

Removing a character in a string one at a time

Basically I want to remove a character in a string one at a time if it occurs multiple times .

For eg :- if I have a word abaccea and character 'a' then the output of the function should be baccea , abacce , abccea.

I read that I can make maketrans for a and empty string but it replaces every a in the string.

Is there an efficient way to do this besides noting all the positions in a list and then replacing and generating the words ??

Upvotes: 4

Views: 2321

Answers (4)

Mark Byers
Mark Byers

Reputation: 839254

I'd say that your approach sounds good - it is a reasonably efficient way to do it and it will be clear to the reader what you are doing.

However a slightly less elegant but possibly faster alternative is to use the start parameter of the find function.

i = 0
while True:
    j = word.find('a', i)
    if j == -1:
        break
    print word[:j] + word[j+1:]
    i = j + 1

The find function is likely to be highly optimized in C, so this may give you a performance improvement compared to iterating over the characters in the string yourself in Python. Whether you want to do this though depends on whether you are looking for efficiency or elegance. I'd recommend going for the simple and clear approach first, and only optimizing it if performance profiling shows that efficiency is an important issue.

Here are some performance measurements showing that the code using find can run faster:

>>> method1='[s[:key] + s[key+1:] for key,val in enumerate(s) if val == "a"]'
>>> method2='''
result=[]
i = 0
while True:
    j = s.find('a', i)
    if j == -1:
        break
    result.append(s[:j] + s[j+1:])
    i = j + 1
'''

>>> timeit.timeit(method1, init, number=100000)
2.5391986271997666
>>> timeit.timeit(method2, init, number=100000)
1.1471052885212885

Upvotes: 1

Morlock
Morlock

Reputation: 7131

You could try the following script. It provides a simple function to do what you ask. The use of list comprehensions [x for x in y if something(x)] is well worth learning.

#!/usr/bin/python

word = "abaccea"
letter = "a"

def single_remove(word, letter):
    """Remove character c from text t one at a time
    """
    indexes = [c for c in xrange(len(word)) if word[c] == letter]
    return [word[:i] + word[i + 1:] for i in indexes]

print single_remove(word, letter)

returns ['baccea', 'abccea', 'abacce']

Cheers

Upvotes: 3

Yuda Prawira
Yuda Prawira

Reputation: 12481

how about this ?

>>> def replace_a(word):
...     word = word[1:8]
...     return word
... 
>>> replace_a("abaccea")
'baccea'
>>>

Upvotes: -2

Gabi Purcaru
Gabi Purcaru

Reputation: 31574

Here is a quick way of doing it:

In [6]: s = "abaccea"
In [9]: [s[:key] + s[key+1:] for key,val in enumerate(s) if val == "a"]
Out[10]: ['baccea', 'abccea', 'abacce']

There is the benefit of being able to turn this into a generator by simpling replacing square brackets with round ones.

Upvotes: 5

Related Questions