How to reverse lookup characters in list?

Discussion in 'Python' started by tjland@iserv.net, Jul 28, 2003.

  1. Guest

    Okay so im working on a very simple encryption method using just loops.
    Kind of novel i think. Okay so first i set up a list of the alphabet with
    just every seperate letter, then user is prompted for a word, this is not
    user friendly just for me. Ok now as you can see below this is pretty
    basic, if u can follow all the loops i get to a point where I have the
    letter positions in the list for the final word but i dont know of a way
    to come back with the corresponding letter to output to the user. Ummm
    some help would be really appreciated. Sorry about the length!


    ----------------------------------------------------------------------------
    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","r","s","t","u","v","w","x","y","z"]

    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.
    ----------------------------------------------------------------------------



    When you see the net take the shot
    When you miss your shot shoot again
    When you make your shot you win!

    Just remember
    Offense sells tickets
    But defense wins championships!
    , Jul 28, 2003
    #1
    1. Advertising

  2. Keith Jones Guest

    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.
    Keith Jones, Jul 28, 2003
    #2
    1. Advertising

  3. Keith Jones wrote:

    > 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.


    Or simply "list(string.lowercase)".


    // Klaus

    --
    ><> unselfish actions pay back better
    Klaus Alexander Seistrup, Jul 28, 2003
    #3
  4. Andrew Dalke Guest

    Keith Jones
    > import string
    > string.lowercase
    >
    > that gives you a-z, lowercase..


    Actually, use 'string.ascii_lowercase' because 'string.lowercase' depends
    on your locale.

    On the other hand, for this application, string.lower might be the right
    thing.

    Also, the tjland? You need to change the

    c = c + v
    into
    c = (c + v) % len(alphabet)

    Suppose c is 'z' and v is 2. Then 25 (the position of the 'z') + 2 is 27,
    which
    is beyond the list of letters. You need some way to loop around to 0 once
    you go over the end, and the '%' is the loop-around operator. (It's
    actually
    called the mod function.) It also works the other way, so "-1 % 26" loops
    around to 25.

    You might find this helpful

    >>> s = "testing"
    >>> v = 2
    >>> letters = string.ascii_lowercase
    >>> t = ""
    >>> for c in s:

    .... t = t + letters[(letters.index(c)+v)%len(letters)]
    ....
    >>> t

    'vguvkpi'
    >>> s = t
    >>> v = -2
    >>> t = ""
    >>> for c in s:

    .... t = t + letters[(letters.index(c)+v)%len(letters)]
    ....
    >>> t

    'testing'
    >>>


    With a bit more experience, I think you'll find this also interesting

    v = 2

    # Start by mapping each letter to itself
    # (There are 256 possible values in an 8-bit character.)
    encoding_d = {}
    for i in range(256):
    c = chr(i)
    encoding_d[c] = c

    lc = string.ascii_lowercase
    for i in range(len(lc)):
    from_char = lc
    to_char = lc[(i+v) % len(lc)]
    encoding_d[from_char] = to_char

    uc = string.ascii_uppercase
    for i in range(len(uc)):
    from_char = uc
    to_char = uc[(i+v) % len(uc)]
    encoding_d[from_char] = to_char

    s = "This is a test."

    # This is better written as:
    # t = "".join([encoding_d[c] for c in s])
    # but that's something to try out after you learn a
    # bit more Python.

    t = ""
    for c in s:
    t = t + encoding_d[c]

    print t

    # Make a decoding dictionary which is the opposite of the
    # encoding dictionary
    decoding_d = {}
    for from_char, to_char in encoding_d.items():
    decoding_d[to_char] = from_char

    # or as: u = "".join([decoding_d[c] for c in t])
    u = ""
    for c in t:
    u = u + decoding_d[c]

    print u

    With some more experience beyond that, you might write it like this
    (It uses a very different style and implementation)

    import string

    def _r(s, n):
    # rotate the characters left n positions
    n %= len(s) # make sure it's properly within range
    return s[n:] + s[:n]

    class RotEncoder:
    def __init__(self, n):
    from_letters = string.ascii_lowercase + string.ascii_uppercase
    to_letters = _r(string.ascii_lowercase, n) + _r(string.ascii_uppercase,
    n)
    self.encode_table = string.maketrans(from_letters, to_letters)
    self.decode_table = string.maketrans(to_letters, from_letters)
    def encode(self, s):
    return string.translate(s, self.encode_table)
    def decode(self, s):
    return string.translate(s, self.decode_table)

    code = RotEncoder(2)
    print code.encode("testing")
    print code.decode(code.encode("testing"))

    Andrew
    Andrew Dalke, Jul 28, 2003
    #4
  5. Bob Gailer Guest

    At 06:11 AM 7/28/2003 +0000, Keith Jones wrote:

    >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]


    Or just list(string.lowercase])
    [snip]

    Bob Gailer

    303 442 2625


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.500 / Virus Database: 298 - Release Date: 7/10/2003
    Bob Gailer, Jul 28, 2003
    #5
  6. Keith Jones Guest


    > Or just list(string.lowercase])
    > [snip]
    >


    Haha.. I KNEW there was a better way to do it! It was late, I was tired, I
    had list comprehensions on my mind, <insert more inane excuses here>. :)


    Keith
    Keith Jones, Jul 28, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGFuIE5hc2g=?=

    reverse DNS lookup

    =?Utf-8?B?RGFuIE5hc2g=?=, Oct 14, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    454
    =?Utf-8?B?RGFuIE5hc2g=?=
    Oct 14, 2004
  2. Madhur Ahuja
    Replies:
    1
    Views:
    727
    Paul Lutus
    Aug 29, 2004
  3. rh0dium
    Replies:
    10
    Views:
    843
    bruno at modulix
    Mar 10, 2006
  4. Scott McFadden

    Reverse lookup question (IP to Country)

    Scott McFadden, Oct 25, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    333
    Juan T. Llibre
    Oct 25, 2007
  5. Yankee Imperialist Dog

    web service for reverse lookup phone number

    Yankee Imperialist Dog, Jun 6, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    1,148
    Munna
    Jun 7, 2008
Loading...

Share This Page