Tuple Functions¶
import introcs
The purpose of this module is to allow students to work with tuples without having to
understand method calls. It has the side effect of unifying tuple functionality with
string functionality (i.e. adding find
functions and handling index ranges in
the same way).
count_tup¶
- introcs.count_tup(tupl, value, start=None, end=None)¶
Counts the number of times
value
occurs intupl[start:end]
.Optional arguments start and end are interpreted as in slice notation.
- Parameters
tupl (
tuple
) – The tuple to searchvalue (
any
) – The value to countstart (
int
) – The start of the search rangeend (
int
) – The end of the search range
- Returns
The number of times
value
occurs intupl[start:end]
.- Return type
int
find_tup¶
- introcs.find_tup(tupl, value, start=None, end=None)¶
Finds the lowest index of
value
withintupl
in the range [start
,end
].Optional arguments
start
andend
are interpreted as in slice notation. However, the index returned is relative to the tuple and not the slicetupl[start:end]
. The function returns -1 ifvalue
is not found.Note: The
find_tup()
function should be used only if you need to know the position ofvalue
. To check ifvalue
is in the tuple, use the in operator:>>> >>> 1 in (1,2,3) True
- Parameters
tupl (
tuple
) – The tuple to searchvalue (
any
) – The value to search forstart (
int
) – The start of the search rangeend (
int
) – The end of the search range
- Returns
The lowest index of
value
withintupl
in the range [start
,end
].- Return type
int
rfind_tup¶
- introcs.rfind_tup(tupl, value, start=None, end=None)¶
Finds the highest index of
value
withintupl
in the range [start
,end
].Optional arguments
start
andend
are interpreted as in slice notation. However, the index returned is relative to the tuple and not the slicetupl[start:end]
. The function returns -1 ifvalue
is not found.- Parameters
tupl (
tuple
) – The tuple to searchvalue (
any
) – The value to search forstart (
int
) – The start of the search rangeend (
int
) – The end of the search range
- Returns
The highest index of
value
withintupl
in the range [start
,end
].- Return type
int
index_tup¶
- introcs.index_tup(tupl, value, start=None, end=None)¶
Finds the lowest index of
value
withintupl
in the range [start
,end
].Optional arguments
start
andend
are interpreted as in slice notation. However, the index returned is relative to the tuple and not the slicetupl[start:end]
.This function is like
find_tup()
, except that it raises aValueError
when the value is not found.- Parameters
tupl (
tuple
) – The tuple to searchvalue (
any
) – The value to search forstart (
int
) – The start of the search rangeend (
int
) – The end of the search range
- Returns
The lowest index of
value
withintupl
in the range [start
,end
].- Return type
int
rindex_tup¶
- introcs.rindex_tup(tupl, value, start=None, end=None)¶
Finds the highest index of
value
withintupl
in the range [start
,end
].Optional arguments
start
andend
are interpreted as in slice notation. However, the index returned is relative to the tuple and not the slicetupl[start:end]
.This function is like
rfind_tup()
, except that it raises aValueError
when the value is not found.- Parameters
tupl (
tuple
) – The tuple to searchvalue (
any
) – The value to search forstart (
int
) – The start of the search rangeend (
int
) – The end of the search range
- Returns
The highest index of
value
withintupl
in the range [start
,end
].- Return type
int
replace_tup¶
- introcs.replace_tup(tupl, old, new, count=- 1)¶
Creates a copy of
tupl
with all occurrences of valueold
replaced bynew
.Objects are replaced by value equality, not id equality (i.e.
==
notis
). If the optional argumentcount
is given, only the first count occurrences are replaced.- Parameters
tupl (
tuple
) – The tuple to copyold (
any
) – The old value to replacenew (
any
) – The new value to replace withcount (
int
) – The number of occurrences to replace
- Returns
A copy of
tupl
with all occurrences of valueold
replaced bynew
.- Return type
tuple
Contents: