string.replace doesn't removes ":"

  • Thread starter Joshua Robinson
  • Start date
J

Joshua Robinson

Hi *Monte-Pythons*,

x = "this is a simple : text: that has colon"
s = x.replace(string.punctuation, ""); OR
s = x.replace(string.punctuation, "");
print x # 'this is a simple : text: that has colon'
# The colon is still in the text !!!!

Is this a bug or am I doing something wrong ?

Py.Version: 2.7
OS: Ubuntu 12.10 (64 bits)

Cheers,
-Joshua
 
J

Johannes Bauer

Hi *Monte-Pythons*,

x = "this is a simple : text: that has colon"
s = x.replace(string.punctuation, ""); OR
s = x.replace(string.punctuation, "");
print x # 'this is a simple : text: that has colon'
# The colon is still in the text !!!!

Is this a bug or am I doing something wrong ?

The latter. str.replace() only replaces complete substrings, not single
character occurences of the given pattern. That is

"foo".replace("foo", "bar") == "bar"
"foofoo".replace("foo", "bar") == "barbar"
"foofoo".replace("fo", "bar") == "barobaro"
"foofoo".replace("abcdef", "bar") == "foofoo"

Regards,
Johannes

--
Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <[email protected]>
 
V

vduncan80

The latter. str.replace() only replaces complete substrings, not single

character occurences of the given pattern. That is



"foo".replace("foo", "bar") == "bar"

"foofoo".replace("foo", "bar") == "barbar"

"foofoo".replace("fo", "bar") == "barobaro"

"foofoo".replace("abcdef", "bar") == "foofoo"



Regards,

Johannes



--



Ah, der neueste und bis heute genialste Streich unsere großen

Kosmologen: Die Geheim-Vorhersage.

- Karl Kaos über Rüdiger Thomas in dsa <[email protected]>

Hello Joshua:

Hopefully you have worked out the issue. Johannes is right on the money using 'replace' as shown below.

x = "this is a simple : text: that has colon
s = x.replace(":", "")
print(s)
'this is a simple text that has colon'

Sincerely,
VDuncan
 
R

Rick Johnson

============================================================
REFERENCES:
============================================================
[1]: Should string.replace handle list, tuple and dict
arguments in addition to strings?

py> string.replace(('a', 'b', 'c'), 'abcdefgabc')
'defg'
[...]

And here is a fine example of how a "global function architecture" can seriously warp your mind! Let me try that again!

Hypothetical Examples:

py> 'abcdefgabc'.replace(('a', 'b', 'c'), "")
'defg'
py> 'abcdefgabc'.replace(['a', 'b', 'c'], "")
'defg'
py> 'abcdefgabc'.replace({'a':'A', 'b':'2', 'c':'C'})
'A2CdefgA2C'

Or, an alternative to passing dict where both old and new arguments accept the sequence:

py> d = {'a':'A', 'b':'2', 'c':'C'}
py> 'abcdefgabc'.replace(d.keys(), d.values())
'A2CdefgA2C'

Nice thing about dict is you can control both sub-string and replacement-string on a case-by-case basis. But there is going to be a need to apply a single replacement string to a sequence of substrings; like the null string example provided by the OP.

