| | |
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:
Solved Threads: 0
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.-
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.
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:
And PSST!!! To turn them into lists use list comprehension:
python Syntax (Toggle Plain Text)
>>> import string >>> string.letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.digits '0123456789'
And PSST!!! To turn them into lists use list comprehension:
python Syntax (Toggle Plain Text)
>>> [ 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!!!
python Syntax (Toggle Plain Text)
>>> [ 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
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
Not to be picky, but an iterable can be converted to a list with the built-in function list.
Python Syntax (Toggle Plain Text)
>>> 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:
python Syntax (Toggle Plain Text)
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
Last edited by jlm699; Sep 26th, 2008 at 12:41 pm.
Why do all the extra work, if Python can do it for you?
I know, I know, not as much fun as the long way!
python Syntax (Toggle Plain Text)
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])
drink her pretty
•
•
Join Date: Dec 2006
Posts: 1,070
Reputation:
Solved Threads: 299
•
•
•
•
I want to refer back to the previous values without having to list all of them out again
Python Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Replacing items in a string using a loop? (Python)
- The div method for columns doesn't always work (HTML and CSS)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (PHP)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (MySQL)
- Warning: mysql_num_rows(): (PHP)
- Simple Problem (Getting the Assigned Value within loop dynamically) (Shell Scripting)
- Source Code that don't work? (Java)
- Math/computerscience Log Problem- Helpppp (Computer Science)
- No mouse, no keyboard on multi Network XP (Windows NT / 2000 / XP)
Other Threads in the Python Forum
- Previous Thread: Need Python help please
- Next Thread: python roguelike
Views: 1634 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt apache application argv beginner binary book calculator change cipher code command converter dictionaries dictionary dynamic event examples excel file float format ftp function google gui hints homework import inches input java keyboard launcher line linux list lists loop maze microphone mouse movingimageswithpygame newb number numbers obexftp output parsing path permissions phonebook plugin port prime program programming projects py2exe pygame pyqt python random recursion recursive refresh remote scrolledtext search session signal simple ssh string strings strip table terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 valueerror variable verify vigenere windows wordgame wxpython xlwt






