Okay, so I've been working on a program that converts ascii values to text. The way that I have the code is functional, yet not convenient. Let me post the code and then explain.-

letters=['       enter          ','',' ',0,1,2,3,4,5,6,7,8,9,'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','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']

numbers=[10,13,32,48,49,50,51,52,53,54,55,56,57,65,66,67,\
68,69,70,71,72,73,74,75,76,77,78,79,80,81,\
82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,\
106,107,108,109,110,111,112,113,114,115,116,\
117,118,119,120,121,122]


# Ascii values in the following list

result3=[83, 97, 109, 117, 101, 108, 32, 83, 109, 105, 116, 104, 13,\
10, 13, 10, 84, 104, 97, 110, 107, 32, 121, 111, 117, 32, 102, 111, 114, 32,\
108, 111, 111, 107, 105, 110, 103, 32, 116, 104, 101, 32, 111, 116, 104, 101, 114,\
32, 119, 97, 121, 32, 111, 110, 32, 116, 104, 101, 32, 105, 110, 99, 114, 101, 97, 115,\
101, 100, 32, 108, 101, 118, 101, 108, 115, 32, 111, 102, 32, 116, 111, 120, 105, 99, 32,\
99, 104, 101, 109, 105, 99, 97, 108, 115, 32, 105, 110, 32, 116, 104, 101, 32, 114, 105, 118,\
101, 114, 32, 114, 117, 110, 110, 105, 110, 103, 32, 97, 108, 111, 110, 103, 115, 105]


result4=[]

while len(result3)!=0: #while 3 is not equal to 0:
    if result3[0]==numbers[0]:
        print letters[0],
        result4.append(letters[0])
        result3.remove(result3[0])
        

        letters=['       enter          ','',' ',0,1,2,3,4,5,6,7,8,9,'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','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']

        numbers=[10,13,32,48,49,50,51,52,53,54,55,56,57,\
65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,\
        82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,\
106,107,108,109,110,111,112,113,114,115,116,\
        117,118,119,120,121,122]
    elif result3[0]!=numbers[0]:
        numbers.remove(numbers[0])
        #print "letters 0", letters[0]
        letters.remove(letters[0])

Okay, the indentation isn't quite correct in this posted code here, but what this does is it starts out with a list of possible ascii values in the numbers list and a list of equivalent text values in the letters list. Then it checks the result3 list and if the value in the result3 list doesn't match the first value in the numbers list, then it removes the first value in the numbers list until it is a match, then it prints that value and replaces the numbers list and the letters list. What I want to be able to do is specify the values for the letters and numbers at the beginning, then when I want to replace the values within the while loop, I want to refer back to the previous values without having to list all of them out again as it is currently in this code. I hope this makes sense.

Can anybody help me out with this. I've tried many different things, but none of them seem to work. And I'm guessing this is pretty sloppy code. Maybe somebody with a little more experience could tidy it up for me?

Any help is appreciated. Let me know if you need more clarification.

PSST! Secret:

>>> import string
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'

And PSST!!! To turn them into lists use list comprehension:

>>> [ lett for lett in string.letters ]
['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', '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']
>>> [ dig for dig in string.digits ]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>>

Oh!! also for the ascii values use another string comprehension!!!

>>> [ ord( lett ) for lett in string.letters ]
[97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90

Thank you. I will give this a try and see what I get.

Not to be picky, but an iterable can be converted to a list with the built-in function list.

>>> import string
>>> list(string.letters)
['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', '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']

Yar. I actually looked at what your code does and it is extremely over complicating the matter... here's a much simpler method:

import string

letters = list(string.letters) + list(string.digits) + ['\n','\r',' ']
letters.sort()
numbers = [ ord(i) for i in letters ]

# Ascii values in the following list
result3=[83,97,109,117,101,108,32,83,109,105,116,104,13,10,13,10,84,\
        104,97,110,107,32,121,111,117,32,102,111,114,32,108,111,111,\
        107,105,110,103,32,116,104,101,32,111,116,104,101,114,32,119,\
        97,121,32,111,110,32,116,104,101,32,105,110,99,114,101,97,115,\
        101,100,32,108,101,118,101,108,115,32,111,102,32,116,111,120,\
        105,99,32,99,104,101,109,105,99,97,108,115,32,105,110,32,116,104,\
        101,32,114,105,118,101,114,32,114,117,110,110,105,110,103,32,97,\
        108,111,110,103,115,105]

out_str = ''

for i in result3:
    if i in numbers:
        idx = numbers.index(i)
        out_str += letters[idx]
print out_str

Why do all the extra work, if Python can do it for you?

result3=[
83,97,109,117,101,108,32,83,109,105,116,104,13,10,13,10,84,
104,97,110,107,32,121,111,117,32,102,111,114,32,108,111,111,
107,105,110,103,32,116,104,101,32,111,116,104,101,114,32,119,
97,121,32,111,110,32,116,104,101,32,105,110,99,114,101,97,115,
101,100,32,108,101,118,101,108,115,32,111,102,32,116,111,120,
105,99,32,99,104,101,109,105,99,97,108,115,32,105,110,32,116,104,
101,32,114,105,118,101,114,32,114,117,110,110,105,110,103,32,97,
108,111,110,103,115,105,100,101,32,109,121,32,104,111,109,101]

# show the text hidden in the list of ascii values
print "".join([chr(x) for x in result3])

I know, I know, not as much fun as the long way!

commented: Sexy, simple method +1

Interesting. Thanks for all the help. I just started learning python about a week ago so I appreciate any and all help. Looks like I'll probably want to continue reading all those python tutorials. ;)

I want to refer back to the previous values without having to list all of them out again

To answer your original question, you can copy a list with

orig_numbers_list=[10,13,32]
for j in range(48, 123):
   orig_numbers_list.append(j)

numbers = orig_numbers_list[:]
print "orig =", orig_numbers_list

del numbers[0]
del numbers[5]
print "\n\ncopy =", numbers

Note the "[:]". The statement "numbers = orig_numbers_list" would yield a pointer to the original list, not a copy, so changing one list would also change the other since they would both point to the same list, so "[:]" identifies a copy operation.

That makes sense.

Note the "[:]". The statement "numbers = orig_numbers_list" would yield a pointer to the original list, not a copy, so changing one list would also change the other since they would both point to the same list, so "[:]" identifies a copy operation.

To technically answer my question, this answer was all I needed, but I appreciate all the replies that I got. Everything I learn is a benefit to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.