Created
June 9, 2013 11:28
-
-
Save Lysander/5743234 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1 | |
| 1 | |
| 2 | |
| 3 | |
| 3 | |
| 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| from itertools import groupby | |
| def group_rows_by_values(rows): | |
| for _, group in groupby(rows): | |
| for row in group: | |
| yield row | |
| yield "" | |
| def main(): | |
| with open(sys.argv[1]) as infile: | |
| print("\n".join(group_rows_by_values(row.strip() for row in infile))) | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| python group.py data.txt | |
| 1 | |
| 1 | |
| 2 | |
| 3 | |
| 3 | |
| 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment