gurobipy.tuplelist#

class tuplelist#

Gurobi tuple list. This is a sub-class of the Python list class that is designed to efficiently support a usage pattern that is quite common when building optimization models. In particular, if a tuplelist is populated with a list of tuples, the select function on this class efficiently selects tuples whose values match specified values in specified tuple fields. To give an example, the statement l.select(1, '*', 5) would select all member tuples whose first field is equal to ‘1’ and whose third field is equal to ‘5’. The '*' character is used as a wildcard to indicate that any value is acceptable in that field.

You generally build tuplelist objects in the same way you would build standard Python lists. For example, you can use the += operator to append a new list of items to an existing tuplelist, or the + operator to concatenate a pair of tuplelist objects. You can also call the append, extend, insert, pop, and remove functions.

To access the members of a tuplelist, you also use standard list functions. For example, l[0] returns the first member of a tuplelist, while l[0:10] returns a tuplelist containing the first ten members. You can also use len(l) to query the length of a list.

Note that tuplelist objects build and maintain a set of internal data structures to support efficient select operations. If you wish to reclaim the storage associated with these data structures, you can call the clean function.

A tuplelist is designed to store tuples containing scalar values (int, float, string, …). It may produce unpredictable results with other Python objects, such as tuples of tuples. Thus, you can store (1, 2.0, 'abc') in a tuplelist, but you shouldn’t store ((1, 2.0), 'abc').

tuplelist(list)#

tuplelist constructor.

Parameters:

list – Initial list of member tuples.

Returns:

A tuplelist object.

Example:
l = gp.tuplelist([(1,2), (1,3), (2,4)])
l = gp.tuplelist([('A', 'B', 'C'), ('A', 'C', 'D')])
select(pattern)#

Returns a tuplelist containing all member tuples that match the specified pattern. The pattern requires one argument for each field in the member tuple. A scalar argument must match the corresponding field exactly. A list argument matches if any list member matches the corresponding field. A '*' argument matches any value in the corresponding field.

Parameters:

pattern – Pattern to match for a member tuple.

Example:
l.select(1, 3, '*', 6)
l.select([1, 2], 3, '*', 6)
l.select('A', '*', 'C')
clean()#

Discards internal data structure associated with a tuplelist object. Note that calling this routine won’t affect the contents of the tuplelist. It only affects the memory used and the performance of later calls to select.

Example:
l.clean()
__contains__(val)#

Provides efficient support for the Python in operator.

Example:
if (1,2) in l:
  print("Tuple (1,2) is in tuplelist l")