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 1372
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><underscore><underscore>doc<underscore><under score>
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
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: -
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...
|
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...
|
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...
|
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...
|
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?
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |