473,800 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

52 New Member
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 1384
Smygis
126 New Member
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
BurnTard
52 New Member
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 New Member
simply do:
Expand|Select|Wrap|Line Numbers
  1. print watheveryouwattoknowabut.__doc__
  2.  
that is <dot><underscor e><underscore>d oc<underscore>< underscore>
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 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Contributor
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
2365
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 inherited and the client is a happy bunny. This was my first crash course outing with PHP, but with basic PERL experience found it fairly easy to pick up and used the code
42
2764
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 a tuple? I understand that a tuple is immutable and a list is mutable but there has to be more to it than just that. Everything I tried with a list worked the same with a tuple. So, what's the difference and why choose one over the other? Jeff
18
2165
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 words comming from the customer. The file is presorted. So here it goes: lstdict = map(lambda x: x.lower().strip(), file("D:\\CommonDictionary.txt"))
7
4833
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 ordered by my instructor and she does have her reasons so I have to use char*. So there is alot of c in the code as well Anyways, I have a linked list of linked lists of a class we defined, I need to make all this into a char*, I know that I...
9
3753
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? Here is an example of just such a usage: <dl class="faq"> <di>
41
3969
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 Y contains all the elements that are B but not in A. Z will then have the elements that are in both A and B. One way of doing this is of course to iterate throug the lists and compare each of the element, but is there a more efficient way? ...
3
472
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 your assitance in helping me solve this problem. Information: Create 4 double linked lists as follows: (a) A double linked list called NAMES which will contain all C like
10
1763
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 match a certain criterion). Changes to the filter are instantly reflected in the underlying list. Clear enough? Ok, so I figured that this is generic enough to be found in some standard module already (I've had this often enough: Painfully
51
8658
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 for linked lists, I couldn't find one. I read something about Mcilroy's "Optimistic Merge Sort" and studied some implementation, but they were for arrays. Does anybody know if Mcilroys optimization is applicable to truly linked lists at all?
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.