콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
5 답글
21809 화면

Already tried using: name = str.replace(name, ",","") but either i don't know what should I import into module or should i use different command

or getting an error: TypeError: descriptor 'replace' requires a 'str' object but received a 'list'

Would anyone tell me how  to do it ?

Quiet urgent ..... ;)

 

아바타
취소

Try this code: name = [i.replace(",", "") for i in name]

작성자
numbers = self.eangen()
numbers.append(self.calcheck(numbers))
res = ''.join(map(str, numbers))
record.write({'intcode': res})
Puzzle solved (partially) :)
베스트 답변

Hi Dr Obx,

You can use strip() or replace() method.

str = " Anil Kesariya "
new_str = str.strip()
# o/p: Anil Kesariya

This method will remove unwanted space from left and right side of string.

If you want to remove the space or any symbolic pattern from whole string than you can use replace().

eg:1 remove space
str = " A n i l K e s a r i y a "
new_str = str.strip(" ","")
o/p : Anilkesariya
eg:2 remove "
str = Anil"kesari"ya'
new_str = str.replace('"',"")
o/p: Anilkesariya

Hope this will help.

Rgds,

Anil.






아바타
취소
베스트 답변

Hello my friend;

Strings in python are immutable (can't be changed). Because of this, the effect of line.replace(...) is just to create a new string, rather than changing the old one. You need to rebind (assign) it to line in order to have that variable take the new value, with those characters removed.

Also, the way you are doing it is going to be kind of slow, relatively. It's also likely to be a bit confusing to experienced pythonators, who will see a doubly-nested structure and think for a moment that something more complicated is going on.

You can instead use str.translate: (https://docs.python.org/2/library/stdtypes.html#str.translate)

line = line.translate(None, '!@#$')

which only works on Python 2.6 and newer Python 2.x versions * —

or regular expression replacement with re.sub (https://docs.python.org/2/library/re.html#re.sub)

import re

line = re.sub('[!@#$]', '', line)

Here is an example:

s = "this is a string"

l = list(s) # convert to list

l[1] = "" # "delete" letter h (the item actually still exists but is empty)

l[1:2] = [] # really delete letter h (the item is actually removed from the list)

del(l[1]) # another way to delete it

p = l.index("a") # find position of the letter "a"

del(l[p]) # delete it

s = "".join(l) # convert back to string

Timing comparison

def findreplace(m_string, char):

m_string = list(m_string)

for k in m_string:

if k == char:

del(m_string[m_string.index(k)])

return "".join(m_string)

def replace(m_string, char):

return m_string.replace("i", "")

def translate(m_string, char):

return m_string.translate(None, "i")

from timeit import timeit

print timeit("findreplace('it is icy','i')", "from __main__ import findreplace")

print timeit("replace('it is icy','i')", "from __main__ import replace")

print timeit("translate('it is icy','i')", "from __main__ import translate")

Result

1.64474582672
0.29278588295
0.311302900314

str.replace and str.translate methods are 8 and 5 times faster.

Regards.

아바타
취소
작성자

Gr8 thanx. I'm a beginner so every information matter ;) I'll take it into my head so next time I'll try your ideas and methods ;) Thank you