Tuple
Creating a Tuple
t = ([10, 12], "math", 2022.01.01T09:10:01, 97c, 22l, 3.5f)
type(t)
Output: tuple
Accessing Tuple Items
-
Access a tuple item by referring to the position.
t[3]
Output: 'a'
t[-2]
Output: 22
-
Access tuple items by slicing. Note that the step parameter cannot be specified in Python Parser.
t[0:4]
Output: ([10, 12], 'math', 2022.01.01T09:10:01, a)
t[4:]
Output: (22, 3.5)
Updating a Tuple
A tuple is an immutable sequence whose items cannot be changed, removed, or added. It is infeasible to update a tuple directly through indices. However, you can modify the mutable sequences in a tuple by assigning the mutable sequence to a new variable and updating the value of this variable.
tmp = t[0]
tmp[0]=1
t
// not support: t[0][0]=1
Output: ([1, 12], 'math', 2022.01.01T09:10:01, a, 22, 3.5)
Getting Available Attributes and Methods of a Tuple
dir(s)
Output: ['__add__', '__at__', '__dir__', '__eq__', '__iadd__', '__imultiply__', '__init__', '__iter__', '__len__', '__multiply__', '__ne__', '__repr__', '__req__', '__rne__', '__str__', 'toddb']
toddb
is a unique method in Python Parser, which supports converting
a Python Parser tuple into a DolphinDB tuple.
s = (1, 2, 3)
type(s.toddb())
Output: dolphindb.VECTOR.ANY
Operators Supported by a Tuple
Operator |
Usage |
Expression |
---|---|---|
* | Duplicate a tuple | (1, 2, 3) * 3 |
+ | Join tuples | (1, 2022.01M, 3) + (4, 5, "banana") |