| | |
simple question
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 68
Reputation:
Solved Threads: 0
yet another ridiculously simple question, yet i feel compelled to ask because i've been stuck here for like an hour without it working
ok so say you have one list, a, and you need the user of the program to input a list of several more numbers, call that list b, then take out the numbers in b from a.
so i try a bunch of stuff from the tutorial in like 100 different ways: a = a - b, for i in b: a.remove(i), a.remove(b)... yet i always get an error
it works with sets but i can't use sets because they keep throwing out the nonunique numbers (so like if you have [10,10,10] it would just make it [10])
ok so say you have one list, a, and you need the user of the program to input a list of several more numbers, call that list b, then take out the numbers in b from a.
python Syntax (Toggle Plain Text)
>>>a = [1,2,3,4,5,6,7,8,9,10,10,10] >>>b = input ("Enter numbers: ") [3,4,10] >>>#something here >>>print (a) [1,2,5,6,7,8,9,10,10]
so i try a bunch of stuff from the tutorial in like 100 different ways: a = a - b, for i in b: a.remove(i), a.remove(b)... yet i always get an error
it works with sets but i can't use sets because they keep throwing out the nonunique numbers (so like if you have [10,10,10] it would just make it [10])
Last edited by max.yevs; Jun 8th, 2009 at 1:57 am.
How about:
That should go through each number in b and remove it from a if it appears in a...
python Syntax (Toggle Plain Text)
for number in b: if number in a: a.remove(number)
That should go through each number in b and remove it from a if it appears in a...
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
Use simple boolean statements.
Here is my python interaction:
Here is my python interaction:
python Syntax (Toggle Plain Text)
>>> a = [1,2,3,4,5,6,7,8,9,10,10,10] >>> b = list(input ("Enter numbers: ")) Enter numbers: [3,4,10] >>> a= [x for x in a if x not in b] >>> a [1, 2, 5, 6, 7, 8, 9] >>>
Last edited by siddhant3s; Jun 8th, 2009 at 3:20 am.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
siddhant3s, you may notice that your option has in fact removed the three 10s from list a, when you only input one 10. In the question, the other two 10s are left in list a after only one 10 is input.
Last edited by shadwickman; Jun 8th, 2009 at 3:26 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
•
•
Join Date: Apr 2009
Posts: 68
Reputation:
Solved Threads: 0
shadwick man's version works great, but i think my real problem is inputting the list
if the list is already defined it works great
but i can't find a way for the user to input it
if i say print (b) it does not define b as [3,4] but rather as '[3, 4]'
if the list is already defined it works great
python Syntax (Toggle Plain Text)
>>>a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10] >>>b = [3,4,10] >>>for number in b: >>> if number in a: >>> a.remove(number) >>>print (a) [1,2,5,6,7,8,9,10,10]
but i can't find a way for the user to input it
python Syntax (Toggle Plain Text)
>>>a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10] >>>b = input("enter numbers: ") [3,4,10] b= list(b) #i would think this should convert it from string to list but it doesn't >>>for number in b: >>> if number in a: >>> a.remove(number) >>>print (a) [1,2,5,6,7,8,9,10,10]
Last edited by max.yevs; Jun 8th, 2009 at 3:38 am.
If you want, you can have it take input and split it at every space, or comma, etc:
It works like this:
python Syntax (Toggle Plain Text)
b = raw_input("Enter numbers separated by a space: ").split(" ")
python Syntax (Toggle Plain Text)
>>>Enter numbers separated by a space: 3 4 10 >>>b >>>[3,4,10]
Last edited by shadwickman; Jun 8th, 2009 at 3:53 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
•
•
Join Date: Apr 2009
Posts: 68
Reputation:
Solved Threads: 0
that's exactly what i'm looking for shadwickman... but when i do it it doesn't work...
what i get is
it does not see the numbers as integers for some reason... and it does not remove them from list "a" either
python Syntax (Toggle Plain Text)
b = raw_input("Enter numbers separated by a space: ").split(" ")
what i get is
python Syntax (Toggle Plain Text)
Enter numbers separated by a space: 3 4 5 >>> b ['3', '4', '5']
it does not see the numbers as integers for some reason... and it does not remove them from list "a" either
Last edited by max.yevs; Jun 8th, 2009 at 3:54 am.
Oh wow, I'm sorry. I completely forgot about raw_input returning them as strings. You can run each index through the int() function, changing it to an integer, but a problem arises if someone enters something other than digits. Let me give two solutions...
This one is simple, yet throws an error if the user enters any non-digit characters.
This one loops if there are any non-digits, and handles the error they present.
This one is simple, yet throws an error if the user enters any non-digit characters.
python Syntax (Toggle Plain Text)
b = raw_input("Enter numbers separated by a space: ").split(" ") for i, n in enumerate(b): b[i] = int(n) # can also be written as: # b = [int(i) for i in raw_input("Enter numbers separated by a space: ").split(" ")]
This one loops if there are any non-digits, and handles the error they present.
python Syntax (Toggle Plain Text)
while 1: end = True # end loop unless error b = raw_input("Enter numbers separated by a space: ").split(" ") for i, n in enumerate(b): try: b[i] = int(n) # will cause error if i can't be converted to int except: print "Non-numerical input found. Only enter numbers!" end = False if end: break
Last edited by shadwickman; Jun 8th, 2009 at 4:04 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
All it does is split the input into a list and save it as b like we did before. As of then, we have a list of numbers, but they are actually strings (e.g. ['3', '4', '10']).
From there, we use a for loop to cycle though list b (i is the index number, and n is the index value from the enumerate function). All we do then is tell it to replace b[i] (the current index it's on) with the old value of it (the string, n) converted to an integer through the int function.
I hope that clarifies it a bit!
From there, we use a for loop to cycle though list b (i is the index number, and n is the index value from the enumerate function). All we do then is tell it to replace b[i] (the current index it's on) with the old value of it (the string, n) converted to an integer through the int function.
I hope that clarifies it a bit!
Last edited by shadwickman; Jun 8th, 2009 at 4:15 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
![]() |
Similar Threads
- help! simple question (Python)
- HI/ I have a simple question (Domains and DNS)
- Simple Map Question (C++)
- A simple question about Ram (Motherboards, CPUs and RAM)
- A Simple Question (Java)
- simple question (C++)
- Simple array question (C++)
- This has to be a simple question to answer. (Visual Basic 4 / 5 / 6)
- Simple question (Windows Servers and IIS)
Other Threads in the Python Forum
- Previous Thread: Web interface for server-client application
- Next Thread: Shut down/open file script?
| Thread Tools | Search this Thread |
Tag cloud for Python
address anydbm app beginner cipher client code conversion coordinates corners curves development dictionary dynamic examples excel feet file float font format ftp function generator getvalue gui handling homework images import input ip java keycontrol line linux list lists loan loop maintain maze microcontroller millimeter mouse mysqldb number numbers output parsing path permissions port prime programming projects py2exe pygame pymailer pyqt python queue random raw_input recursion recursive scrolledtext searchingfile shebang slicenotation socket split ssh string strings table terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables vigenere web windows wx.wizard wxpython xlwt





