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.

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.