Replacing list items with other list

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 33
Reputation: jworld2 is an unknown quantity at this point 
Solved Threads: 0
jworld2 jworld2 is offline Offline
Light Poster

Replacing list items with other list

 
0
  #1
Sep 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Replacing list items with other list

 
0
  #2
Sep 26th, 2008
PSST! Secret:
  1. >>> import string
  2. >>> string.letters
  3. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  4. >>> string.digits
  5. '0123456789'

And PSST!!! To turn them into lists use list comprehension:
  1. >>> [ lett for lett in string.letters ]
  2. ['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']
  3. >>> [ dig for dig in string.digits ]
  4. ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  5. >>>
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Replacing list items with other list

 
0
  #3
Sep 26th, 2008
Oh!! also for the ascii values use another string comprehension!!!

  1. >>> [ ord( lett ) for lett in string.letters ]
  2. [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
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 33
Reputation: jworld2 is an unknown quantity at this point 
Solved Threads: 0
jworld2 jworld2 is offline Offline
Light Poster

Re: Replacing list items with other list

 
0
  #4
Sep 26th, 2008
Thank you. I will give this a try and see what I get.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Replacing list items with other list

 
0
  #5
Sep 26th, 2008
Not to be picky, but an iterable can be converted to a list with the built-in function list.
  1. >>> import string
  2. >>> list(string.letters)
  3. ['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']
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Replacing list items with other list

 
0
  #6
Sep 26th, 2008
Yar. I actually looked at what your code does and it is extremely over complicating the matter... here's a much simpler method:

  1. import string
  2.  
  3. letters = list(string.letters) + list(string.digits) + ['\n','\r',' ']
  4. letters.sort()
  5. numbers = [ ord(i) for i in letters ]
  6.  
  7. # Ascii values in the following list
  8. result3=[83,97,109,117,101,108,32,83,109,105,116,104,13,10,13,10,84,\
  9. 104,97,110,107,32,121,111,117,32,102,111,114,32,108,111,111,\
  10. 107,105,110,103,32,116,104,101,32,111,116,104,101,114,32,119,\
  11. 97,121,32,111,110,32,116,104,101,32,105,110,99,114,101,97,115,\
  12. 101,100,32,108,101,118,101,108,115,32,111,102,32,116,111,120,\
  13. 105,99,32,99,104,101,109,105,99,97,108,115,32,105,110,32,116,104,\
  14. 101,32,114,105,118,101,114,32,114,117,110,110,105,110,103,32,97,\
  15. 108,111,110,103,115,105]
  16.  
  17. out_str = ''
  18.  
  19. for i in result3:
  20. if i in numbers:
  21. idx = numbers.index(i)
  22. out_str += letters[idx]
  23. print out_str
Last edited by jlm699; Sep 26th, 2008 at 12:41 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Replacing list items with other list

 
1
  #7
Sep 26th, 2008
Why do all the extra work, if Python can do it for you?
  1. result3=[
  2. 83,97,109,117,101,108,32,83,109,105,116,104,13,10,13,10,84,
  3. 104,97,110,107,32,121,111,117,32,102,111,114,32,108,111,111,
  4. 107,105,110,103,32,116,104,101,32,111,116,104,101,114,32,119,
  5. 97,121,32,111,110,32,116,104,101,32,105,110,99,114,101,97,115,
  6. 101,100,32,108,101,118,101,108,115,32,111,102,32,116,111,120,
  7. 105,99,32,99,104,101,109,105,99,97,108,115,32,105,110,32,116,104,
  8. 101,32,114,105,118,101,114,32,114,117,110,110,105,110,103,32,97,
  9. 108,111,110,103,115,105,100,101,32,109,121,32,104,111,109,101]
  10.  
  11. # show the text hidden in the list of ascii values
  12. print "".join([chr(x) for x in result3])
I know, I know, not as much fun as the long way!
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 33
Reputation: jworld2 is an unknown quantity at this point 
Solved Threads: 0
jworld2 jworld2 is offline Offline
Light Poster

Re: Replacing list items with other list

 
0
  #8
Sep 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,070
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is online now Online
Veteran Poster

Re: Replacing list items with other list

 
0
  #9
Sep 26th, 2008
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
  1. orig_numbers_list=[10,13,32]
  2. for j in range(48, 123):
  3. orig_numbers_list.append(j)
  4.  
  5. numbers = orig_numbers_list[:]
  6. print "orig =", orig_numbers_list
  7.  
  8. del numbers[0]
  9. del numbers[5]
  10. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 33
Reputation: jworld2 is an unknown quantity at this point 
Solved Threads: 0
jworld2 jworld2 is offline Offline
Light Poster

Re: Replacing list items with other list

 
0
  #10
Sep 26th, 2008
That makes sense.
Impossible Physics
Graphics and Imaging
http://www.impossiblephysics.com
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1634 | Replies: 10
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC