scripts/merge-csv-lists.py

46 lines
1.3 KiB
Python

#!/usr/bin/python
import argparse
def flatten_and_split(input_string):
elements = input_string.replace("\n", "").split(",")
flat_list = [item.strip() for element in elements for item in element.split("-")]
return ",".join(flat_list)
def combine_and_uniquify(*inputs):
combined_list = [
item.strip().title() for input_list in inputs for item in input_list.split(",")
]
unique_names = set(combined_list)
final_set = set()
for name in unique_names:
parts = name.split()
if len(parts) == 2:
first, last = parts
reversed_name = f"{last} {first}"
# Add the name if its reversed variant is not already in the final set
if reversed_name not in final_set:
final_set.add(name)
else:
final_set.add(name)
return ",".join(sorted(final_set))
def main():
parser = argparse.ArgumentParser(
description="Combine multiple comma-separated lists into one unique sorted list."
)
parser.add_argument("lists", nargs="+", type=str, help="Comma-separated lists.")
args = parser.parse_args()
processed_lists = [flatten_and_split(lst) for lst in args.lists]
result = combine_and_uniquify(*processed_lists)
print(result)
if __name__ == "__main__":
main()