Seq¶
Bases: BaseAgg[T], BaseEager[T]
pyochain.Seq represent an in memory collection.
Provides a subset of pyochain.Iter methods with eager evaluation, and is the return type of pyochain.Iter.collect().
Source code in src/pyochain/_iter/_main.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
__init__ ¶
__init__(data: Collection[T]) -> None
Source code in src/pyochain/_iter/_main.py
217 218 | |
apply ¶
apply(
func: Callable[Concatenate[Iterable[T], P], Iterator[R]],
*args: P.args,
**kwargs: P.kwargs,
) -> Iter[R]
Apply a function to the underlying iterable and return an Iter of the result. Allow to pass user defined functions that transform the iterable while retaining the Iter wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[Concatenate[Iterable[T], P], Iterator[R]]
|
Function to apply to the underlying iterable. |
required |
*args
|
P.args
|
Positional arguments to pass to the function. |
()
|
**kwargs
|
P.kwargs
|
Keyword arguments to pass to the function. |
{}
|
Example:
>>> import pyochain as pc
>>> def double(data: Iterable[int]) -> Iterator[int]:
... return (x * 2 for x in data)
>>> pc.Iter.from_([1, 2, 3]).apply(double).into(list)
[2, 4, 6]
Source code in src/pyochain/_core/_main.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
argmax ¶
argmax(key: Callable[[T], U] | None = None) -> int
Index of the first occurrence of a maximum value in an iterable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Callable[[T], U] | None
|
Optional function to determine the value for comparison. |
None
|
>>> import pyochain as pc
>>> pc.Iter.from_("abcdefghabcd").argmax()
7
>>> pc.Iter.from_([0, 1, 2, 3, 3, 2, 1, 0]).argmax()
3
>>> models = pc.Iter.from_(["svm", "random forest", "knn", "naïve bayes"])
>>> accuracy = pc.Seq([68, 61, 84, 72])
>>> # Most accurate model
>>> models.item(accuracy.argmax())
'knn'
>>>
>>> # Best accuracy
>>> accuracy.into(max)
84
Source code in src/pyochain/_iter/_aggregations.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
argmin ¶
argmin(key: Callable[[T], U] | None = None) -> int
Index of the first occurrence of a minimum value in an iterable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Callable[[T], U] | None
|
Optional function to determine the value for comparison. |
None
|
>>> import pyochain as pc
>>> pc.Iter.from_("efghabcdijkl").argmin()
4
>>> pc.Iter.from_([3, 2, 1, 0, 4, 2, 1, 0]).argmin()
3
For example, look up a label corresponding to the position of a value that minimizes a cost function:
>>> def cost(x):
... "Days for a wound to heal given a subject's age."
... return x**2 - 20 * x + 150
>>> labels = pc.Iter.from_(["homer", "marge", "bart", "lisa", "maggie"])
>>> ages = pc.Seq([35, 30, 10, 9, 1])
>>> # Fastest healing family member
>>> labels.item(ages.argmin(key=cost))
'bart'
>>> # Age with fastest healing
>>> ages.into(min, key=cost)
10
Source code in src/pyochain/_iter/_aggregations.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
collect ¶
collect(factory: Callable[[Iterable[T]], Collection[T]] = list) -> Seq[T]
Collect the elements into a sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factory
|
Callable[[Iterable[T]], Collection[T]]
|
A callable that takes an iterable and returns a collection. Defaults to list. |
list
|
Example:
>>> import pyochain as pc
>>> pc.Iter.from_(range(5)).collect().unwrap()
[0, 1, 2, 3, 4]
Source code in src/pyochain/_core/_main.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
combination_index ¶
combination_index(r: Iterable[T]) -> int
Equivalent to list(combinations(iterable, r)).index(element).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
Iterable[T]
|
The combination to find the index of. |
required |
The subsequences of iterable that are of length r can be ordered lexicographically.
combination_index computes the index of the first element, without computing the previous combinations.
ValueError will be raised if the given element isn't one of the combinations of iterable.
>>> import pyochain as pc
>>> pc.Iter.from_("abcdefg").combination_index("adf")
10
Source code in src/pyochain/_iter/_aggregations.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
count ¶
count() -> int
Return the length of the sequence. Like the builtin len but works on lazy sequences.
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2]).count()
2
Source code in src/pyochain/_iter/_aggregations.py
131 132 133 134 135 136 137 138 139 140 141 142 | |
diff_symmetric ¶
diff_symmetric(*others: Iterable[T]) -> Seq[T]
Return the symmetric difference (XOR) of this iterable and 'others'.
Note
This method consumes inner data, unsorts it, and removes duplicates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*others
|
Iterable[T]
|
Other iterables to compute the symmetric difference with. |
()
|
Example:
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2, 2]).diff_symmetric([2, 3]).iter().sort().unwrap()
[1, 3]
>>> pc.Iter.from_([1, 2, 3]).diff_symmetric([3, 4, 5]).iter().sort().unwrap()
[1, 2, 4, 5]
Source code in src/pyochain/_iter/_eager.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
diff_unique ¶
diff_unique(*others: Iterable[T]) -> Seq[T]
Return the difference of this iterable and 'others'. (Elements in 'self' but not in 'others').
Note
This method consumes inner data, unsorts it, and removes duplicates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*others
|
Iterable[T]
|
Other iterables to subtract from this iterable. |
()
|
Example:
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2, 2]).diff_unique([2, 3]).unwrap()
{1}
Source code in src/pyochain/_iter/_eager.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
first ¶
first() -> T
Return the first element.
>>> import pyochain as pc
>>> pc.Iter.from_([9]).first()
9
Source code in src/pyochain/_iter/_aggregations.py
95 96 97 98 99 100 101 102 103 104 105 | |
intersection ¶
intersection(*others: Iterable[T]) -> Seq[T]
Return the elements common to this iterable and 'others'.
Note
This method consumes inner data, unsorts it, and removes duplicates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*others
|
Iterable[T]
|
Other iterables to intersect with. |
()
|
Example:
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2, 2]).intersection([2, 3], [2]).unwrap()
{2}
Source code in src/pyochain/_iter/_eager.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
into ¶
into(func: Callable[Concatenate[T, P], R], *args: P.args, **kwargs: P.kwargs) -> R
Pass the unwrapped underlying data into a function.
The result is not wrapped.
>>> import pyochain as pc
>>> pc.Iter.from_(range(5)).into(list)
[0, 1, 2, 3, 4]
Source code in src/pyochain/_core/_main.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
item ¶
item(index: int) -> T
Return item at index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
The index of the item to retrieve. |
required |
>>> import pyochain as pc
>>> pc.Iter.from_([10, 20]).item(1)
20
Source code in src/pyochain/_iter/_aggregations.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
iter ¶
iter() -> Iter[T]
Get an iterator over the sequence. Call this to switch to lazy evaluation.
Source code in src/pyochain/_iter/_main.py
220 221 222 223 224 225 | |
last ¶
last() -> T
Return the last element.
>>> import pyochain as pc
>>> pc.Iter.from_([7, 8, 9]).last()
9
Source code in src/pyochain/_iter/_aggregations.py
119 120 121 122 123 124 125 126 127 128 129 | |
max ¶
max() -> U
Return the maximum of the sequence.
>>> import pyochain as pc
>>> pc.Iter.from_([3, 1, 2]).max()
3
Source code in src/pyochain/_iter/_aggregations.py
249 250 251 252 253 254 255 256 257 258 259 | |
mean ¶
mean() -> float
Return the mean of the sequence.
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2, 3]).mean()
2
Source code in src/pyochain/_iter/_aggregations.py
261 262 263 264 265 266 267 268 269 270 271 | |
median ¶
median() -> float
Return the median of the sequence.
>>> import pyochain as pc
>>> pc.Iter.from_([1, 3, 2]).median()
2
Source code in src/pyochain/_iter/_aggregations.py
273 274 275 276 277 278 279 280 281 282 283 | |
min ¶
min() -> U
Return the minimum of the sequence.
>>> import pyochain as pc
>>> pc.Iter.from_([3, 1, 2]).min()
1
Source code in src/pyochain/_iter/_aggregations.py
237 238 239 240 241 242 243 244 245 246 247 | |
mode ¶
mode() -> U
Return the mode of the sequence.
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2, 2, 3]).mode()
2
Source code in src/pyochain/_iter/_aggregations.py
285 286 287 288 289 290 291 292 293 294 295 | |
most_common ¶
most_common(n: int | None = None) -> Seq[tuple[T, int]]
Return the n most common elements and their counts.
If n is None, then all elements are returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int | None
|
Number of most common elements to return. Defaults to None (all elements). |
None
|
Example:
>>> import pyochain as pc
>>> pc.Iter.from_([1, 1, 2, 3, 3, 3]).most_common(2).unwrap()
[(3, 3), (1, 2)]
Source code in src/pyochain/_iter/_eager.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
pipe ¶
pipe(func: Callable[Concatenate[Self, P], R], *args: P.args, **kwargs: P.kwargs) -> R
Pipe the instance in the function and return the result.
Source code in src/pyochain/_core/_main.py
13 14 15 16 17 18 19 20 | |
println ¶
println(pretty: bool = True) -> Self
Print the underlying data and return self for chaining.
Useful for debugging, simply insert .println() in the chain,
and then removing it will not affect the rest of the chain.
Source code in src/pyochain/_core/_main.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
reduce ¶
reduce(func: Callable[[T, T], T]) -> T
Apply a function of two arguments cumulatively to the items of an iterable, from left to right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[T, T], T]
|
Function to apply cumulatively to the items of the iterable. |
required |
This effectively reduces the iterable to a single value.
If initial is present, it is placed before the items of the iterable in the calculation.
It then serves as a default when the iterable is empty.
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2, 3]).reduce(lambda a, b: a + b)
6
Source code in src/pyochain/_iter/_aggregations.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
second ¶
second() -> T
Return the second element.
>>> import pyochain as pc
>>> pc.Iter.from_([9, 8]).second()
8
Source code in src/pyochain/_iter/_aggregations.py
107 108 109 110 111 112 113 114 115 116 117 | |
sort ¶
sort(reverse: bool = False, key: Callable[[U], Any] | None = None) -> Seq[U]
Sort the elements of the sequence.
Note
This method must consume the entire iterable to perform the sort. The result is a new iterable over the sorted sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reverse
|
bool
|
Whether to sort in descending order. Defaults to False. |
False
|
key
|
Callable[[U], Any] | None
|
Function to extract a comparison key from each element. Defaults to None. |
None
|
Example:
>>> import pyochain as pc
>>> pc.Iter.from_([3, 1, 2]).sort().into(list)
[1, 2, 3]
Source code in src/pyochain/_iter/_eager.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
stdev ¶
stdev() -> float
Return the standard deviation of the sequence.
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2, 3]).stdev()
1.0
Source code in src/pyochain/_iter/_aggregations.py
297 298 299 300 301 302 303 304 305 306 307 308 309 | |
sum ¶
sum() -> U | Literal[0]
Return the sum of the sequence.
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2, 3]).sum()
6
Source code in src/pyochain/_iter/_aggregations.py
225 226 227 228 229 230 231 232 233 234 235 | |
tail ¶
tail(n: int) -> Seq[T]
Return a tuple of the last n elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of elements to return. |
required |
Example:
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2, 3]).tail(2).unwrap()
(2, 3)
Source code in src/pyochain/_iter/_eager.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
top_n ¶
top_n(n: int, key: Callable[[T], Any] | None = None) -> Seq[T]
Return a tuple of the top-n items according to key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of top elements to return. |
required |
key
|
Callable[[T], Any] | None
|
Function to extract a comparison key from each element. Defaults to None. |
None
|
Example:
>>> import pyochain as pc
>>> pc.Iter.from_([1, 3, 2]).top_n(2).unwrap()
(3, 2)
Source code in src/pyochain/_iter/_eager.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
union ¶
union(*others: Iterable[T]) -> Seq[T]
Return the union of this iterable and 'others'.
Note
This method consumes inner data and removes duplicates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*others
|
Iterable[T]
|
Other iterables to include in the union. |
()
|
Example:
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2, 2]).union([2, 3], [4]).iter().sort().unwrap()
[1, 2, 3, 4]
Source code in src/pyochain/_iter/_eager.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
unwrap ¶
unwrap() -> T
Return the underlying data.
This is a terminal operation.
Source code in src/pyochain/_core/_main.py
61 62 63 64 65 66 67 | |
unzip ¶
unzip() -> Unzipped[U, V]
Converts an iterator of pairs into a pair of iterators.
Iter.unzip() consumes the iterator of pairs.
Returns an Unzipped NamedTuple, containing two iterators:
- one from the left elements of the pairs
- one from the right elements.
This function is, in some sense, the opposite of zip.
>>> import pyochain as pc >>> data = [(1, "a"), (2, "b"), (3, "c")] >>> unzipped = pc.Iter.from_(data).unzip() >>> unzipped.first.into(list) [1, 2, 3] >>> unzipped.second.into(list) ['a', 'b', 'c']
Source code in src/pyochain/_iter/_aggregations.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
variance ¶
variance() -> float
Return the variance of the sequence.
>>> import pyochain as pc
>>> pc.Iter.from_([1, 2, 3, 7, 8]).variance()
9.7
Source code in src/pyochain/_iter/_aggregations.py
311 312 313 314 315 316 317 318 319 320 321 322 323 | |