scripts/merge-csv-lists.py

64 lines
1.7 KiB
Python
Raw Normal View History

2023-10-22 15:34:48 +00:00
#!/usr/bin/python
import argparse
def unique_combined_list(*inputs):
2023-10-22 15:34:48 +00:00
# Combine lists
combined_list = [
item.strip().title() for input_list in inputs for item in input_list.split(",")
]
2023-10-22 15:34:48 +00:00
# Create an empty list to store the final unique names
final_list = []
# Check for reversed names
for name in combined_list:
parts = name.split()
# If the name has two words, check for its reversed variant
if len(parts) == 2:
first, last = parts
reversed_name = f"{last} {first}"
# If neither the name nor its reversed variant is in the final list, add the name
if name not in final_list and reversed_name not in final_list:
final_list.append(name)
# If it's a single-word name, simply add it if it's not in the final list
else:
if name not in final_list:
final_list.append(name)
# Sort the list
sorted_list = sorted(final_list)
2023-10-22 15:34:48 +00:00
# Convert the list back to a comma-separated string
output = ",".join(sorted_list)
return output
def main():
# Create an argument parser
parser = argparse.ArgumentParser(
description="Combine multiple comma-separated lists into one unique sorted list."
2023-10-22 15:34:48 +00:00
)
# Add a variable number of input lists
parser.add_argument("lists", nargs="+", type=str, help="Comma-separated lists.")
2023-10-22 15:34:48 +00:00
# Parse the arguments
args = parser.parse_args()
# If only one list is provided, use it twice
if len(args.lists) == 1:
args.lists.append(args.lists[0])
2023-10-22 15:34:48 +00:00
# Get the unique combined list
result = unique_combined_list(*args.lists)
2023-10-22 15:34:48 +00:00
print(result)
if __name__ == "__main__":
main()