I am aiming to do something similar to what might usually be done with something like textwrap.wrap
, but instead of wrapping text with a specified maximum length I wish to instead specify how many lines.
I have split my string into an array of words at word barriers and now I want to rejoin my array of strings with a space such that I have a specified number of lines.
Here's what I have so far; num
is the number of lines I require, words
is an array of words. wpc
is an (incorrect) assumption of how many "words per chunk":
num = 4
words = [
"The",
"quick",
"brown",
"fox",
"jumps",
"over",
"the",
"lazy",
"dog"
]
wpc = math.ceil(len(words)/num)
chunks = [' '.join(words[num*i:num*(i+1)]) for i in range(wpc)]
This gives the incorrect result:
[
"The quick brown fox",
"jumps over the lazy",
"dog"
]
Which has 3 lines not the 4 required.
Both the list of words and the number of lines required are dynamic, where am I going wrong?
The correct output is a bit dubious.. one possibility is
[
"The quick brown",
"fox jumps",
"over the",
"lazy dog"
]
But of course the "3 word" line could be anywhere. It doesn't matter too much how the odd line is placed (first, last, randomly) so long as there are always num
lines.
Additionally if you added more words it would be good to evenly distribute the words (again, I'm not too fussed how they get distributed):
[
"The quick brown",
"giant fox jumps",
"over the very",
"lazy dog"
]