simple question

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2009
Posts: 68
Reputation: max.yevs is an unknown quantity at this point 
Solved Threads: 0
max.yevs max.yevs is offline Offline
Junior Poster in Training

simple question

 
0
  #1
Jun 8th, 2009
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.

  1. >>>a = [1,2,3,4,5,6,7,8,9,10,10,10]
  2. >>>b = input ("Enter numbers: ")
  3. [3,4,10]
  4. >>>#something here
  5. >>>print (a)
  6. [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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: simple question

 
0
  #2
Jun 8th, 2009
How about:
  1. for number in b:
  2. if number in a:
  3. 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: simple question

 
0
  #3
Jun 8th, 2009
Use simple boolean statements.
Here is my python interaction:
  1. >>> a = [1,2,3,4,5,6,7,8,9,10,10,10]
  2. >>> b = list(input ("Enter numbers: "))
  3. Enter numbers: [3,4,10]
  4. >>> a= [x for x in a if x not in b]
  5. >>> a
  6. [1, 2, 5, 6, 7, 8, 9]
  7. >>>
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: simple question

 
0
  #4
Jun 8th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 68
Reputation: max.yevs is an unknown quantity at this point 
Solved Threads: 0
max.yevs max.yevs is offline Offline
Junior Poster in Training

Re: simple question

 
0
  #5
Jun 8th, 2009
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
  1. >>>a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10]
  2. >>>b = [3,4,10]
  3. >>>for number in b:
  4. >>> if number in a:
  5. >>> a.remove(number)
  6. >>>print (a)
  7. [1,2,5,6,7,8,9,10,10]

but i can't find a way for the user to input it
  1. >>>a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10]
  2. >>>b = input("enter numbers: ")
  3. [3,4,10]
  4. b= list(b) #i would think this should convert it from string to list but it doesn't
  5. >>>for number in b:
  6. >>> if number in a:
  7. >>> a.remove(number)
  8. >>>print (a)
  9. [1,2,5,6,7,8,9,10,10]
if i say print (b) it does not define b as [3,4] but rather as '[3, 4]'
Last edited by max.yevs; Jun 8th, 2009 at 3:38 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: simple question

 
0
  #6
Jun 8th, 2009
If you want, you can have it take input and split it at every space, or comma, etc:
  1. b = raw_input("Enter numbers separated by a space: ").split(" ")
It works like this:
  1. >>>Enter numbers separated by a space: 3 4 10
  2. >>>b
  3. >>>[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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 68
Reputation: max.yevs is an unknown quantity at this point 
Solved Threads: 0
max.yevs max.yevs is offline Offline
Junior Poster in Training

Re: simple question

 
0
  #7
Jun 8th, 2009
that's exactly what i'm looking for shadwickman... but when i do it it doesn't work...

  1. b = raw_input("Enter numbers separated by a space: ").split(" ")

what i get is
  1. Enter numbers separated by a space: 3 4 5
  2. >>> b
  3. ['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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: simple question

 
0
  #8
Jun 8th, 2009
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.
  1. b = raw_input("Enter numbers separated by a space: ").split(" ")
  2. for i, n in enumerate(b):
  3. b[i] = int(n)
  4.  
  5. # can also be written as:
  6. # 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.
  1. while 1:
  2. end = True # end loop unless error
  3. b = raw_input("Enter numbers separated by a space: ").split(" ")
  4. for i, n in enumerate(b):
  5. try:
  6. b[i] = int(n) # will cause error if i can't be converted to int
  7. except:
  8. print "Non-numerical input found. Only enter numbers!"
  9. end = False
  10. if end:
  11. 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 68
Reputation: max.yevs is an unknown quantity at this point 
Solved Threads: 0
max.yevs max.yevs is offline Offline
Junior Poster in Training

Re: simple question

 
0
  #9
Jun 8th, 2009
thanks, that first one works perfectly!

not sure how it works but i need digit only so its perfect.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: simple question

 
1
  #10
Jun 8th, 2009
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!
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC