944,129 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1069
  • Python RSS
Sep 16th, 2007
0

Why is this not working?

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
OLer94 is offline Offline
7 posts
since Sep 2007
Sep 16th, 2007
0

Re: Why is this not working?

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.
Reputation Points: 741
Solved Threads: 693
Nearly a Posting Maven
woooee is offline Offline
2,310 posts
since Dec 2006
Sep 16th, 2007
0

Re: Why is this not working?

Click to Expand / Collapse  Quote originally posted by woooee ...
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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
OLer94 is offline Offline
7 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Array Distances
Next Thread in Python Forum Timeline: quick question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC