Inconsistent modulus result in python

Question:

I’ve got a simple list "passage" that contains strings. Goal is to join these split words into lines of 5 words each. Using modulus 5 (against index value + 1 to simplify) as test of when to add a newline character.

For reasons that escape me, it works fine, except when it suddenly decides to skip a multiple of 5. I can’t make out why it is inconsistent. Mysteriously skips indices 40, 45, and 55.

for word in passage:
    indx = passage.index(word)   # dump into variable "indx" for convenience
    if (indx+1) %5  == 0:        # act every 5th element of list
        passage[indx] = f"{word}n{indx+1}" # add line break and index number to element

print(passage)
print()

final = " ".join(passage)
print(final)

Altered list output:

['When', 'in', 'the', 'Course', 'ofn5', 'human', 'events,', 'it', 'becomes', 'necessaryn10', 'for', 'one', 'people', 'to', 'dissolven15', 'the', 'political', 'bands', 'which', 'haven20', 'connected', 'them', 'with', 'another,', 'andn25', 'to', 'assume', 'among', 'the', 'powersn30', 'of', 'the', 'earth,', 'the', 'separaten35', 'and', 'equal', 'station', 'to', 'which', 'the', 'Laws', 'of', 'Nature', 'and', 'of', "Nature's", 'God', 'entitle', 'them,n50', 'a', 'decent', 'respect', 'to', 'the', 'opinions', 'of', 'mankind', 'requires', 'thatn60', 'they', 'should', 'declare', 'the', 'causesn65', 'which', 'impel', 'them', 'to', 'the', 'separation.']

and "joined" output as string:

When in the Course of
5 human events, it becomes necessary
10 for one people to dissolve
15 the political bands which have
20 connected them with another, and
25 to assume among the powers
30 of the earth, the separate
35 and equal station to which the Laws of Nature and of Nature's God entitle them,
50 a decent respect to the opinions of mankind requires that
60 they should declare the causes
65 which impel them to the separation.

Thoughts?

Sorry not to have included the original list, ewong:

['When', 'in', 'the', 'Course', 'of', 'human', 'events,', 'it', 'becomes', 'necessary', 'for', 'one', 'people', 'to', 'dissolve', 'the', 'political', 'bands', 'which', 'have', 'connected', 'them', 'with', 'another,', 'and', 'to', 'assume', 'among', 'the', 'powers', 'of', 'the', 'earth,', 'the', 'separate', 'and', 'equal', 'station', 'to', 'which', 'the', 'Laws', 'of', 'Nature', 'and', 'of', "Nature's", 'God', 'entitle', 'them,', 'a', 'decent', 'respect', 'to', 'the', 'opinions', 'of', 'mankind', 'requires', 'that', 'they', 'should', 'declare', 'the', 'causes', 'which', 'impel', 'them', 'to', 'the', 'separation.']

I’ll check out enumerate. (Just getting started with Python. Sorry if I seem obtuse.)

Eduardo Reis, thanks for the suggestion that duplicate array elements cause some sort of index problem. I’ll investigate.

Asked By: Charles Ellmaker

||

Answers:

You can just split the passage into words (on whitespace) and then chunk that list of words into blocks of 5, joining each chunk with the index (if > 0):

passage = """
When in the Course of human events, it becomes necessary
for one people to dissolve the political bands which have
connected them with another, and to assume among the powers 
of the earth, the separate and equal station to which the 
Laws of Nature and of Nature's God entitle them, a decent 
respect to the opinions of mankind requires that they should 
declare the causes which impel them to the separation.
"""
words = passage.split()
chunks = [' '.join(([str(i)] if i else []) + words[i:i+5]) for i in range(0, len(words), 5)]

Output:

[
 'When in the Course of',
 '5 human events, it becomes necessary',
 '10 for one people to dissolve',
 '15 the political bands which have',
 '20 connected them with another, and',
 '25 to assume among the powers',
 '30 of the earth, the separate',
 '35 and equal station to which',
 '40 the Laws of Nature and',
 "45 of Nature's God entitle them,",
 '50 a decent respect to the',
 '55 opinions of mankind requires that',
 '60 they should declare the causes',
 '65 which impel them to the',
 '70 separation.'
]

This can then be joined with n to get the output:

print('n'.join(chunks))

Output:

When in the Course of
5 human events, it becomes necessary
10 for one people to dissolve
15 the political bands which have
20 connected them with another, and
25 to assume among the powers
30 of the earth, the separate
35 and equal station to which
40 the Laws of Nature and
45 of Nature's God entitle them,
50 a decent respect to the
55 opinions of mankind requires that
60 they should declare the causes
65 which impel them to the
70 separation.
Answered By: Nick
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.