How to reverse lookup characters in list?
Keith Jones
kmj9907 at cs.rit.edu
Mon Jul 28 02:11:30 EDT 2003
okay, isn't the number in the list the index of the new letter? So you'd
just use alphabet[d].. though what you're doing can be done in a much
simpler manner..
for starters... check out the string module. you can do
import string
string.lowercase
that gives you a-z, lowercase.. I think you'll be fine with a string,
rather than a list (infact, it's better since it's immutable). If you must
ahve a list, you can do
[a for a in string.lowercase]
to get a list of characters from a to z.
Now, all you're really doing with all those lists and loops is just
mapping from one character to another. In fact, you're just doing a rotX,
where x is the value of your variable v
you could just do:
rotation = int(raw_input('rotation value: '))
word = raw_input('word: ')
....................
new_word = ''
for char in word:
position = string.lowercase.index(char)
position += rotation
# this makes it wrap around. i.e. 'z'+2 = 'b'
position = position % 26
new_word += string.lowercase[position]
print 'your new word is', new_word
....................
Hope that helps some.
On Mon, 28 Jul 2003 00:15:35 -0400, tjlan wrote:
> ----------------------------------------------------------------------------
> from time import sleep
> #Alphabet list used to move letters forward for simple encryption.
> alphabet =
> ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q....
>
> word_list = []
> number_list = []
> number_list2 = []
> new_word = []
> #variable for how far you want to switch v = 2
> word = raw_input("Word: ")
> a = len(word)
> for x in range(0, len(word)):
> print word[x],
> word_list.append(word[x])
> b = alphabet.index(word[x])
> number_list.append(b)
>
> for x in range(0, len(number_list)):
> c = number_list[x]
> c = c + v
> number_list2.append(c)
>
> for x in range(0, len(number_list2)):
> d = number_list2[x]
> new_word.append(d)
>
> for x in range(0,
> #Stopped here because dont know of way to switch back.
More information about the Python-list
mailing list