(hopefully there's no mistakes this time)
 
R

Rick Johnson

============================================================
REFERENCES:
============================================================
[1]: Should string.replace handle list, tuple and dict
arguments in addition to strings?

py> string.replace(('a', 'b', 'c'), 'abcdefgabc')
'defg'
[...]

And here is a fine example of how a "global function architecture" can seriously warp your mind! Let me try that again!

Hypothetical Examples:

py> 'abcdefgabc'.replace(('a', 'b', 'c'), "")
'defg'
py> 'abcdefgabc'.replace(['a', 'b', 'c'], "")
'defg'
py> 'abcdefgabc'.replace({'a':'A', 'b':'2', 'c':'C'})
'A2CdefgA2C'

Or, an alternative to passing dict where both old and new arguments accept the sequence:

py> d = {'a':'A', 'b':'2', 'c':'C'}
py> 'abcdefgabc'.replace(d.keys(), d.values())
'A2CdefgA2C'

Nice thing about dict is you can control both sub-string and replacement-string on a case-by-case basis. But there is going to be a need to apply a single replacement string to a sequence of substrings; like the null string example provided by the OP.

(hopefully there's no mistakes this time)
 
R

Rick Johnson

... table = {ord(k):table[k] for k in table}
... return s.translate(table)
...'€€€€€€€€€€€€€defg€€€€€€€€€€€€€'

[quip] I just really prefer a cryptic solution to a problem when a simplistic and consistent approach would suffice.[/quip] TO HELL WITH THE ZEN!

"Beautiful is better than ugly."
BROKEN!

"Explicit is better than implicit."
BROKEN!

"Simple is better than complex."
BROKEN!

"Sparse is better than dense."
BROKEN!

"Readability counts."
BROKEN BROKEN BROKEN!!!!

"Special cases aren't special enough to break the rules."
BROKEN!

"In the face of ambiguity, refuse the temptation to guess."
BROKEN!

"There should be one-- and preferably only one --obvious way to do it."
BROKEN BROKEN BROKEN!

"If the implementation is hard to explain, it's a bad idea."
BROKEN!

"If the implementation is easy to explain, it may be a good idea."
REINFORCED BY BAD EXAMPLE
 
M

Mark Lawrence

d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}
'abcdefgabc'.translate(d) 'A2CdefgA2C'


def jmTranslate(s, table):
... table = {ord(k):table[k] for k in table}
... return s.translate(table)
...
d = {'a': 'A', 'b': '2', 'c': 'C'}
jmTranslate('abcdefgabc', d) 'A2CdefgA2C'
d = {'a': None, 'b': None, 'c': None}
jmTranslate('abcdefgabc', d) 'defg'
d = {'a': '€€€€€', 'b': '€€€€', 'c': '€€€€'}
jmTranslate('abcdefgabc', d)
'€€€€€€€€€€€€€defg€€€€€€€€€€€€€'

[quip] I just really prefer a cryptic solution to a problem when a simplistic and consistent approach would suffice.[/quip] TO HELL WITH THE ZEN!

"Beautiful is better than ugly."
BROKEN!

"Explicit is better than implicit."
BROKEN!

"Simple is better than complex."
BROKEN!

"Sparse is better than dense."
BROKEN!

"Readability counts."
BROKEN BROKEN BROKEN!!!!

"Special cases aren't special enough to break the rules."
BROKEN!

"In the face of ambiguity, refuse the temptation to guess."
BROKEN!

"There should be one-- and preferably only one --obvious way to do it."
BROKEN BROKEN BROKEN!

"If the implementation is hard to explain, it's a bad idea."
BROKEN!

"If the implementation is easy to explain, it may be a good idea."
REINFORCED BY BAD EXAMPLE

jmf and rr in combination reminded me of this. I hope you all get my
drift :)

http://www.cc.gatech.edu/fac/Spencer.Rugaber/poems/love.txt
 
8

88888 Dihedral

Rick Johnsonæ–¼ 2013å¹´2月14日星期四UTC+8上åˆ12時34分11秒寫é“:
... table = {ord(k):table[k] for k in table}
... return s.translate(table)
'€€€€€€€€€€€€€defg€€€€€€€€€€€€€'
In python the variables of value types, and the variables of lists and
dictionaries are passed to functions somewhat different.

This should be noticed by any serious programmer in python.
 
J

jmfauth

Rick Johnsonæ–¼ 2013å¹´2月14日星期四UTC+8上åˆ12時34分11秒寫é“:






d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}
'abcdefgabc'.translate(d)
'A2CdefgA2C'
def jmTranslate(s, table):
...     table = {ord(k):table[k] for k in table}
...     return s.translate(table)
...
d = {'a': 'A', 'b': '2', 'c': 'C'}
jmTranslate('abcdefgabc', d)
'A2CdefgA2C'
d = {'a': None, 'b': None, 'c': None}
jmTranslate('abcdefgabc', d)
'defg'
d = {'a': '€€€€€', 'b':'€€€€', 'c': '€€€€'}
jmTranslate('abcdefgabc', d)
'€€€€€€€€€€€€€defg€€€€€€€€€€€€€'

In python the variables of value types, and the variables of lists and
dictionaries are passed to functions somewhat different.

This should be noticed by any serious programmer in python.

---------

The purpose of my quick and dirty fct was to
show it's possible to create a text replacement
fct which is using exclusively text / strings
via a dict. (Even if in my exemple, I'm using
- and can use - None as an empty string !)


You are right.

It is also arguable, that beeing forced to have
to use a number in order to replace a character,
may not be a so good idea.

This should be noticed by any serious language designer.

More seriously.
..translate() is a very nice and underestimated method.

jmf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top