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: -
str= ""
-
str=raw_input("Random gibberish")
-
a=[random numbers]
-
-
if str in a:
-
print "Random gibberish"
-
del str in a # As in "I want the number he just typed to be removed from the list"
-
-
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...
6 1384
The string "1" is not equal to the integer "1". -
>>> lst = [2,1,5,2]
-
>>> num = "1"
-
>>> num in lst
-
False
-
>>> num = int("1")
-
>>> num in lst
-
True
-
>>> lst.index(num)
-
1
-
>>> dir(lst)
-
['__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']
-
>>> print lst.remove.__doc__
-
L.remove(value) -- remove first occurrence of value
-
>>> lst.remove(num)
-
>>> lst
-
[2, 5, 2]
-
One tip is to lern how to use doc strings and the dir function.
The string "1" is not equal to the integer "1". -
>>> lst = [2,1,5,2]
-
>>> num = "1"
-
>>> num in lst
-
False
-
>>> num = int("1")
-
>>> num in lst
-
True
-
>>> lst.index(num)
-
1
-
>>> dir(lst)
-
['__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']
-
>>> print lst.remove.__doc__
-
L.remove(value) -- remove first occurrence of value
-
>>> lst.remove(num)
-
>>> lst
-
[2, 5, 2]
-
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?
simply do: -
print watheveryouwattoknowabut.__doc__
-
that is <dot><underscor e><underscore>d oc<underscore>< underscore>
exaple: - >>> print dir.__doc__
-
dir([object]) -> list of strings
-
-
Return an alphabetized list of names comprising (some of) the attributes
-
of the given object, and of attributes reachable from it:
-
-
No argument: the names in the current scope.
-
Module object: the module attributes.
-
Type or class object: its attributes, and recursively the attributes of
-
its bases.
-
Otherwise: its attributes, its class's attributes, and recursively the
-
attributes of its class's base classes.
-
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: -
str= ""
-
str=raw_input("Random gibberish")
-
a=[random numbers]
-
-
if str in a:
-
print "Random gibberish"
-
del str in a # As in "I want the number he just typed to be removed from the list"
-
-
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: - >>> numList = [random.randint(0,9) for _ in range(10)]
-
>>> numList
-
[2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
-
>>> n = int(raw_input('Enter a number'))
-
>>> n
-
2
-
>>> while True:
-
... if n in numList:
-
... numList.remove(2)
-
... else:
-
... break
-
...
-
>>> numList
-
[5, 8, 3, 4, 5, 9]
-
>>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
-
>>> numList.remove(2)
-
>>> numList
-
[5, 8, 3, 2, 2, 4, 2, 5, 9]
-
>>>
OR - >>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
-
>>> while True:
-
... try: numList.remove(n)
-
... except: break
-
...
-
>>> numList
-
[5, 8, 3, 4, 5, 9]
-
>>>
Do not use a Python built-in function name for a variable (str). Maybe this will help: - >>> numList = [random.randint(0,9) for _ in range(10)]
-
>>> numList
-
[2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
-
>>> n = int(raw_input('Enter a number'))
-
>>> n
-
2
-
>>> while True:
-
... if n in numList:
-
... numList.remove(2)
-
... else:
-
... break
-
...
-
>>> numList
-
[5, 8, 3, 4, 5, 9]
-
>>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
-
>>> numList.remove(2)
-
>>> numList
-
[5, 8, 3, 2, 2, 4, 2, 5, 9]
-
>>>
OR - >>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
-
>>> while True:
-
... try: numList.remove(n)
-
... except: break
-
...
-
>>> numList
-
[5, 8, 3, 4, 5, 9]
-
>>>
OR -
while n in numList: numList.remove(n)
-
God night. 2.21AM now.
Signing off.
-
while n in numList: numList.remove(n)
-
.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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"))
|
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...
|
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>
| |
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?
...
|
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
|
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
|
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?
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |