0

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.

2
Contributors
2
Replies
4
Views
9 Years
Discussion Span
Last Post by OLer94
0

Try append instead of add. Any tutorial or on-line book can help you with lists and how to add/append items.

0

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 :$

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.