merge-csv-lists: refactor
This commit is contained in:
parent
7ff425cb2f
commit
a7c1936acf
|
@ -2,60 +2,52 @@
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
|
def split_and_flat(input_string):
|
||||||
|
if "\n" in input_string:
|
||||||
|
_without_newlines = input_string.split("\n")
|
||||||
|
_without_empty = filter(lambda x: x != "", _without_newlines)
|
||||||
|
input_string = ",".join(_without_empty)
|
||||||
|
comma_split = input_string.split(",")
|
||||||
|
flat_list = []
|
||||||
|
for item in comma_split:
|
||||||
|
dash_split = item.split("-")
|
||||||
|
flat_list.extend([value.strip().replace("\n", "") for value in dash_split])
|
||||||
|
return ",".join(flat_list)
|
||||||
|
|
||||||
|
|
||||||
def unique_combined_list(*inputs):
|
def unique_combined_list(*inputs):
|
||||||
# Combine lists
|
|
||||||
combined_list = [
|
combined_list = [
|
||||||
item.strip().title() for input_list in inputs for item in input_list.split(",")
|
item.strip().title() for input_list in inputs for item in input_list.split(",")
|
||||||
]
|
]
|
||||||
|
|
||||||
# Create an empty list to store the final unique names
|
|
||||||
final_list = []
|
final_list = []
|
||||||
|
|
||||||
# Check for reversed names
|
|
||||||
for name in combined_list:
|
for name in combined_list:
|
||||||
parts = name.split()
|
parts = name.split()
|
||||||
|
|
||||||
# If the name has two words, check for its reversed variant
|
|
||||||
if len(parts) == 2:
|
if len(parts) == 2:
|
||||||
first, last = parts
|
first, last = parts
|
||||||
reversed_name = f"{last} {first}"
|
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:
|
if name not in final_list and reversed_name not in final_list:
|
||||||
final_list.append(name)
|
final_list.append(name)
|
||||||
# If it's a single-word name, simply add it if it's not in the final list
|
|
||||||
else:
|
else:
|
||||||
if name not in final_list:
|
if name not in final_list:
|
||||||
final_list.append(name)
|
final_list.append(name)
|
||||||
|
|
||||||
# Sort the list
|
|
||||||
sorted_list = sorted(final_list)
|
sorted_list = sorted(final_list)
|
||||||
|
|
||||||
# Convert the list back to a comma-separated string
|
|
||||||
output = ",".join(sorted_list)
|
output = ",".join(sorted_list)
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Create an argument parser
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Combine multiple comma-separated lists into one unique sorted list."
|
description="Combine multiple comma-separated lists into one unique sorted list."
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add a variable number of input lists
|
|
||||||
parser.add_argument("lists", nargs="+", type=str, help="Comma-separated lists.")
|
parser.add_argument("lists", nargs="+", type=str, help="Comma-separated lists.")
|
||||||
|
|
||||||
# Parse the arguments
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# If only one list is provided, use it twice
|
|
||||||
if len(args.lists) == 1:
|
if len(args.lists) == 1:
|
||||||
args.lists.append(args.lists[0])
|
args.lists.append(args.lists[0])
|
||||||
|
|
||||||
# Get the unique combined list
|
# Process each list with split_and_flat if it contains a dash
|
||||||
result = unique_combined_list(*args.lists)
|
processed_lists = [split_and_flat(lst) if "-" in lst else lst for lst in args.lists]
|
||||||
|
|
||||||
|
result = unique_combined_list(*processed_lists)
|
||||||
print(result)
|
print(result)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue