473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
6 1372
Smygis
126 100+
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
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
126 100+
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
2,851 Expert Mod 2GB
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
126 100+
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
ghostdog74
511 Expert 256MB
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

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

Similar topics

9
by: Dave Smithz | last post by:
Hi, Summary: Best way to divide out the HTML and PHP in some code I inherited. How can I keep the HTML files separate? Full Details: I've now been working two weeks on PHP / MySQL code I...
42
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or...
18
by: Elbert Lev | last post by:
Hi, all! Here is the problem: I have a file, which contains a common dictionary - one word per line (appr. 700KB and 70000 words). I have to read it in memory for future "spell checking" of the...
7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
3
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for...
10
by: Wildemar Wildenburger | last post by:
Hi there :) I don't know how else to call what I'm currently implementing: An object that behaves like a list but doesn't store it's own items but rather pulls them from a larger list (if they...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.