Sequence¶
-
class
abjad.utilities.Sequence.
Sequence
(items=None)¶ Sequence.
Initializes sequence:
>>> abjad.sequence([1, 2, 3, 4, 5, 6]) Sequence([1, 2, 3, 4, 5, 6])
>>> expression = abjad.sequence() >>> expression([1, 2, 3, 4, 5, 6]) Sequence([1, 2, 3, 4, 5, 6])
Initializes and reverses sequence:
>>> sequence = abjad.sequence([1, 2, 3, 4, 5, 6]) >>> sequence.reverse() Sequence([6, 5, 4, 3, 2, 1])
>>> expression = abjad.sequence() >>> expression = expression.reverse() >>> expression([1, 2, 3, 4, 5, 6]) Sequence([6, 5, 4, 3, 2, 1])
Initializes, reverses and flattens sequence:
>>> sequence = abjad.sequence([1, 2, 3, [4, 5, [6]]]) >>> sequence = sequence.reverse() >>> sequence = sequence.flatten(depth=-1) >>> sequence Sequence([4, 5, 6, 3, 2, 1])
>>> expression = abjad.sequence() >>> expression = expression.reverse() >>> expression = expression.flatten(depth=-1) >>> expression([1, 2, 3, [4, 5, [6]]]) Sequence([4, 5, 6, 3, 2, 1])
Attributes Summary
__add__
Adds argument
to sequence.__eq__
Is true when argument
is a sequence with items equal to those of this sequence.__format__
Formats sequence. __getitem__
Gets item or slice identified by argument
.__hash__
Hashes sequence. __len__
Gets length of sequence. __radd__
Adds sequence to argument
.__repr__
Gets interpreter representation of sequence. filter
Filters sequence by predicate
.flatten
Flattens sequence. group_by
Groups sequence items by value of items. is_decreasing
Is true when sequence decreases. is_increasing
Is true when sequence increases. is_permutation
Is true when sequence is a permutation. is_repetition_free
Is true when sequence is repetition-free. items
Gets sequence items. join
Join subsequences in sequence
.map
Maps operand
to sequence items.nwise
Iterates sequence n
at a time.partition_by_counts
Partitions sequence by counts
.partition_by_ratio_of_lengths
Partitions sequence by ratio
of lengths.partition_by_ratio_of_weights
Partitions sequence by ratio of weights
.partition_by_weights
Partitions sequence by weights
exactly.permute
Permutes sequence by permutation
.remove
Removes items at indices
.remove_repeats
Removes repeats from sequence
.repeat
Repeats sequence. repeat_to_length
Repeats sequence to length
.repeat_to_weight
Repeats sequence to weight
.replace
Replaces items at indices
withnew_material
.retain
Retains items at indices
.retain_pattern
Retains items at indices matching pattern
.reverse
Reverses sequence. rotate
Rotates sequence by index n
.select
Selects sequence. sort
Sorts sequence. split
Splits sequence by weights
.sum
Sums sequence. sum_by_sign
Sums consecutive sequence items by sign
.truncate
Truncates sequence. weight
Gets weight. zip
Zips sequences in sequence. Special methods
-
__add__
(argument)¶ Adds
argument
to sequence.Adds tuple to sequence:
>>> abjad.sequence([1, 2, 3]) + (4, 5, 6) Sequence([1, 2, 3, 4, 5, 6])
Adds list to sequence:
>>> abjad.sequence([1, 2, 3]) + [4, 5, 6] Sequence([1, 2, 3, 4, 5, 6])
Adds sequence to sequence:
>>> sequence_1 = abjad.sequence([1, 2, 3]) >>> sequence_2 = abjad.sequence([4, 5, 6]) >>> sequence_1 + sequence_2 Sequence([1, 2, 3, 4, 5, 6])
>>> expression_1 = abjad.Expression(name='J') >>> expression_1 = expression_1.sequence() >>> expression_2 = abjad.Expression(name='K') >>> expression_2 = expression_2.sequence() >>> expression = expression_1 + expression_2
>>> expression([1, 2, 3], [4, 5, 6]) Sequence([1, 2, 3, 4, 5, 6])
>>> expression.get_string() 'J + K'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Reverses result:
>>> sequence_1 = abjad.sequence([1, 2, 3]) >>> sequence_2 = abjad.sequence([4, 5, 6]) >>> sequence = sequence_1 + sequence_2 >>> sequence.reverse() Sequence([6, 5, 4, 3, 2, 1])
>>> expression_1 = abjad.Expression(name='J') >>> expression_1 = expression_1.sequence() >>> expression_2 = abjad.Expression(name='K') >>> expression_2 = expression_2.sequence() >>> expression = expression_1 + expression_2 >>> expression = expression.reverse()
>>> expression([1, 2, 3], [4, 5, 6]) Sequence([6, 5, 4, 3, 2, 1])
>>> expression.get_string() 'R(J + K)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Returns new sequence.
-
(
AbjadValueObject
).__copy__
(*arguments)¶ Copies Abjad value object.
Returns new Abjad value object.
-
__eq__
(argument)¶ Is true when
argument
is a sequence with items equal to those of this sequence.Is true when
argument
is a sequence with items equal to those of this sequence:>>> abjad.sequence([1, 2, 3, 4, 5, 6]) == abjad.sequence([1, 2, 3, 4, 5, 6]) True
Is false when
argument
is not a sequence with items equal to those of this sequence:>>> abjad.sequence([1, 2, 3, 4, 5, 6]) == ([1, 2, 3, 4, 5, 6]) False
Returns true or false.
-
__format__
(format_specification='')¶ Formats sequence.
Formats sequence:
>>> abjad.f(abjad.sequence([1, 2, 3, 4, 5, 6])) Sequence([1, 2, 3, 4, 5, 6])
Formats expression:
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> abjad.f(expression) abjad.Expression( callbacks=[ abjad.Expression( evaluation_template='abjad.utilities.Sequence', is_initializer=True, string_template='{}', ), ], name='J', proxy_class=abjad.Sequence, )
Returns string.
-
__getitem__
(argument)¶ Gets item or slice identified by
argument
.Gets first item in sequence:
>>> sequence = abjad.sequence([1, 2, 3, 4, 5, 6])
>>> sequence[0] 1
Gets last item in sequence:
>>> sequence = abjad.sequence([1, 2, 3, 4, 5, 6])
>>> sequence[-1] 6
Gets slice from sequence:
>>> sequence = abjad.sequence([1, 2, 3, 4, 5, 6]) >>> sequence = sequence[:3]
>>> sequence Sequence([1, 2, 3])
Gets item in sequence and wraps result in new sequence:
>>> sequence = abjad.sequence([1, 2, 3, 4, 5, 6]) >>> sequence = abjad.sequence(sequence[0])
>>> sequence Sequence([1])
Gets slice from sequence and flattens slice:
>>> sequence = abjad.sequence([1, 2, [3, [4]], 5]) >>> sequence = sequence[:-1] >>> sequence = sequence.flatten(depth=-1)
>>> sequence Sequence([1, 2, 3, 4])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression[:-1] >>> expression = expression.flatten(depth=-1)
>>> expression([1, 2, [3, [4]], 5]) Sequence([1, 2, 3, 4])
>>> expression.get_string() 'flatten(J[:-1], depth=-1)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Returns item or new sequence.
-
__hash__
()¶ Hashes sequence.
Required to be explicitly redefined on Python 3 if __eq__ changes.
Returns integer.
-
__len__
()¶ Gets length of sequence.
Gets length of sequence:
>>> len(abjad.sequence([1, 2, 3, 4, 5, 6])) 6
Gets length of sequence:
>>> len(abjad.sequence('text')) 4
Returns nonnegative integer.
-
__radd__
(argument)¶ Adds sequence to
argument
.Adds sequence to tuple:
>>> (1, 2, 3) + abjad.sequence([4, 5, 6]) Sequence([1, 2, 3, 4, 5, 6])
Adds sequence to list:
>>> [1, 2, 3] + abjad.sequence([4, 5, 6]) Sequence([1, 2, 3, 4, 5, 6])
Adds sequence to sequence:
>>> abjad.sequence([1, 2, 3]) + abjad.sequence([4, 5, 6]) Sequence([1, 2, 3, 4, 5, 6])
>>> expression_1 = abjad.Expression(name='J') >>> expression_1 = expression_1.sequence() >>> expression_2 = abjad.Expression(name='K') >>> expression_2 = expression_2.sequence() >>> expression = expression_1 + expression_2
>>> expression([1, 2, 3], [4, 5, 6]) Sequence([1, 2, 3, 4, 5, 6])
>>> expression.get_string() 'J + K'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Returns new sequence.
-
__repr__
()¶ Gets interpreter representation of sequence.
Gets interpreter representation:
>>> abjad.sequence([99]) Sequence([99])
Gets interpreter representation:
>>> abjad.sequence([1, 2, 3, 4, 5, 6]) Sequence([1, 2, 3, 4, 5, 6])
Returns string.
Methods
-
filter
(predicate=None)¶ Filters sequence by
predicate
.By length:
With lambda:
>>> items = [[1], [2, 3, [4]], [5], [6, 7, [8]]] >>> sequence = abjad.sequence(items)
>>> sequence.filter(lambda _: len(_) == 1) Sequence([[1], [5]])
With inequality:
>>> items = [[1], [2, 3, [4]], [5], [6, 7, [8]]] >>> sequence = abjad.sequence(items)
>>> sequence.filter(abjad.LengthInequality('==', 1)) Sequence([[1], [5]])
As expression:
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> inequality = abjad.LengthInequality('==', 1) >>> expression = expression.filter(inequality)
>>> expression([[1], [2, 3, [4]], [5], [6, 7, [8]]]) Sequence([[1], [5]])
By duration:
With inequality:
>>> staff = abjad.Staff("c'4. d'8 e'4. f'8 g'2") >>> sequence = abjad.sequence(staff)
>>> sequence.filter(abjad.DurationInequality('==', (1, 8))) Sequence([Note("d'8"), Note("f'8")])
As expression:
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> inequality = abjad.DurationInequality('==', (1, 8)) >>> expression = expression.filter(inequality)
>>> expression(staff) Sequence([Note("d'8"), Note("f'8")])
Todo
supply with clean string and markup templates.
Returns new sequence.
-
flatten
(classes=None, depth=1, indices=None)¶ Flattens sequence.
Flattens sequence:
>>> items = [1, [2, 3, [4]], 5, [6, 7, [8]]] >>> sequence = abjad.sequence(items)
>>> sequence.flatten() Sequence([1, 2, 3, [4], 5, 6, 7, [8]])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.flatten()
>>> expression([1, [2, 3, [4]], 5, [6, 7, [8]]]) Sequence([1, 2, 3, [4], 5, 6, 7, [8]])
>>> expression.get_string() 'flatten(J)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Flattens sequence to depth 2:
>>> items = [1, [2, 3, [4]], 5, [6, 7, [8]]] >>> sequence = abjad.sequence(items)
>>> sequence.flatten(depth=2) Sequence([1, 2, 3, 4, 5, 6, 7, 8])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.flatten(depth=2)
>>> expression([1, [2, 3, [4]], 5, [6, 7, [8]]]) Sequence([1, 2, 3, 4, 5, 6, 7, 8])
>>> expression.get_string() 'flatten(J, depth=2)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Flattens sequence to depth -1:
>>> items = [1, [2, 3, [4]], 5, [6, 7, [8]]] >>> sequence = abjad.sequence(items)
>>> sequence.flatten(depth=-1) Sequence([1, 2, 3, 4, 5, 6, 7, 8])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.flatten(depth=-1)
>>> expression([1, [2, 3, [4]], 5, [6, 7, [8]]]) Sequence([1, 2, 3, 4, 5, 6, 7, 8])
>>> expression.get_string() 'flatten(J, depth=-1)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Flattens sequence at indices:
>>> items = [1, [2, 3, [4]], 5, [6, 7, [8]]] >>> sequence = abjad.sequence(items)
>>> sequence.flatten(indices=[3]) Sequence([1, [2, 3, [4]], 5, 6, 7, 8])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.flatten(indices=[3])
>>> expression([1, [2, 3, [4]], 5, [6, 7, [8]]]) Sequence([1, [2, 3, [4]], 5, 6, 7, 8])
>>> expression.get_string() 'flatten(J, indices=[3])'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Flattens sequence at negative indices:
>>> items = [1, [2, 3, [4]], 5, [6, 7, [8]]] >>> sequence = abjad.sequence(items)
>>> sequence.flatten(indices=[-1]) Sequence([1, [2, 3, [4]], 5, 6, 7, 8])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.flatten(indices=[-1])
>>> expression([1, [2, 3, [4]], 5, [6, 7, [8]]]) Sequence([1, [2, 3, [4]], 5, 6, 7, 8])
>>> expression.get_string() 'flatten(J, indices=[-1])'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Flattens tuples in sequence only:
>>> items = ['ab', 'cd', ('ef', 'gh'), ('ij', 'kl')] >>> sequence = abjad.sequence(items)
>>> sequence.flatten(classes=(tuple,)) Sequence(['ab', 'cd', 'ef', 'gh', 'ij', 'kl'])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.flatten(classes=(tuple,))
>>> expression(['ab', 'cd', ('ef', 'gh'), ('ij', 'kl')]) Sequence(['ab', 'cd', 'ef', 'gh', 'ij', 'kl'])
>>> expression.get_string() 'flatten(J, classes=(tuple,))'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Returns new sequence.
-
group_by
(predicate=None)¶ Groups sequence items by value of items.
>>> items = [0, 0, -1, -1, 2, 3, -5, 1, 1, 5, -5] >>> sequence = abjad.sequence(items) >>> for item in sequence.group_by(): ... item ... Sequence([0, 0]) Sequence([-1, -1]) Sequence([2]) Sequence([3]) Sequence([-5]) Sequence([1, 1]) Sequence([5]) Sequence([-5])
>>> staff = abjad.Staff("c'8 d' d' e' e' e'") >>> predicate = abjad.PitchSet.from_selection >>> for item in abjad.sequence(staff).group_by(predicate): ... item ... Sequence([Note("c'8")]) Sequence([Note("d'8"), Note("d'8")]) Sequence([Note("e'8"), Note("e'8"), Note("e'8")])
>>> predicate = abjad.select().leaves().pitch_set() >>> expression = abjad.sequence().group_by(predicate)
>>> staff = abjad.Staff("c'8 d' d' e' e' e'") >>> for item in expression(staff): ... item ... Sequence([Note("c'8")]) Sequence([Note("d'8"), Note("d'8")]) Sequence([Note("e'8"), Note("e'8"), Note("e'8")])
Returns nested sequence.
-
is_decreasing
(strict=True)¶ Is true when sequence decreases.
Is true when sequence is strictly decreasing:
>>> abjad.sequence([5, 4, 3, 2, 1, 0]).is_decreasing(strict=True) True
>>> abjad.sequence([3, 3, 3, 2, 1, 0]).is_decreasing(strict=True) False
>>> abjad.sequence([3, 3, 3, 3, 3, 3]).is_decreasing(strict=True) False
>>> abjad.Sequence().is_decreasing(strict=True) True
Is true when sequence decreases monotonically:
>>> abjad.sequence([5, 4, 3, 2, 1, 0]).is_decreasing(strict=False) True
>>> abjad.sequence([3, 3, 3, 2, 1, 0]).is_decreasing(strict=False) True
>>> abjad.sequence([3, 3, 3, 3, 3, 3]).is_decreasing(strict=False) True
>>> abjad.Sequence().is_decreasing(strict=False) True
Returns true or false.
-
is_increasing
(strict=True)¶ Is true when sequence increases.
Is true when sequence is strictly increasing:
>>> abjad.sequence([0, 1, 2, 3, 4, 5]).is_increasing(strict=True) True
>>> abjad.sequence([0, 1, 2, 3, 3, 3]).is_increasing(strict=True) False
>>> abjad.sequence([3, 3, 3, 3, 3, 3]).is_increasing(strict=True) False
>>> abjad.Sequence().is_increasing(strict=True) True
Is true when sequence increases monotonically:
>>> abjad.sequence([0, 1, 2, 3, 4, 5]).is_increasing(strict=False) True
>>> abjad.sequence([0, 1, 2, 3, 3, 3]).is_increasing(strict=False) True
>>> abjad.sequence([3, 3, 3, 3, 3, 3]).is_increasing(strict=False) True
>>> abjad.Sequence().is_increasing(strict=False) True
Returns true or false.
-
is_permutation
(length=None)¶ Is true when sequence is a permutation.
Is true when sequence is a permutation:
>>> abjad.sequence([4, 5, 0, 3, 2, 1]).is_permutation() True
Is false when sequence is not a permutation:
>>> abjad.sequence([1, 1, 5, 3, 2, 1]).is_permutation() False
Returns true or false.
-
is_repetition_free
()¶ Is true when sequence is repetition-free.
Is true when sequence is repetition-free:
>>> abjad.sequence([0, 1, 2, 6, 7, 8]).is_repetition_free() True
Is true when sequence is empty:
>>> abjad.Sequence().is_repetition_free() True
Is false when sequence contains repetitions:
>>> abjad.sequence([0, 1, 2, 2, 7, 8]).is_repetition_free() False
Returns true or false.
-
join
()¶ Join subsequences in
sequence
.>>> items = [(1, 2, 3), (), (4, 5), (), (6,)] >>> sequence = abjad.sequence(items) >>> sequence Sequence([(1, 2, 3), (), (4, 5), (), (6,)])
>>> sequence.join() Sequence([(1, 2, 3, 4, 5, 6)])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.split([10], cyclic=True) >>> expression = expression.join()
>>> expression(range(1, 11)) Sequence([Sequence([1, 2, 3, 4, 5, 5, 1, 7, 2, 6, 4, 5, 5])])
>>> expression.get_string() 'join(split(J, <10>))'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Returns new sequence.
-
map
(operand=None)¶ Maps
operand
to sequence items.Partitions sequence and sums parts:
>>> sequence = abjad.sequence(range(1, 10+1)) >>> sequence = sequence.partition_by_counts( ... [3], ... cyclic=True, ... ) >>> sequence = sequence.map(sum)
>>> sequence Sequence([6, 15, 24])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [3], ... cyclic=True, ... ) >>> expression = expression.map(abjad.sequence().sum())
>>> expression(range(1, 10+1)) Sequence([6, 15, 24])
>>> expression.get_string() 'sum(X) /@ partition(J, <3>)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Maps identity:
>>> sequence = abjad.sequence([1, 2, 3, 4, 5, 6]) >>> sequence.map() Sequence([1, 2, 3, 4, 5, 6])
Returns new sequence.
-
nwise
(n=2, cyclic=False, wrapped=False)¶ Iterates sequence
n
at a time.Iterates iterable by pairs:
>>> sequence = abjad.sequence(range(10)) >>> for item in sequence.nwise(): ... item ... Sequence([0, 1]) Sequence([1, 2]) Sequence([2, 3]) Sequence([3, 4]) Sequence([4, 5]) Sequence([5, 6]) Sequence([6, 7]) Sequence([7, 8]) Sequence([8, 9])
Iterates iterable by triples:
>>> sequence = abjad.sequence(range(10)) >>> for item in sequence.nwise(n=3): ... item ... Sequence([0, 1, 2]) Sequence([1, 2, 3]) Sequence([2, 3, 4]) Sequence([3, 4, 5]) Sequence([4, 5, 6]) Sequence([5, 6, 7]) Sequence([6, 7, 8]) Sequence([7, 8, 9])
Iterates iterable by pairs. Wraps around at end:
>>> sequence = abjad.sequence(range(10)) >>> for item in sequence.nwise(n=2, wrapped=True): ... item ... Sequence([0, 1]) Sequence([1, 2]) Sequence([2, 3]) Sequence([3, 4]) Sequence([4, 5]) Sequence([5, 6]) Sequence([6, 7]) Sequence([7, 8]) Sequence([8, 9]) Sequence([9, 0])
Iterates iterable by triples. Wraps around at end:
>>> sequence = abjad.sequence(range(10)) >>> for item in sequence.nwise(n=3, wrapped=True): ... item ... Sequence([0, 1, 2]) Sequence([1, 2, 3]) Sequence([2, 3, 4]) Sequence([3, 4, 5]) Sequence([4, 5, 6]) Sequence([5, 6, 7]) Sequence([6, 7, 8]) Sequence([7, 8, 9]) Sequence([8, 9, 0]) Sequence([9, 0, 1])
Iterates iterable by pairs. Cycles indefinitely:
>>> sequence = abjad.sequence(range(10)) >>> pairs = sequence.nwise(n=2, cyclic=True) >>> for _ in range(15): ... next(pairs) ... Sequence([0, 1]) Sequence([1, 2]) Sequence([2, 3]) Sequence([3, 4]) Sequence([4, 5]) Sequence([5, 6]) Sequence([6, 7]) Sequence([7, 8]) Sequence([8, 9]) Sequence([9, 0]) Sequence([0, 1]) Sequence([1, 2]) Sequence([2, 3]) Sequence([3, 4]) Sequence([4, 5])
Returns infinite generator.
Iterates iterable by triples. Cycles indefinitely:
>>> sequence = abjad.sequence(range(10)) >>> triples = sequence.nwise(n=3, cyclic=True) >>> for _ in range(15): ... next(triples) ... Sequence([0, 1, 2]) Sequence([1, 2, 3]) Sequence([2, 3, 4]) Sequence([3, 4, 5]) Sequence([4, 5, 6]) Sequence([5, 6, 7]) Sequence([6, 7, 8]) Sequence([7, 8, 9]) Sequence([8, 9, 0]) Sequence([9, 0, 1]) Sequence([0, 1, 2]) Sequence([1, 2, 3]) Sequence([2, 3, 4]) Sequence([3, 4, 5]) Sequence([4, 5, 6])
Returns infinite generator.
Iterates items one at a time:
>>> sequence = abjad.sequence(range(10)) >>> for item in sequence.nwise(n=1): ... item ... Sequence([0]) Sequence([1]) Sequence([2]) Sequence([3]) Sequence([4]) Sequence([5]) Sequence([6]) Sequence([7]) Sequence([8]) Sequence([9])
Ignores
wrapped
whencyclic
is true.Returns generator.
-
partition_by_counts
(counts, cyclic=False, enchain=False, overhang=False, reversed_=False)¶ Partitions sequence by
counts
.Partitions sequence once by counts without overhang:
>>> sequence = abjad.sequence(range(16)) >>> sequence = sequence.partition_by_counts( ... [3], ... cyclic=False, ... overhang=False, ... )
>>> sequence Sequence([Sequence([0, 1, 2])])
>>> for part in sequence: ... part ... Sequence([0, 1, 2])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [3], ... cyclic=False, ... overhang=False, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2])
>>> expression.get_string() 'partition(J, [3])'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence once by counts without overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [4, 3], ... cyclic=False, ... overhang=False, ... )
>>> for part in parts: ... part ... Sequence([0, 1, 2, 3]) Sequence([4, 5, 6])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [4, 3], ... cyclic=False, ... overhang=False, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2, 3]) Sequence([4, 5, 6])
>>> expression.get_string() 'partition(J, [4, 3])'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence cyclically by counts without overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [3], ... cyclic=True, ... overhang=False, ... )
>>> for part in parts: ... part ... Sequence([0, 1, 2]) Sequence([3, 4, 5]) Sequence([6, 7, 8]) Sequence([9, 10, 11]) Sequence([12, 13, 14])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [3], ... cyclic=True, ... overhang=False, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2]) Sequence([3, 4, 5]) Sequence([6, 7, 8]) Sequence([9, 10, 11]) Sequence([12, 13, 14])
>>> expression.get_string() 'partition(J, <3>)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence cyclically by counts without overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [4, 3], ... cyclic=True, ... overhang=False, ... )
>>> for part in parts: ... part ... Sequence([0, 1, 2, 3]) Sequence([4, 5, 6]) Sequence([7, 8, 9, 10]) Sequence([11, 12, 13])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [4, 3], ... cyclic=True, ... overhang=False, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2, 3]) Sequence([4, 5, 6]) Sequence([7, 8, 9, 10]) Sequence([11, 12, 13])
>>> expression.get_string() 'partition(J, <4, 3>)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence once by counts with overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [3], ... cyclic=False, ... overhang=True, ... )
>>> for part in parts: ... part ... Sequence([0, 1, 2]) Sequence([3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [3], ... cyclic=False, ... overhang=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2]) Sequence([3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
>>> expression.get_string() 'partition(J, [3]+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence once by counts with overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [4, 3], ... cyclic=False, ... overhang=True, ... )
>>> for part in parts: ... part ... Sequence([0, 1, 2, 3]) Sequence([4, 5, 6]) Sequence([7, 8, 9, 10, 11, 12, 13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [4, 3], ... cyclic=False, ... overhang=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2, 3]) Sequence([4, 5, 6]) Sequence([7, 8, 9, 10, 11, 12, 13, 14, 15])
>>> expression.get_string() 'partition(J, [4, 3]+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence cyclically by counts with overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [3], ... cyclic=True, ... overhang=True, ... )
>>> for part in parts: ... part ... Sequence([0, 1, 2]) Sequence([3, 4, 5]) Sequence([6, 7, 8]) Sequence([9, 10, 11]) Sequence([12, 13, 14]) Sequence([15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [3], ... cyclic=True, ... overhang=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2]) Sequence([3, 4, 5]) Sequence([6, 7, 8]) Sequence([9, 10, 11]) Sequence([12, 13, 14]) Sequence([15])
>>> expression.get_string() 'partition(J, <3>+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence cyclically by counts with overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [4, 3], ... cyclic=True, ... overhang=True, ... )
>>> for part in parts: ... part ... Sequence([0, 1, 2, 3]) Sequence([4, 5, 6]) Sequence([7, 8, 9, 10]) Sequence([11, 12, 13]) Sequence([14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [4, 3], ... cyclic=True, ... overhang=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2, 3]) Sequence([4, 5, 6]) Sequence([7, 8, 9, 10]) Sequence([11, 12, 13]) Sequence([14, 15])
>>> expression.get_string() 'partition(J, <4, 3>+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Reverse-partitions sequence once by counts without overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [3], ... cyclic=False, ... overhang=False, ... reversed_=True, ... )
>>> for part in parts: ... part ... Sequence([13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [3], ... cyclic=False, ... overhang=False, ... reversed_=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([13, 14, 15])
>>> expression.get_string() 'partition(J, R[3])'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Reverse-partitions sequence once by counts without overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [4, 3], ... cyclic=False, ... overhang=False, ... reversed_=True, ... )
>>> for part in parts: ... part ... Sequence([9, 10, 11]) Sequence([12, 13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [4, 3], ... cyclic=False, ... overhang=False, ... reversed_=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([9, 10, 11]) Sequence([12, 13, 14, 15])
>>> expression.get_string() 'partition(J, R[4, 3])'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Reverse-partitions sequence cyclically by counts without overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [3], ... cyclic=True, ... overhang=False, ... reversed_=True, ... )
>>> for part in parts: ... part ... Sequence([1, 2, 3]) Sequence([4, 5, 6]) Sequence([7, 8, 9]) Sequence([10, 11, 12]) Sequence([13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [3], ... cyclic=True, ... overhang=False, ... reversed_=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([1, 2, 3]) Sequence([4, 5, 6]) Sequence([7, 8, 9]) Sequence([10, 11, 12]) Sequence([13, 14, 15])
>>> expression.get_string() 'partition(J, R<3>)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Reverse-partitions sequence cyclically by counts without overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [4, 3], ... cyclic=True, ... overhang=False, ... reversed_=True, ... )
>>> for part in parts: ... part ... Sequence([2, 3, 4]) Sequence([5, 6, 7, 8]) Sequence([9, 10, 11]) Sequence([12, 13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [4, 3], ... cyclic=True, ... overhang=False, ... reversed_=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([2, 3, 4]) Sequence([5, 6, 7, 8]) Sequence([9, 10, 11]) Sequence([12, 13, 14, 15])
>>> expression.get_string() 'partition(J, R<4, 3>)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Reverse-partitions sequence once by counts with overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [3], ... cyclic=False, ... overhang=True, ... reversed_=True, ... )
>>> for part in parts: ... part ... Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) Sequence([13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [3], ... cyclic=False, ... overhang=True, ... reversed_=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) Sequence([13, 14, 15])
>>> expression.get_string() 'partition(J, R[3]+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Reverse-partitions sequence once by counts with overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [4, 3], ... cyclic=False, ... overhang=True, ... reversed_=True, ... )
>>> for part in parts: ... part ... Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8]) Sequence([9, 10, 11]) Sequence([12, 13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [4, 3], ... cyclic=False, ... overhang=True, ... reversed_=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8]) Sequence([9, 10, 11]) Sequence([12, 13, 14, 15])
>>> expression.get_string() 'partition(J, R[4, 3]+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Reverse-partitions sequence cyclically by counts with overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [3], ... cyclic=True, ... overhang=True, ... reversed_=True, ... )
>>> for part in parts: ... part ... Sequence([0]) Sequence([1, 2, 3]) Sequence([4, 5, 6]) Sequence([7, 8, 9]) Sequence([10, 11, 12]) Sequence([13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [3], ... cyclic=True, ... overhang=True, ... reversed_=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0]) Sequence([1, 2, 3]) Sequence([4, 5, 6]) Sequence([7, 8, 9]) Sequence([10, 11, 12]) Sequence([13, 14, 15])
>>> expression.get_string() 'partition(J, R<3>+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Reverse-partitions sequence cyclically by counts with overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [4, 3], ... cyclic=True, ... overhang=True, ... reversed_=True, ... )
>>> for part in parts: ... part ... Sequence([0, 1]) Sequence([2, 3, 4]) Sequence([5, 6, 7, 8]) Sequence([9, 10, 11]) Sequence([12, 13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [4, 3], ... cyclic=True, ... overhang=True, ... reversed_=True, ... )
>>> for part in parts: ... part ... Sequence([0, 1]) Sequence([2, 3, 4]) Sequence([5, 6, 7, 8]) Sequence([9, 10, 11]) Sequence([12, 13, 14, 15])
>>> expression.get_string() 'partition(J, R<4, 3>+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence once by counts and asserts that sequence partitions exactly (with no overhang):
>>> sequence = abjad.sequence(range(10)) >>> parts = sequence.partition_by_counts( ... [2, 3, 5], ... cyclic=False, ... overhang=abjad.Exact, ... )
>>> for part in parts: ... part ... Sequence([0, 1]) Sequence([2, 3, 4]) Sequence([5, 6, 7, 8, 9])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [2, 3, 5], ... cyclic=False, ... overhang=abjad.Exact, ... )
>>> for part in expression(range(10)): ... part ... Sequence([0, 1]) Sequence([2, 3, 4]) Sequence([5, 6, 7, 8, 9])
>>> expression.get_string() 'partition(J, [2, 3, 5]!)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence cyclically by counts and asserts that sequence partitions exactly Exact partitioning means partitioning with no overhang:
>>> sequence = abjad.sequence(range(10)) >>> parts = sequence.partition_by_counts( ... [2], ... cyclic=True, ... overhang=abjad.Exact, ... )
>>> for part in parts: ... part ... Sequence([0, 1]) Sequence([2, 3]) Sequence([4, 5]) Sequence([6, 7]) Sequence([8, 9])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [2], ... cyclic=True, ... overhang=abjad.Exact, ... )
>>> for part in expression(range(10)): ... part ... Sequence([0, 1]) Sequence([2, 3]) Sequence([4, 5]) Sequence([6, 7]) Sequence([8, 9])
>>> expression.get_string() 'partition(J, <2>!)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions string:
>>> sequence = abjad.sequence('some text') >>> parts = sequence.partition_by_counts( ... [3], ... cyclic=False, ... overhang=True, ... )
>>> for part in parts: ... part ... Sequence(['s', 'o', 'm']) Sequence(['e', ' ', 't', 'e', 'x', 't'])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [3], ... cyclic=False, ... overhang=True, ... )
>>> for part in expression('some text'): ... part ... Sequence(['s', 'o', 'm']) Sequence(['e', ' ', 't', 'e', 'x', 't'])
>>> expression.get_string() 'partition(J, [3]+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence cyclically into enchained parts by counts; truncates overhang:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [2, 6], ... cyclic=True, ... enchain=True, ... overhang=False, ... )
>>> for part in parts: ... part ... Sequence([0, 1]) Sequence([1, 2, 3, 4, 5, 6]) Sequence([6, 7]) Sequence([7, 8, 9, 10, 11, 12]) Sequence([12, 13])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [2, 6], ... cyclic=True, ... enchain=True, ... overhang=False, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1]) Sequence([1, 2, 3, 4, 5, 6]) Sequence([6, 7]) Sequence([7, 8, 9, 10, 11, 12]) Sequence([12, 13])
>>> expression.get_string() 'partition(J, E<2, 6>)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence cyclically into enchained parts by counts; returns overhang at end:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [2, 6], ... cyclic=True, ... enchain=True, ... overhang=True, ... )
>>> for part in parts: ... part ... Sequence([0, 1]) Sequence([1, 2, 3, 4, 5, 6]) Sequence([6, 7]) Sequence([7, 8, 9, 10, 11, 12]) Sequence([12, 13]) Sequence([13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [2, 6], ... cyclic=True, ... enchain=True, ... overhang=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1]) Sequence([1, 2, 3, 4, 5, 6]) Sequence([6, 7]) Sequence([7, 8, 9, 10, 11, 12]) Sequence([12, 13]) Sequence([13, 14, 15])
>>> expression.get_string() 'partition(J, E<2, 6>+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
REGRESSION: partitions sequence cyclically into enchained parts by counts; does not return false 1-element part at end:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts( ... [5], ... cyclic=True, ... enchain=True, ... overhang=True, ... )
>>> for part in parts: ... part ... Sequence([0, 1, 2, 3, 4]) Sequence([4, 5, 6, 7, 8]) Sequence([8, 9, 10, 11, 12]) Sequence([12, 13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts( ... [5], ... cyclic=True, ... enchain=True, ... overhang=True, ... )
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2, 3, 4]) Sequence([4, 5, 6, 7, 8]) Sequence([8, 9, 10, 11, 12]) Sequence([12, 13, 14, 15])
>>> expression.get_string() 'partition(J, E<5>+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Edge case: empty counts nests sequence and ignores keywords:
>>> sequence = abjad.sequence(range(16)) >>> parts = sequence.partition_by_counts([])
>>> for part in parts: ... part ... Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.partition_by_counts([])
>>> for part in expression(range(16)): ... part ... Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
>>> expression.get_string() 'partition(J, [])'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Returns nested sequence.
-
partition_by_ratio_of_lengths
(ratio)¶ Partitions sequence by
ratio
of lengths.Partitions sequence by
1:1:1
ratio:>>> numbers = abjad.sequence(range(10)) >>> ratio = abjad.Ratio((1, 1, 1))
>>> for part in numbers.partition_by_ratio_of_lengths(ratio): ... part ... Sequence([0, 1, 2]) Sequence([3, 4, 5, 6]) Sequence([7, 8, 9])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> ratio = abjad.Ratio((1, 1, 1)) >>> expression = expression.partition_by_ratio_of_lengths(ratio)
>>> for part in expression(range(10)): ... part ... Sequence([0, 1, 2]) Sequence([3, 4, 5, 6]) Sequence([7, 8, 9])
>>> expression.get_string() 'partition(J, 1:1:1)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Partitions sequence by
1:1:2
ratio:>>> numbers = abjad.sequence(range(10)) >>> ratio = abjad.Ratio((1, 1, 2))
>>> for part in numbers.partition_by_ratio_of_lengths(ratio): ... part ... Sequence([0, 1, 2]) Sequence([3, 4]) Sequence([5, 6, 7, 8, 9])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> ratio = abjad.Ratio((1, 1, 2)) >>> expression = expression.partition_by_ratio_of_lengths(ratio)
>>> for part in expression(range(10)): ... part ... Sequence([0, 1, 2]) Sequence([3, 4]) Sequence([5, 6, 7, 8, 9])
>>> expression.get_string() 'partition(J, 1:1:2)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Returns a sequence of sequences.
-
partition_by_ratio_of_weights
(weights)¶ Partitions sequence by ratio of
weights
.>>> ratio = abjad.Ratio([1, 1, 1]) >>> sequence = abjad.sequence(10 * [1]) >>> sequence = sequence.partition_by_ratio_of_weights(ratio) >>> for item in sequence: ... item ... Sequence([1, 1, 1]) Sequence([1, 1, 1, 1]) Sequence([1, 1, 1])
>>> ratio = abjad.Ratio([1, 1, 1, 1]) >>> sequence = abjad.sequence(10 * [1]) >>> sequence = sequence.partition_by_ratio_of_weights(ratio) >>> for item in sequence: ... item ... Sequence([1, 1, 1]) Sequence([1, 1]) Sequence([1, 1, 1]) Sequence([1, 1])
>>> ratio = abjad.Ratio([2, 2, 3]) >>> sequence = abjad.sequence(10 * [1]) >>> sequence = sequence.partition_by_ratio_of_weights(ratio) >>> for item in sequence: ... item ... Sequence([1, 1, 1]) Sequence([1, 1, 1]) Sequence([1, 1, 1, 1])
>>> ratio = abjad.Ratio([3, 2, 2]) >>> sequence = abjad.sequence(10 * [1]) >>> sequence = sequence.partition_by_ratio_of_weights(ratio) >>> for item in sequence: ... item ... Sequence([1, 1, 1, 1]) Sequence([1, 1, 1]) Sequence([1, 1, 1])
>>> ratio = abjad.Ratio([1, 1]) >>> items = [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2] >>> sequence = abjad.sequence(items) >>> sequence = sequence.partition_by_ratio_of_weights(ratio) >>> for item in sequence: ... item ... Sequence([1, 1, 1, 1, 1, 1, 2, 2]) Sequence([2, 2, 2, 2])
>>> ratio = abjad.Ratio([1, 1, 1]) >>> items = [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2] >>> sequence = abjad.sequence(items) >>> sequence = sequence.partition_by_ratio_of_weights(ratio) >>> for item in sequence: ... item ... Sequence([1, 1, 1, 1, 1, 1]) Sequence([2, 2, 2]) Sequence([2, 2, 2])
>>> ratio = abjad.Ratio([1, 1, 1]) >>> sequence = abjad.sequence([5, 5]) >>> sequence = sequence.partition_by_ratio_of_weights(ratio) >>> for item in sequence: ... item ... Sequence([5]) Sequence([5]) Sequence([])
>>> ratio = abjad.Ratio([1, 1, 1, 1]) >>> sequence = abjad.sequence([5, 5]) >>> sequence = sequence.partition_by_ratio_of_weights(ratio) >>> for item in sequence: ... item ... Sequence([5]) Sequence([]) Sequence([5]) Sequence([])
>>> ratio = abjad.Ratio([2, 2, 3]) >>> sequence = abjad.sequence([5, 5]) >>> sequence = sequence.partition_by_ratio_of_weights(ratio) >>> for item in sequence: ... item ... Sequence([5]) Sequence([5]) Sequence([])
>>> ratio = abjad.Ratio([3, 2, 2]) >>> sequence = abjad.sequence([5, 5]) >>> sequence = sequence.partition_by_ratio_of_weights(ratio) >>> for item in sequence: ... item ... Sequence([5]) Sequence([5]) Sequence([])
Rounded weight-proportions of sequences returned equal to rounded
weights
.Returns nested sequence.
-
partition_by_weights
(weights, cyclic=False, overhang=False, allow_part_weights=Exact)¶ Partitions sequence by
weights
exactly.>>> sequence = abjad.sequence([3, 3, 3, 3, 4, 4, 4, 4, 5])
Partitions sequence once by weights with overhang:
>>> for item in sequence.partition_by_weights( ... [3, 9], ... cyclic=False, ... overhang=False, ... ): ... item ... Sequence([3]) Sequence([3, 3, 3])
Partitions sequence once by weights. Allows overhang:
>>> for item in sequence.partition_by_weights( ... [3, 9], ... cyclic=False, ... overhang=True, ... ): ... item ... Sequence([3]) Sequence([3, 3, 3]) Sequence([4, 4, 4, 4, 5])
Partitions sequence cyclically by weights:
>>> for item in sequence.partition_by_weights( ... [12], ... cyclic=True, ... overhang=False, ... ): ... item ... Sequence([3, 3, 3, 3]) Sequence([4, 4, 4])
Partitions sequence cyclically by weights. Allows overhang:
>>> for item in sequence.partition_by_weights( ... [12], ... cyclic=True, ... overhang=True, ... ): ... item ... Sequence([3, 3, 3, 3]) Sequence([4, 4, 4]) Sequence([4, 5])
>>> sequence = abjad.sequence([3, 3, 3, 3, 4, 4, 4, 4, 5, 5])
Partitions sequence once by weights. Allows part weights to be just less than specified:
>>> for item in sequence.partition_by_weights( ... [10, 4], ... cyclic=False, ... overhang=False, ... allow_part_weights=abjad.Less, ... ): ... item ... Sequence([3, 3, 3]) Sequence([3])
Partitions sequence once by weights. Allows part weights to be just less than specified. Allows overhang:
>>> for item in sequence.partition_by_weights( ... [10, 4], ... cyclic=False, ... overhang=True, ... allow_part_weights=abjad.Less, ... ): ... item ... Sequence([3, 3, 3]) Sequence([3]) Sequence([4, 4, 4, 4, 5, 5])
Partitions sequence cyclically by weights. Allows part weights to be just less than specified:
>>> for item in sequence.partition_by_weights( ... [10, 5], ... cyclic=True, ... overhang=False, ... allow_part_weights=abjad.Less, ... ): ... item ... Sequence([3, 3, 3]) Sequence([3]) Sequence([4, 4]) Sequence([4]) Sequence([4, 5]) Sequence([5])
Partitions sequence cyclically by weights. Allows part weights to be just less than specified. Allows overhang:
>>> for item in sequence.partition_by_weights( ... [10, 5], ... cyclic=True, ... overhang=True, ... allow_part_weights=abjad.Less, ... ): ... item ... Sequence([3, 3, 3]) Sequence([3]) Sequence([4, 4]) Sequence([4]) Sequence([4, 5]) Sequence([5])
>>> sequence = abjad.sequence([3, 3, 3, 3, 4, 4, 4, 4, 5, 5])
Partitions sequence once by weights. Allow part weights to be just more than specified:
>>> for item in sequence.partition_by_weights( ... [10, 4], ... cyclic=False, ... overhang=False, ... allow_part_weights=abjad.More, ... ): ... item ... Sequence([3, 3, 3, 3]) Sequence([4])
Partitions sequence once by weights. Allows part weights to be just more than specified. Allows overhang:
>>> for item in sequence.partition_by_weights( ... [10, 4], ... cyclic=False, ... overhang=True, ... allow_part_weights=abjad.More, ... ): ... item ... Sequence([3, 3, 3, 3]) Sequence([4]) Sequence([4, 4, 4, 5, 5])
Partitions sequence cyclically by weights. Allows part weights to be just more than specified:
>>> for item in sequence.partition_by_weights( ... [10, 4], ... cyclic=True, ... overhang=False, ... allow_part_weights=abjad.More, ... ): ... item ... Sequence([3, 3, 3, 3]) Sequence([4]) Sequence([4, 4, 4]) Sequence([5])
Partitions sequence cyclically by weights. Allows part weights to be just more than specified. Allows overhang:
>>> for item in sequence.partition_by_weights( ... [10, 4], ... cyclic=True, ... overhang=True, ... allow_part_weights=abjad.More, ... ): ... item ... Sequence([3, 3, 3, 3]) Sequence([4]) Sequence([4, 4, 4]) Sequence([5]) Sequence([5])
Returns nested sequence.
-
permute
(permutation)¶ Permutes sequence by
permutation
.>>> sequence = abjad.sequence([10, 11, 12, 13, 14, 15]) >>> sequence.permute([5, 4, 0, 1, 2, 3]) Sequence([15, 14, 10, 11, 12, 13])
>>> sequence = abjad.sequence([11, 12, 13, 14]) >>> sequence.permute([1, 0, 3, 2]) Sequence([12, 11, 14, 13])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.permute([1, 0, 3, 2])
>>> expression([11, 12, 13, 14]) Sequence([12, 11, 14, 13])
>>> expression.get_string() 'permute(J, [1, 0, 3, 2])'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Raises exception when lengths do not match:
>>> sequence = abjad.sequence([1, 2, 3, 4, 5, 6]) >>> sequence.permute([3, 0, 1, 2]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/josiah/Source/github.com/Abjad/abjad/abjad/utilities/Sequence.py", line 3962, in permute raise ValueError(message) ValueError: permutation Sequence([3, 0, 1, 2]) must match length of sequence Sequence([1, 2, 3, 4, 5, 6]).
Returns new sequence.
-
remove
(indices=None, period=None)¶ Removes items at
indices
.>>> sequence = abjad.sequence(range(15))
>>> sequence.remove() Sequence([])
>>> sequence.remove(indices=[2, 3]) Sequence([0, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
Removes elements and indices -2 and -3:
>>> sequence.remove(indices=[-2, -3]) Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14])
>>> sequence.remove(indices=[2, 3], period=4) Sequence([0, 1, 4, 5, 8, 9, 12, 13])
>>> sequence.remove(indices=[-2, -3], period=4) Sequence([2, 3, 6, 7, 10, 11, 14])
>>> sequence.remove(indices=[]) Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
>>> sequence.remove(indices=[97, 98, 99]) Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
Removes no elements:
>>> sequence.remove(indices=[-97, -98, -99]) Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
Returns new sequence.
-
remove_repeats
()¶ Removes repeats from
sequence
.>>> items = [31, 31, 35, 35, 31, 31, 31, 31, 35] >>> sequence = abjad.sequence(items) >>> sequence.remove_repeats() Sequence([31, 35, 31, 35])
Returns new sequence.
-
repeat
(n=1)¶ Repeats sequence.
>>> abjad.sequence([1, 2, 3]).repeat(n=0) Sequence([])
>>> abjad.sequence([1, 2, 3]).repeat(n=1) Sequence([Sequence([1, 2, 3])])
>>> abjad.sequence([1, 2, 3]).repeat(n=2) Sequence([Sequence([1, 2, 3]), Sequence([1, 2, 3])])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.repeat(n=2)
>>> expression([1, 2, 3]) Sequence([Sequence([1, 2, 3]), Sequence([1, 2, 3])])
>>> expression.get_string() 'repeat(J, n=2)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Returns sequence of sequences.
-
repeat_to_length
(length=None, start=0)¶ Repeats sequence to
length
.Repeats list to length 11:
>>> abjad.sequence(range(5)).repeat_to_length(11) Sequence([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0])
>>> abjad.sequence(range(5)).repeat_to_length(11, start=2) Sequence([2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2])
>>> sequence = abjad.sequence([0, -1, -2, -3, -4]) >>> sequence.repeat_to_length(11) Sequence([0, -1, -2, -3, -4, 0, -1, -2, -3, -4, 0])
>>> sequence.repeat_to_length(0) Sequence([])
>>> abjad.sequence([1, 2, 3]).repeat_to_length(10, start=100) Sequence([2, 3, 1, 2, 3, 1, 2, 3, 1, 2])
Returns new sequence.
-
repeat_to_weight
(weight, allow_total=Exact)¶ Repeats sequence to
weight
.Repeats sequence to weight of 23 exactly:
>>> abjad.sequence([5, -5, -5]).repeat_to_weight(23) Sequence([5, -5, -5, 5, -3])
Repeats sequence to weight of 23 more:
>>> sequence = abjad.sequence([5, -5, -5]) >>> sequence.repeat_to_weight(23, allow_total=abjad.More) Sequence([5, -5, -5, 5, -5])
Repeats sequence to weight of 23 or less:
>>> sequence = abjad.sequence([5, -5, -5]) >>> sequence.repeat_to_weight(23, allow_total=abjad.Less) Sequence([5, -5, -5, 5])
>>> items = [abjad.NonreducedFraction(3, 16)] >>> sequence = abjad.sequence(items) >>> weight = abjad.NonreducedFraction(5, 4) >>> sequence = sequence.repeat_to_weight(weight) >>> sum(sequence) NonreducedFraction(20, 16)
>>> [_.pair for _ in sequence] [(3, 16), (3, 16), (3, 16), (3, 16), (3, 16), (3, 16), (2, 16)]
Returns new sequence.
-
replace
(indices, new_material)¶ Replaces items at
indices
withnew_material
.Replaces items at indices 0, 2, 4, 6:
>>> sequence = abjad.sequence(range(16)) >>> sequence.replace( ... ([0], 2), ... (['A', 'B', 'C', 'D'], None), ... ) Sequence(['A', 1, 'B', 3, 'C', 5, 'D', 7, 8, 9, 10, 11, 12, 13, 14, 15])
Replaces elements at indices 0, 1, 8, 13:
>>> sequence = abjad.sequence(range(16)) >>> sequence.replace( ... ([0, 1, 8, 13], None), ... (['A', 'B', 'C', 'D'], None), ... ) Sequence(['A', 'B', 2, 3, 4, 5, 6, 7, 'C', 9, 10, 11, 12, 'D', 14, 15])
Replaces every item at even index:
>>> sequence = abjad.sequence(range(16)) >>> sequence.replace( ... ([0], 2), ... (['*'], 1), ... ) Sequence(['*', 1, '*', 3, '*', 5, '*', 7, '*', 9, '*', 11, '*', 13, '*', 15])
Replaces every element at an index congruent to 0 (mod 6) with
'A'
; replaces every element at an index congruent to 2 (mod 6) with'B'
:>>> sequence = abjad.sequence(range(16)) >>> sequence.replace( ... ([0], 2), ... (['A', 'B'], 3), ... ) Sequence(['A', 1, 'B', 3, 4, 5, 'A', 7, 'B', 9, 10, 11, 'A', 13, 'B', 15])
Returns new sequence.
-
retain
(indices=None, period=None)¶ Retains items at
indices
.>>> sequence = abjad.sequence(range(10)) >>> sequence.retain() Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> sequence.retain(indices=[2, 3]) Sequence([2, 3])
>>> sequence.retain(indices=[-2, -3]) Sequence([7, 8])
>>> sequence.retain(indices=[2, 3], period=4) Sequence([2, 3, 6, 7])
>>> sequence.retain(indices=[-2, -3], period=4) Sequence([0, 3, 4, 7, 8])
>>> sequence.retain(indices=[]) Sequence([])
>>> sequence.retain(indices=[97, 98, 99]) Sequence([])
>>> sequence.retain(indices=[-97, -98, -99]) Sequence([])
Returns new sequence.
-
retain_pattern
(pattern)¶ Retains items at indices matching
pattern
.>>> sequence = abjad.sequence(range(10)) >>> sequence.retain_pattern(abjad.index_all()) Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> sequence.retain_pattern(abjad.index([2, 3])) Sequence([2, 3])
>>> sequence.retain_pattern(abjad.index([-2, -3])) Sequence([7, 8])
>>> sequence.retain_pattern(abjad.index([2, 3], 4)) Sequence([2, 3, 6, 7])
>>> sequence.retain_pattern(abjad.index([-2, -3], 4)) Sequence([0, 3, 4, 7, 8])
>>> sequence.retain_pattern(abjad.index([97, 98, 99])) Sequence([])
>>> sequence.retain_pattern(abjad.index([-97, -98, -99])) Sequence([])
Returns new sequence.
-
reverse
(recurse=False)¶ Reverses sequence.
Reverses sequence:
>>> sequence = abjad.sequence([[1, 2], 3, [4, 5]])
>>> sequence.reverse() Sequence([[4, 5], 3, [1, 2]])
Reverses recursively:
>>> segment_1 = abjad.PitchClassSegment([1, 2]) >>> pitch = abjad.NumberedPitch(3) >>> segment_2 = abjad.PitchClassSegment([4, 5]) >>> sequence = abjad.sequence([segment_1, pitch, segment_2])
>>> for item in sequence.reverse(recurse=True): ... item ... PitchClassSegment([5, 4]) NumberedPitch(3) PitchClassSegment([2, 1])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.reverse(recurse=True)
>>> for item in expression([segment_1, pitch, segment_2]): ... item ... PitchClassSegment([5, 4]) NumberedPitch(3) PitchClassSegment([2, 1])
>>> expression.get_string() 'R*(J)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Returns new sequence.
-
rotate
(n=0)¶ Rotates sequence by index
n
.Rotates sequence to the right:
>>> sequence = abjad.sequence(range(10))
>>> sequence.rotate(n=4) Sequence([6, 7, 8, 9, 0, 1, 2, 3, 4, 5])
Rotates sequence to the left:
>>> sequence = abjad.sequence(range(10))
>>> sequence.rotate(n=-3) Sequence([3, 4, 5, 6, 7, 8, 9, 0, 1, 2])
Rotates sequence neither to the right nor the left:
>>> sequence = abjad.sequence(range(10))
>>> sequence.rotate(n=0) Sequence([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Returns new sequence.
-
select
()¶ Selects sequence.
Returns selection.
-
sort
(key=None, reverse=False)¶ Sorts sequence.
>>> sequence = abjad.sequence([3, 2, 5, 4, 1, 6]) >>> sequence.sort() Sequence([1, 2, 3, 4, 5, 6])
>>> sequence Sequence([3, 2, 5, 4, 1, 6])
Returns new sequence.
-
split
(weights, cyclic=False, overhang=False)¶ Splits sequence by
weights
.Splits sequence cyclically by weights with overhang:
>>> sequence = abjad.sequence([10, -10, 10, -10])
>>> for part in sequence.split( ... (3, 15, 3), ... cyclic=True, ... overhang=True, ... ): ... part ... Sequence([3]) Sequence([7, -8]) Sequence([-2, 1]) Sequence([3]) Sequence([6, -9]) Sequence([-1])
>>> expression = abjad.Expression(name='J') >>> expression = expression.sequence() >>> expression = expression.split( ... (3, 15, 3), ... cyclic=True, ... overhang=True, ... )
>>> for part in expression([10, -10, 10, -10]): ... part ... Sequence([3]) Sequence([7, -8]) Sequence([-2, 1]) Sequence([3]) Sequence([6, -9]) Sequence([-1])
>>> expression.get_string() 'split(J, <3, 15, 3>+)'
>>> markup = expression.get_markup() >>> abjad.show(markup)
Splits sequence once by weights with overhang:
>>> for part in sequence.split( ... (3, 15, 3), ... cyclic=False, ... overhang=True, ... ): ... part ... Sequence([3]) Sequence([7, -8]) Sequence([-2, 1]) Sequence([9, -10])
Splits sequence once by weights without overhang:
>>> for part in sequence.split( ... (3, 15, 3), ... cyclic=False, ... overhang=False, ... ): ... part ... Sequence([3]) Sequence([7, -8]) Sequence([-2, 1])
Returns new sequence.
-
sum
()¶ Sums sequence.
Sums sequence of positive numbers:
>>> sequence = abjad.sequence([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> sequence.sum() 55
Sum sequence of numbers with mixed signs:
>>> sequence = abjad.sequence([-1, 2, -3, 4, -5, 6, -7, 8, -9, 10])
>>> sequence.sum() 5
Sums sequence and wraps result in new sequence:
>>> sequence = abjad.sequence(range(1, 10+1)) >>> result = sequence.sum() >>> sequence = abjad.sequence(result)
>>> sequence Sequence([55])
Returns new sequence.
-
sum_by_sign
(sign=(-1, 0, 1))¶ Sums consecutive sequence items by
sign
.>>> items = [0, 0, -1, -1, 2, 3, -5, 1, 2, 5, -5, -6] >>> sequence = abjad.sequence(items)
>>> sequence.sum_by_sign() Sequence([0, -2, 5, -5, 8, -11])
>>> sequence.sum_by_sign(sign=[-1]) Sequence([0, 0, -2, 2, 3, -5, 1, 2, 5, -11])
>>> sequence.sum_by_sign(sign=[0]) Sequence([0, -1, -1, 2, 3, -5, 1, 2, 5, -5, -6])
>>> sequence.sum_by_sign(sign=[1]) Sequence([0, 0, -1, -1, 5, -5, 8, -5, -6])
>>> sequence.sum_by_sign(sign=[-1, 0]) Sequence([0, -2, 2, 3, -5, 1, 2, 5, -11])
>>> sequence.sum_by_sign(sign=[-1, 1]) Sequence([0, 0, -2, 5, -5, 8, -11])
>>> sequence.sum_by_sign(sign=[0, 1]) Sequence([0, -1, -1, 5, -5, 8, -5, -6])
>>> sequence.sum_by_sign(sign=[-1, 0, 1]) Sequence([0, -2, 5, -5, 8, -11])
Sumsn consecutive negative elements when
-1
insign
.Sums consecutive zero-valued elements when
0
insign
.Sums consecutive positive elements when
1
insign
.Returns new sequence.
-
truncate
(sum_=None, weight=None)¶ Truncates sequence.
>>> sequence = abjad.sequence([-1, 2, -3, 4, -5, 6, -7, 8, -9, 10])
Truncates sequence to weights ranging from 1 to 10:
>>> for weight in range(1, 11): ... result = sequence.truncate(weight=weight) ... print(weight, result) ... 1 Sequence([-1]) 2 Sequence([-1, 1]) 3 Sequence([-1, 2]) 4 Sequence([-1, 2, -1]) 5 Sequence([-1, 2, -2]) 6 Sequence([-1, 2, -3]) 7 Sequence([-1, 2, -3, 1]) 8 Sequence([-1, 2, -3, 2]) 9 Sequence([-1, 2, -3, 3]) 10 Sequence([-1, 2, -3, 4])
Truncates sequence to sums ranging from 1 to 10:
>>> for sum_ in range(1, 11): ... result = sequence.truncate(sum_=sum_) ... print(sum_, result) ... 1 Sequence([-1, 2]) 2 Sequence([-1, 2, -3, 4]) 3 Sequence([-1, 2, -3, 4, -5, 6]) 4 Sequence([-1, 2, -3, 4, -5, 6, -7, 8]) 5 Sequence([-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]) 6 Sequence([-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]) 7 Sequence([-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]) 8 Sequence([-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]) 9 Sequence([-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]) 10 Sequence([-1, 2, -3, 4, -5, 6, -7, 8, -9, 10])
Truncates sequence to zero weight:
>>> sequence.truncate(weight=0) Sequence([])
Truncates sequence to zero sum:
>>> sequence.truncate(sum_=0) Sequence([])
Ignores
sum
whenweight
andsum
are both set.Raises value error on negative
sum
.Returns new sequence.
-
weight
()¶ Gets weight.
>>> abjad.sequence([]).weight() 0
>>> abjad.sequence([1]).weight() 1
>>> abjad.sequence([1, 2, 3]).weight() 6
>>> abjad.sequence([1, 2, -3]).weight() 6
>>> abjad.sequence([-1, -2, -3]).weight() 6
>>> sequence = abjad.sequence([-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]) >>> sequence.weight() 55
>>> abjad.sequence([[1, -7, -7], [1, -8 -8]]).weight() 32
Returns new sequence.
-
zip
(cyclic=False, truncate=True)¶ Zips sequences in sequence.
Zips cyclically:
>>> sequence = abjad.sequence([[1, 2, 3], ['a', 'b']]) >>> for item in sequence.zip(cyclic=True): ... item ... Sequence([1, 'a']) Sequence([2, 'b']) Sequence([3, 'a'])
>>> items = [[10, 11, 12], [20, 21], [30, 31, 32, 33]] >>> sequence = abjad.sequence(items) >>> for item in sequence.zip(cyclic=True): ... item ... Sequence([10, 20, 30]) Sequence([11, 21, 31]) Sequence([12, 20, 32]) Sequence([10, 21, 33])
Zips without truncation:
>>> items = [[1, 2, 3, 4], [11, 12, 13], [21, 22, 23]] >>> sequence = abjad.sequence(items) >>> for item in sequence.zip(truncate=False): ... item ... Sequence([1, 11, 21]) Sequence([2, 12, 22]) Sequence([3, 13, 23]) Sequence([4])
Zips strictly:
>>> items = [[1, 2, 3, 4], [11, 12, 13], [21, 22, 23]] >>> for item in abjad.sequence(items).zip(): ... item ... Sequence([1, 11, 21]) Sequence([2, 12, 22]) Sequence([3, 13, 23])
Returns nested sequence.
Read-only properties
-
items
¶ Gets sequence items.
Initializes items positionally:
>>> abjad.sequence([1, 2, 3, 4, 5, 6]).items (1, 2, 3, 4, 5, 6)
Initializes items from keyword:
>>> abjad.sequence([1, 2, 3, 4, 5, 6]).items (1, 2, 3, 4, 5, 6)
Initializes items positionally:
>>> expression = abjad.sequence() >>> expression([1, 2, 3, 4, 5, 6]).items (1, 2, 3, 4, 5, 6)
Initializes items from keyword:
>>> expression = abjad.sequence() >>> expression([1, 2, 3, 4, 5, 6]).items (1, 2, 3, 4, 5, 6)
Returns tuple.
-