cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
Neo
Neo
Level VI

How to extract items in a list from another list with partial matches to items in a third list?

I have the following lists

bigList  = {"CD_CORN_1", "ICS_CORN_6", "IPEK21_S00R_P37_2", "CURR_S009_P37_2", "CD_CORN_2", "Ig_RF", "Cmax03_4C230RF", "NG"};
smallList1  = {"CD_CORN_", "CD_CORN_", "CD_CORN_"}; //note these are repeats and that is how I get them
smallList2  = {"IDS_CORN_", "IPEK21_S00R_", "CURR_S009_"};
smallList3  = {"RF", "IPEK21_S00R_", "CURR_S009_", "NG"};

using biglist and smalllist1, I want to get

sbigList1  = {"CD_CORN_1", "CD_CORN_2"};

using biglist and smalllist2, I want to get

sbigList2  = {"ICS_CORN_6", "IPEK21_S00R_P37_2", "CURR_S009_P37_2"};

using biglist and smalllist3, I want to get

sbigList3  = {"IPEK21_S00R_P37_2", "CURR_S009_P37_2", "Ig_RF", "Cmax03_4C230RF", "NG"};

How to do this via JSL?

(order of items in sbiglist's do not matter)

 

When it's too good to be true, it's neither
1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: How to extract items in a list from another list with partial matches to items in a third list?

I would most likely create a function for something like this

Names Default To Here(1);

bigList = {"CD_CORN_1", "ICS_CORN_6", "IPEK21_S00R_P37_2", "CURR_S009_P37_2", "CD_CORN_2", "Ig_RF", "Cmax03_4C230RF", "NG"};
smallList1 = {"CD_CORN_", "CD_CORN_", "CD_CORN_"};
smallList2 = {"IDS_CORN_", "IPEK21_S00R_", "CURR_S009_"};
smallList3 = {"RF", "IPEK21_S00R_", "CURR_S009_", "NG"};

get_partial_matches = function({to_list, with_list}, {Default Local},
	/*""" Get partial matches to_list using with_list
	
	Args:
		from_list (list(str)): list to compare to
		to_list (list(str)): list to compare with
	
	Returns:
		list(str): List with partial matches in alphabetical order
	"""*/
	
	matches = Associative Array(); // associative array to avoid duplicates
	
	For Each({to_item}, to_list,
		For Each({with_item}, with_list,
			If(Contains(to_item, with_item),
				Insert Into(matches, to_item);
			);
		);
	);
	
	return(matches << get keys); // << get keys to get result in a list
);

list1 = get_partial_matches(bigList, smallList1);
list2 = get_partial_matches(bigList, smallList2);
list3 = get_partial_matches(bigList, smallList3);

show(list1, list2, list3);

other option would be something like this, but I would much rather use the earlier one

get_partial_matches2 = function({to_list, with_list}, {Default Local},	
	
	matches = Associative Array(); 
	pattern = Concat Items(with_list, "|");
	
	For Each({to_item}, to_list,
		res = Regex(to_item, pattern);
		If(!IsMissing(res),
			Insert Into(matches, to_item);
		);
	);
	
	return(matches << get keys);
);

list1 = get_partial_matches2(bigList, smallList1);
list2 = get_partial_matches2(bigList, smallList2);
list3 = get_partial_matches2(bigList, smallList3);

show(list1, list2, list3);
-Jarmo

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: How to extract items in a list from another list with partial matches to items in a third list?

Here is how I would do it

Names default to here(1);
bigList  = {"CD_CORN_1", "ICS_CORN_6", "IPEK21_S00R_P37_2", "CURR_S009_P37_2", "CD_CORN_2", "Ig_RF", "Cmax03_4C230RF", "NG"};
smallList1  = {"CD_CORN_"}; //note these are repeats and that is how I get them
smallList2  = {"IDS_CORN_", "IPEK21_S00R_", "CURR_S009_"};
smallList3  = {"RF", "IPEK21_S00R_", "CURR_S009_", "NG"};

foundList = {};

For Each( {theList}, bigList,
	For Each( {sublist}, smallList1,
		if(contains(theList,sublist),
			insert into(foundlist, theList)
		)
	);
	For Each( {sublist}, smallList2,
		if(contains(theList,sublist),
			insert into(foundlist, theList)
		)
	);
	For Each( {sublist}, smallList3,
		if(contains(theList,sublist),
			insert into(foundlist, theList)
		)
	);
);

Each separate element of bigList has to be evaluated separately.

Jim
jthi
Super User

Re: How to extract items in a list from another list with partial matches to items in a third list?

I would most likely create a function for something like this

Names Default To Here(1);

bigList = {"CD_CORN_1", "ICS_CORN_6", "IPEK21_S00R_P37_2", "CURR_S009_P37_2", "CD_CORN_2", "Ig_RF", "Cmax03_4C230RF", "NG"};
smallList1 = {"CD_CORN_", "CD_CORN_", "CD_CORN_"};
smallList2 = {"IDS_CORN_", "IPEK21_S00R_", "CURR_S009_"};
smallList3 = {"RF", "IPEK21_S00R_", "CURR_S009_", "NG"};

get_partial_matches = function({to_list, with_list}, {Default Local},
	/*""" Get partial matches to_list using with_list
	
	Args:
		from_list (list(str)): list to compare to
		to_list (list(str)): list to compare with
	
	Returns:
		list(str): List with partial matches in alphabetical order
	"""*/
	
	matches = Associative Array(); // associative array to avoid duplicates
	
	For Each({to_item}, to_list,
		For Each({with_item}, with_list,
			If(Contains(to_item, with_item),
				Insert Into(matches, to_item);
			);
		);
	);
	
	return(matches << get keys); // << get keys to get result in a list
);

list1 = get_partial_matches(bigList, smallList1);
list2 = get_partial_matches(bigList, smallList2);
list3 = get_partial_matches(bigList, smallList3);

show(list1, list2, list3);

other option would be something like this, but I would much rather use the earlier one

get_partial_matches2 = function({to_list, with_list}, {Default Local},	
	
	matches = Associative Array(); 
	pattern = Concat Items(with_list, "|");
	
	For Each({to_item}, to_list,
		res = Regex(to_item, pattern);
		If(!IsMissing(res),
			Insert Into(matches, to_item);
		);
	);
	
	return(matches << get keys);
);

list1 = get_partial_matches2(bigList, smallList1);
list2 = get_partial_matches2(bigList, smallList2);
list3 = get_partial_matches2(bigList, smallList3);

show(list1, list2, list3);
-Jarmo