Why is this not working?

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

Join Date: Sep 2007
Posts: 7
Reputation: OLer94 is an unknown quantity at this point 
Solved Threads: 0
OLer94 OLer94 is offline Offline
Newbie Poster

Why is this not working?

 
0
  #1
Sep 16th, 2007
Im trying to figure out how to compute a product of ordered pairs. So if I have two sets (1,3) and (2,4), I want to generate the following tuples (1,2), (1,4), (3,2), (3,4).

Now I wrote the following

def orderedproduct(set1, set2):
op=[set()]
for x in set1:
for y in set2:
op.add((x,y))
return op


Now, python has a problem with the second to last line because everytime I make two sets and run the program, it says that AttributeError: 'list' object has no attribute 'add'

Clearly something is breaking down with that line, but is it the line itself or is there something Im not seeing in the program?

Edit: Im not sure why my tabbing isnt working here but there should be 1 tab on the 2nd and 3rd line, 2 tabs on the 4th, 3 tabs on the 5th, 1 tab on the 6th.
Last edited by OLer94; Sep 16th, 2007 at 12:51 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Why is this not working?

 
0
  #2
Sep 16th, 2007
Try append instead of add. Any tutorial or on-line book can help you with lists and how to add/append items.
Last edited by woooee; Sep 16th, 2007 at 1:08 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 7
Reputation: OLer94 is an unknown quantity at this point 
Solved Threads: 0
OLer94 OLer94 is offline Offline
Newbie Poster

Re: Why is this not working?

 
0
  #3
Sep 16th, 2007
Originally Posted by woooee View Post
Try append instead of add. Any tutorial or on-line book can help you with lists and how to add/append items.
Woops, I missed that! It works now. Thanks for tip. Sometimes these things are staring you right in the face
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


Views: 904 | Replies: 2
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC