Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Just like your last question last question you can use str.translate:

You can do this with str.translate, which was changed slightly in Python 3. All you need to do is pass it a translation table, and it'll do what your above code is doing. However to build the translation table is hard in Python 2 and requires the helper function string.maketrans, which was changed to str.maketrans in Python 3.

However for this question, you don't need to use string.maketrans in Python 2, as str.translate takes a secondary argument of values to delete:

>>> 'abcdefghij'.translate(None, 'aeiou')
'bcdfghj'

However you do have to use str.maketrans in Python 3, as str.translate no longer has the second option:

>>> trans = str.maketrans('', '', 'aeiou')
>>> 'abcdefghij'.translate(trans)
'bcdfghj'
>>> trans = {ord('a'): None, ord('e'): None, ord('i'): None, ord('o'): None, ord('u'): None}
>>> 'abcdefghij'.translate(trans)
'bcdfghj'

The simplest way to use this would be to take the first letter and translate everything else:

>>> def removeVowels(string):
 trans = str.maketrans('', '', 'aeiouAEIOU')
 return string[0] + string[1:].translate(trans)
>>> removeVowels('Hello there')
'Hll thr'
>>> removeVowels('All, boo')
'All, b'

Just like your last question you can use str.translate:

You can do this with str.translate, which was changed slightly in Python 3. All you need to do is pass it a translation table, and it'll do what your above code is doing. However to build the translation table is hard in Python 2 and requires the helper function string.maketrans, which was changed to str.maketrans in Python 3.

However for this question, you don't need to use string.maketrans in Python 2, as str.translate takes a secondary argument of values to delete:

>>> 'abcdefghij'.translate(None, 'aeiou')
'bcdfghj'

However you do have to use str.maketrans in Python 3, as str.translate no longer has the second option:

>>> trans = str.maketrans('', '', 'aeiou')
>>> 'abcdefghij'.translate(trans)
'bcdfghj'
>>> trans = {ord('a'): None, ord('e'): None, ord('i'): None, ord('o'): None, ord('u'): None}
>>> 'abcdefghij'.translate(trans)
'bcdfghj'

The simplest way to use this would be to take the first letter and translate everything else:

>>> def removeVowels(string):
 trans = str.maketrans('', '', 'aeiouAEIOU')
 return string[0] + string[1:].translate(trans)
>>> removeVowels('Hello there')
'Hll thr'
>>> removeVowels('All, boo')
'All, b'

Just like your last question you can use str.translate:

You can do this with str.translate, which was changed slightly in Python 3. All you need to do is pass it a translation table, and it'll do what your above code is doing. However to build the translation table is hard in Python 2 and requires the helper function string.maketrans, which was changed to str.maketrans in Python 3.

However for this question, you don't need to use string.maketrans in Python 2, as str.translate takes a secondary argument of values to delete:

>>> 'abcdefghij'.translate(None, 'aeiou')
'bcdfghj'

However you do have to use str.maketrans in Python 3, as str.translate no longer has the second option:

>>> trans = str.maketrans('', '', 'aeiou')
>>> 'abcdefghij'.translate(trans)
'bcdfghj'
>>> trans = {ord('a'): None, ord('e'): None, ord('i'): None, ord('o'): None, ord('u'): None}
>>> 'abcdefghij'.translate(trans)
'bcdfghj'

The simplest way to use this would be to take the first letter and translate everything else:

>>> def removeVowels(string):
 trans = str.maketrans('', '', 'aeiouAEIOU')
 return string[0] + string[1:].translate(trans)
>>> removeVowels('Hello there')
'Hll thr'
>>> removeVowels('All, boo')
'All, b'
Source Link
Peilonrayz
  • 44.4k
  • 7
  • 80
  • 157

Just like your last question you can use str.translate:

You can do this with str.translate, which was changed slightly in Python 3. All you need to do is pass it a translation table, and it'll do what your above code is doing. However to build the translation table is hard in Python 2 and requires the helper function string.maketrans, which was changed to str.maketrans in Python 3.

However for this question, you don't need to use string.maketrans in Python 2, as str.translate takes a secondary argument of values to delete:

>>> 'abcdefghij'.translate(None, 'aeiou')
'bcdfghj'

However you do have to use str.maketrans in Python 3, as str.translate no longer has the second option:

>>> trans = str.maketrans('', '', 'aeiou')
>>> 'abcdefghij'.translate(trans)
'bcdfghj'
>>> trans = {ord('a'): None, ord('e'): None, ord('i'): None, ord('o'): None, ord('u'): None}
>>> 'abcdefghij'.translate(trans)
'bcdfghj'

The simplest way to use this would be to take the first letter and translate everything else:

>>> def removeVowels(string):
 trans = str.maketrans('', '', 'aeiouAEIOU')
 return string[0] + string[1:].translate(trans)
>>> removeVowels('Hello there')
'Hll thr'
>>> removeVowels('All, boo')
'All, b'
lang-py

AltStyle によって変換されたページ (->オリジナル) /