[Tutor] any easier way?

Remco Gerlich scarblac@pino.selwerd.nl
Tue, 26 Mar 2002 22:13:39 +0100


On  0, Azrael <luisnoelf@yahoo.es> wrote:
> 
> 
> >Hi, im working in a prog to rename files taking their previous name and 
> >changing the capital letters to small letters; well this is the code i 
> >have done to do this, my question is if somebody can tell me and easier or 
> >alternative way of doing it, im trying to learn so anything u tell me will 
> >be welcome :)
> >
> >(sorry but the vars are in spanish, didnt had the time to translate them)

This sort of thing is builtin! You can change it to

def cambio(palabra):
   return palabra.upper()
   
or, if you have an older Python,

import string
def cambio(palabra):
   return string.upper(palabra)
   

But at least making the function was a good exercise :). I have some
comments on that code too.

> >def cambio(palabra):
> >     minusculas = 
> > ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','n','o','p','q','r','s','t','u','v','w','x','y','z']
> >     mayusculas = 
> > ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

These already exist (as strings), as string.lowercase and string.uppercase.

> >     palabrac = ''
> >     letras = []
> >     x = 0
> >     for n in range (len(palabra)):
> >         letras.append(palabra[n])

This bit just turns the string palabra into a list; you can do that with
letras = list(palabra) .

> >     final  = letras
> >     while x < len(letras):
> >         for zero in range(len(mayusculas)):
> >             if letras[x] == mayusculas[zero]:
> >                 m = mayusculas.index(mayusculas[zero])
> >                 final.insert(x,minusculas[m])
> >                 del final[x+1]
> >             else:
> >                 pass
> >         x = x + 1

Here, for every letter in letras, you go through all the letters in
mayusculas, and if they're equal, you find the index (you already know that,
it's in your 'zero' variable) and set m equal to it. You should have gone
through minusculas instead - now you're only changing upper case to upper
case.

And instead of final.insert(x, something) followed by del final[x+1], don't
you think that final[x] = something is a bit better? :)

> >     for z in range(len(final)):
> >         palabrac = palabrac + final[z]
> >     return palabrac

And to turn a list of characters into a strings, you can use ''.join(final)
(join the characters together, with '' (nothing) in between).

Your function, cleaned up, could look like this:

import string

def cambio(palabra):
   minusculas = string.lowercase
   mayusculas = string.uppercase
   final = []
   for character in palabra:
      index = minusculas.find(character)
      if index == -1:
         # Not in minusculas, add unchanged
	 final.append(character)
      else:
         # In minusculas, add uppercase
	 final.append(mayusculas[index])
   return ''.join(final)

But, of course, it's just an exercise, this is already builtin :)

-- 
Remco Gerlich