By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Lists used to be so easy, now I can hardly change them.

P: 52
I can hardly solve the simplest thing without asking thescripts for help... Must be getting late. If I keep this up, I'll be an admin within the month.
This time, I have a list with six random numbers in it. Since the numbers are random, I don't know at which indexes they are. What I want to do is basically:

Expand|Select|Wrap|Line Numbers
  1. str= ""
  2. str=raw_input("Random gibberish")
  3. a=[random numbers]
  4.  
  5. if str in a:
  6.     print "Random gibberish"
  7.     del str in a # As in "I want the number he just typed to be removed from the list"
  8.  
  9.  
Which of course does not work. Python dislikes the "del str in a", of course, and I've been trying various things (basically just typed in english words and prayed) but to no avail. Does anyone have a smart way to do this? Try to keep it simple, mind...
Jun 1 '07 #1
Share this Question
Share on Google+
6 Replies


Smygis
100+
P: 126
The string "1" is not equal to the integer "1".

Expand|Select|Wrap|Line Numbers
  1. >>> lst = [2,1,5,2]
  2. >>> num = "1"
  3. >>> num in lst
  4. False
  5. >>> num = int("1")
  6. >>> num in lst
  7. True
  8. >>> lst.index(num)
  9. 1
  10. >>> dir(lst)
  11. ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
  12. >>> print lst.remove.__doc__
  13. L.remove(value) -- remove first occurrence of value
  14. >>> lst.remove(num)
  15. >>> lst
  16. [2, 5, 2]
  17.  
One tip is to lern how to use doc strings and the dir function.
Jun 1 '07 #2

P: 52
The string "1" is not equal to the integer "1".

Expand|Select|Wrap|Line Numbers
  1. >>> lst = [2,1,5,2]
  2. >>> num = "1"
  3. >>> num in lst
  4. False
  5. >>> num = int("1")
  6. >>> num in lst
  7. True
  8. >>> lst.index(num)
  9. 1
  10. >>> dir(lst)
  11. ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
  12. >>> print lst.remove.__doc__
  13. L.remove(value) -- remove first occurrence of value
  14. >>> lst.remove(num)
  15. >>> lst
  16. [2, 5, 2]
  17.  
One tip is to lern how to use doc strings and the dir function.
Do tell, how do I use doc strings and the dir function?
Jun 1 '07 #3

Smygis
100+
P: 126
simply do:
Expand|Select|Wrap|Line Numbers
  1. print watheveryouwattoknowabut.__doc__
  2.  
that is <dot><underscore><underscore>doc<underscore><under score>
exaple:
Expand|Select|Wrap|Line Numbers
  1. >>> print dir.__doc__
  2. dir([object]) -> list of strings
  3.  
  4. Return an alphabetized list of names comprising (some of) the attributes
  5. of the given object, and of attributes reachable from it:
  6.  
  7. No argument:  the names in the current scope.
  8. Module object:  the module attributes.
  9. Type or class object:  its attributes, and recursively the attributes of
  10.     its bases.
  11. Otherwise:  its attributes, its class's attributes, and recursively the
  12.     attributes of its class's base classes.
  13.  
Jun 2 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
I can hardly solve the simplest thing without asking thescripts for help... Must be getting late. If I keep this up, I'll be an admin within the month.
This time, I have a list with six random numbers in it. Since the numbers are random, I don't know at which indexes they are. What I want to do is basically:

Expand|Select|Wrap|Line Numbers
  1. str= ""
  2. str=raw_input("Random gibberish")
  3. a=[random numbers]
  4.  
  5. if str in a:
  6.     print "Random gibberish"
  7.     del str in a # As in "I want the number he just typed to be removed from the list"
  8.  
  9.  
Which of course does not work. Python dislikes the "del str in a", of course, and I've been trying various things (basically just typed in english words and prayed) but to no avail. Does anyone have a smart way to do this? Try to keep it simple, mind...
Do not use a Python built-in function name for a variable (str). Maybe this will help:
Expand|Select|Wrap|Line Numbers
  1. >>> numList = [random.randint(0,9) for _ in range(10)]
  2. >>> numList
  3. [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
  4. >>> n = int(raw_input('Enter a number'))
  5. >>> n
  6. 2
  7. >>> while True:
  8. ...     if n in numList:
  9. ...         numList.remove(2)
  10. ...     else:
  11. ...         break
  12. ...     
  13. >>> numList
  14. [5, 8, 3, 4, 5, 9]
  15. >>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
  16. >>> numList.remove(2)
  17. >>> numList
  18. [5, 8, 3, 2, 2, 4, 2, 5, 9]
  19. >>> 
OR
Expand|Select|Wrap|Line Numbers
  1. >>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
  2. >>> while True:
  3. ...     try: numList.remove(n)
  4. ...     except: break
  5. ...     
  6. >>> numList
  7. [5, 8, 3, 4, 5, 9]
  8. >>> 
Jun 2 '07 #5

Smygis
100+
P: 126
Do not use a Python built-in function name for a variable (str). Maybe this will help:
Expand|Select|Wrap|Line Numbers
  1. >>> numList = [random.randint(0,9) for _ in range(10)]
  2. >>> numList
  3. [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
  4. >>> n = int(raw_input('Enter a number'))
  5. >>> n
  6. 2
  7. >>> while True:
  8. ...     if n in numList:
  9. ...         numList.remove(2)
  10. ...     else:
  11. ...         break
  12. ...     
  13. >>> numList
  14. [5, 8, 3, 4, 5, 9]
  15. >>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
  16. >>> numList.remove(2)
  17. >>> numList
  18. [5, 8, 3, 2, 2, 4, 2, 5, 9]
  19. >>> 
OR
Expand|Select|Wrap|Line Numbers
  1. >>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
  2. >>> while True:
  3. ...     try: numList.remove(n)
  4. ...     except: break
  5. ...     
  6. >>> numList
  7. [5, 8, 3, 4, 5, 9]
  8. >>> 
OR
Expand|Select|Wrap|Line Numbers
  1. while n in numList: numList.remove(n)
  2.  
God night. 2.21AM now.
Signing off.
Jun 2 '07 #6

Expert 100+
P: 511
Expand|Select|Wrap|Line Numbers
  1. while n in numList: numList.remove(n)
  2.  
.
Minor difference between while 1 method and this method in general. if using "while 1" , in the loop, one can code for more than one condition to break.
Jun 2 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.