开发者

Removing a character in a string one at a time

开发者 https://www.devze.com 2023-01-29 13:01 出处:网络
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

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 ??


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.


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


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


how about this ?

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

精彩评论

暂无评论...
验证码 换一张
取 消