Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How about

sage: initial_list = [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3),\
sage:                 (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
sage: sublists = []
sage: for product in list(set([a*b for a,b in initial_list])) :
sage:     sublists.append([pair for pair in initial_list if pair[0]*pair[1] == product])

Or, maybe, as a dictionary:

sage: initial_list = [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3),\
sage:                 (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
sage: products = list(set([a*b for a,b in initial_list]))
sage: sublists = dict([[product,[]] for product in products])
sage: for pair in initial_list :
sage:     product = pair[0]*pair[1]
sage:     sublists[product].append(pair)

Cheers!

How about

sage: initial_list = [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3),\
sage:                 (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
sage: sublists = []
sage: for product in list(set([a*b for a,b in initial_list])) :
sage:     sublists.append([pair for pair in initial_list if pair[0]*pair[1] == product])

Or, maybe, as a dictionary:

sage: initial_list = [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3),\
sage:                 (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
sage: products = list(set([a*b for a,b in initial_list]))
sage: sublists = dict([[product,[]] for product in products])
sage: for pair in initial_list :
sage:     product = pair[0]*pair[1]
sage:     sublists[product].append(pair)

Cheers!

EDIT:

Slightly faster method (only multiplying once):

sage: initial_list = [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3),\
sage:                 (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
sage: sublists = dict()
sage: for pair in initial_list :
sage:     product = pair[0]*pair[1]
sage:     if not(product in sublists.keys()) :
sage:         sublists[product] = []
sage:     sublists[product].append(pair)

How about

sage: initial_list = [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3),\
sage:                 (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
sage: sublists = []
sage: for product in list(set([a*b for a,b in initial_list])) :
sage:     sublists.append([pair for pair in initial_list if pair[0]*pair[1] == product])

Or, maybe, as a dictionary:

sage: initial_list = [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3),\
sage:                 (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
sage: products = list(set([a*b for a,b in initial_list]))
sage: sublists = dict([[product,[]] for product in products])
sage: for pair in initial_list :
sage:     product = pair[0]*pair[1]
sage:     sublists[product].append(pair)

EDIT:

Slightly faster method (only multiplying once):

sage: initial_list = [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3),\
sage:                 (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
sage: sublists = dict()
sage: for pair in initial_list :
sage:     product = pair[0]*pair[1]
sage:     if not(product in sublists.keys()) :
sage:         sublists[product] = []
sage:     sublists[product].append(pair)

EDIT:

Even in a single line :)

sage: sublists = dict([[product,[pair for pair in initial_list if pair[0]*pair[1] == product]] for product in list(set([a*b for a,b in initial_list]))])

Yeah, I am having fun!

How about

sage: initial_list = [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3),\
sage:                 (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
sage: sublists = []
sage: for product in list(set([a*b for a,b in initial_list])) :
sage:     sublists.append([pair for pair in initial_list if pair[0]*pair[1] == product])

Or, maybe, as a dictionary:dictionary, with the products as keys:

sage: initial_list = [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3),\
sage:                 (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
sage: products = list(set([a*b for a,b in initial_list]))
sage: sublists = dict([[product,[]] for product in products])
sage: for pair in initial_list :
sage:     product = pair[0]*pair[1]
sage:     sublists[product].append(pair)

EDIT:

Slightly faster method (only multiplying once):

sage: initial_list = [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3),\
sage:                 (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
sage: sublists = dict()
sage: for pair in initial_list :
sage:     product = pair[0]*pair[1]
sage:     if not(product in sublists.keys()) :
sage:         sublists[product] = []
sage:     sublists[product].append(pair)

EDIT:

Even in a single line :)

sage: sublists = dict([[product,[pair for pair in initial_list if pair[0]*pair[1] == product]] for product in list(set([a*b for a,b in initial_list]))])

Yeah, I am having fun!