Skip to content

Instantly share code, notes, and snippets.

@ezr
Created November 18, 2021 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezr/0c8b9f4562ea1796c798aa95d21c4578 to your computer and use it in GitHub Desktop.
Save ezr/0c8b9f4562ea1796c798aa95d21c4578 to your computer and use it in GitHub Desktop.
takes lists and merges them into columns
#!/usr/bin/env python3
import argparse
from sys import argv, stderr
parser = argparse.ArgumentParser(description='zip together lists')
parser.add_argument("-s", "--separator", default=" ", help='character to separate items', required=False)
parser.add_argument("lists", nargs="+")
args = parser.parse_args()
lists = list(map(lambda x: x.strip().split("\n"), args.lists))
if len(dict.fromkeys(map(len, lists))) != 1:
print("[!] warning - lists were not all the same length", file=stderr)
zipped = zip(*lists)
for t in zipped:
for i, item in enumerate(t):
print(item, end=args.separator) if i+1 != len(t) else print(item)
@ezr
Copy link
Author

ezr commented Nov 18, 2021

Examples:

./zip-args.py 'pi
e
zero
gamma' '3.14
2.72
0
0.58'

pi 3.14
e 2.72
zero 0
gamma 0.58
./zip-args.py --separator ',' 'dog
cat
snake' 'mammal
mammal
reptile' 'nice
mean
spooky'

dog,mammal,nice
cat,mammal,mean
snake,reptile,spooky

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment