I'm trying to understand what is happening in this piece of code. I can see what it does, but the process for how it gets there eludes me.
from itertools import groupby
lines = '''
This is the
first paragraph.
This is the second.
'''.splitlines()
# Use itertools.groupby and bool to return groups of
# consecutive lines that either have content or don't.
for has_chars, frags in groupby(lines, bool):
if has_chars:
print ' '.join(frags)
# PRINTS:
# This is the first paragraph.
# This is the second.
I think my confusion surrounds the multiple variables in the for loop (in this instance has_chars
and frags
). How are multiple variables possible? What is happening? How is python dealing with multiple variables? What am I saying to python when I put multiple variables in a for loop? Is there a limit to how many variables you can create in a for loop? How can I ask a precise question when I don't understand enough about programming to actually form one?
I've tried running it through the python visualiser to get a better understanding. That thing has never made anything clearer for me. Try as I oft do.