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 atuplelist
is populated with a list of tuples, theselect
function on this class efficiently selects tuples whose values match specified values in specified tuple fields. To give an example, the statementl.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 existingtuplelist
, or the+
operator to concatenate a pair oftuplelist
objects. You can also call theappend
,extend
,insert
,pop
, andremove
functions.To access the members of a
tuplelist
, you also use standard list functions. For example,l[0]
returns the first member of atuplelist
, whilel[0:10]
returns atuplelist
containing the first ten members. You can also uselen(l)
to query the length of a list.Note that
tuplelist
objects build and maintain a set of internal data structures to support efficientselect
operations. If you wish to reclaim the storage associated with these data structures, you can call theclean
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 atuplelist
, 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 thetuplelist
. It only affects the memory used and the performance of later calls toselect
.- 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")