OCaml -- how to check if two lists match - list

Say I have two lists:
let l1 = [1;2;3];;
let l2 = [1;2;3];;
I am trying to check if they have the same values and order, but if I do
l1 == l2;;
I get false. How do I check if they have the same values in the same order?

Never mind. I forgot that = was different in OCaml.
list1 = list2
returns true...

Related

How to remove an item from a list in python with substring match

I have got a list l1 = ['00001MMYYYSSSS', '00002YYSSMMYNNN', '00003FFMMNNNSS'] and another list
l2 = ['00001', '00003']. I need to remove the items at index 0 and 2 in the list l1 as it contains the string given in l2. How do I go about doing this?
I have tried the solutions mentioned [here]Is there a simple way to delete a list element by value? and
[here]Python: subset elements in one list based on substring in another list, retain only one element per substring but they return an empty list. Thank you!
This should work:
l1 = ['00001MMYYYSSSS', '00002YYSSMMYNNN', '00003FFMMNNNSS']
l2 = ['00001', '00003']
l_result = [x for x in l1 if not any(l in x for l in l2)] # ['00002YYSSMMYNNN']
# Hello World program i
l1 = ['00001MMYYYSSSS', '00002YYSSMMYNNN', '00003FFMMNNNSS']
l2 = ['00001', '00003']
L3=[]
for el in l2:
for el1 in l1:
if el in el1:
L3.append(el1)
for l3 in L3:
l1.remove(l3)
print l1

Haskell How should I check and return the elements in one tuple that partially exists in the other tuple?

I am new to Haskell programming, I currently Have a list1 [[String]] that contains
[["H1","B1"],["H2","B2"],["H3","B2"]]
and another List2 [[String]]
[["H1","B1","H5"],["H2","B2","H5"],["H2","H3","B2"],["H5","B1","H4"]]
and I want to return a result based on list2 that contains elements in List1 like
[["H1","B1","H5"],["H2","B2","H5"],["H2","H3","B2"]]
does anyone understand what I mean?
I totally have no idea how to start on this problem, Thanks for helping
I want to use it as filter and what I design was
isInList :: [[String]] -> [[String]] -> Bool
isInList (x:xs) y = filter (any (`elem` x)) y
But this gave me a wrong answer [["H1","B1","H5"],["H5","B1","H4"]]
where did I wrong?
Your example output contains the first three sublists from List2. Your List1 is of length 3. Do you want to truncate List2 to the length of List1? If so, try
list1 = [["H1","B1"],["H2","B2"],["H3","B2"]]
list2 = [["H1","B1","H5"],["H2","B2","H5"],["H2","H3","B2"]]
truncatedList2 = take (length list1) list2
main = print truncatedList2
Try running the Haskell snippet.

Groupby match into list of lists

I have two lists that have matching elements. For example:
L1 = [A, B]
L2 = [1_A, i_X, i_Y, 2_A, x_B, y_B, z_B]
I wish to group the matching factors into new list like the following:
match_grouplist = [[1_A, 2_A],[x_B, y_B, z_B]]
I tried,
pull = []; tmp = []
for entry in range(len(L1)):
spp = L[entry]
for ele in L2:
if ele.split("_")[1] == spp:
tmp.append(ele)
pull.extend(tmp)
It produces only a list. Can anyone suggest how to make this a list of list ?
Thanks in Advance,
AP
Here is a solution using list comprehensions :
[ [e2 for e2 in L2 if e2.endswith('_'+e1)] for e1 in L1 ]
This means that for each element e1 of L1 we will look for the elements of L2 that end with _e1, and return it.
The result is [['1_A', '2_A'], ['x_B', 'y_B', 'z_B']]

Function that checks that list one is the beginning of list 2

I'm trying to create a function that takes two lists and checks whether the first list is the start of the second list. I have the following pseudo code:
an empty list starts a list
a list does not start an empty list
a list starts a second list if the they have the same head and tail of the second list
Could someone please explain a good way to tackle this problem?
for the first pseudocode statement I thought of doing something like:
fun starts [] l2 = false |
starts l1 [] = false |
starts l1 l2 = if ((hd(l1) = (hd(l2) andalso (tl(l1) = (tl(l2)) then true
I'm not entirely sure whether this would work as what if the second lists tail is longer than the first lists tail? would an error occur?
If someone could help explain or even find a solution, that would be great!
EDIT:
Found a way of doing it, I don't think I was too far off.
Fun start [] l2 = true |
start l1 [] = false |
start l1 l2 = if (hd(l1)) = (hd(l2)) then (start (tl(l1)) (tl(l2))) else false;
With pattern matching, but without conditionals:
fun start [] l2 = true
| start l1 [] = false
| start (x::xs) (y::ys) = x = y andalso start xs ys

How to get the difference between two lists?

I have two lists:
val list1 = List("word1","word2","word2","word3","word1")
val list2 = List("word1","word4")
I want to remove all occurrences of list2 elements from list1, i.e. I want
List("word2","word2","word3") <= list1 *minus* list2
I did list1 diff list2
which gives me List("word2","word2","word3","word1") which is removing only the first occurrence of "word1".
I cannot convert it to sets because I need knowledge about duplicates (see "word2" above). What to do?
You can use
val unwanted = list2.toSet
list1.filterNot(unwanted)
to remove all items in list2 (you don't need knowledge of duplicates in list2).
val list1 = List("word1","word2","word2","word3","word1")
val list2 = List("word1","word4")
list1 diff list2
This will do it.
You could try this:
val list1 = List("word1","word2","word2","word3","word1")
val list2 = List("word1","word4")
println(list1.filterNot(list2.contains(_)))