listtools.truncate_to_weight

abjad.tools.listtools.truncate_to_weight(l, total)

Truncate list l such that listtools.weight(l) == total.

abjad> for x in range(10):
...     print x, listtools.truncate_to_weight([-2, 2, -2], x)
... 
0 []
1 [-1]
2 [-2]
3 [-2, 1]
4 [-2, 2]
5 [-2, 2, -1]
6 [-2, 2, -2]
7 [-2, 2, -2]
8 [-2, 2, -2]
9 [-2, 2, -2]
abjad> l = [-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]
abjad> for x in range(10):
...     print x, listtools.truncate_to_weight(l, x)
... 
0 []
1 [-1]
2 [-1, 1]
3 [-1, 2]
4 [-1, 2, -1]
5 [-1, 2, -2]
6 [-1, 2, -3]
7 [-1, 2, -3, 1]
8 [-1, 2, -3, 2]
9 [-1, 2, -3, 3]

Return empty list when total == 0:

abjad> listtools.truncate_to_weight([1, 2, 3, 4, 5], 0)
[]

Raise TypeError when l is not a list:

abjad> listtools.truncate_to_weight('foo', 4)
TypeError

Raise ValueError on negative total:

abjad> listtools.truncate_to_weight([2, 2, 2], -4)
ValueError

Previous topic

listtools.truncate_to_sum

Next topic

listtools.unique

This Page