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(_)))
Related
I have two lists:
list1 = [1,2,3]
list2 = [4,5,6]
I want to multiply each number in the first list with each number in the second list to get the result:
list3 = [4,5,6,8,10,12,12,15,18]
How do I do this?
There could be a more pythonic way to do this but this gets the job
done.
lst1 = [1,2,3]
lst2 = [4,5,6]
lst3 = []
for i in lst1:
for j in lst2:
lst3.append(i*j)
print(lst3)
Using list comprehension:
print([(l1*l2) for l1 in lst1 for l2 in lst2])
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...
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.
I have a strange reaction of python (using 2.7) here. I am trying to copy a list and append something to the copy at the same time. Here is the code:
myList = [1]
>>> newList = list(myList).append(2)
>>> newList
>>> print newList
None
>>> type(newList)
<type 'NoneType'>
Why is it that I get a NoneType object instead of my appended list-copy?
I stumbled over this when I tried to take a list1 copy it as many times as a list2 and append the elements of list2 to the ones in list1.
>>> list1 = [1,2]
>>> list2 = [3,4]
>>> list3 = [list(list1).append(i) for i in list2]
>>> list3
[None, None]
I expected:
>>> list3
[[1,2,3],[1,2,4]]
Why is it None,None?
Thanks a lot!
You can do this by adding a extra line:
myList=[1]
myList.append(2);newList=myList
You can also extend (append )list directly like:
list1 = [1,2]
list2 = [3,4]
list1.extend(list2);list3=list1
If u dont want to alter then try this:
list1 = [1,2]
list2 = [3,4]
list3=list1;list3.extend(list2)
And also:
myList=[1]
newList=myList;newList.append(2)
The append function modifies a list and returns None. Newlist was None because append() modifies the list directly, rather than returning the modified list.
This code will create the new list and add to it in one step.
myList = [1]
newList = myList + [2]
I am trying to find a way to make two lists the same length. How can I add zeros to one list so as to make it have the same length with the first one?
i.e. list1=[ 1 2 3 4 5]; list2=[ 1 2 3]
There are many ways you can do that. One of them is
list3 = zeros(size(list1)); %# create an array of the same shape as list1
list3(1:numel(list2)) = list2(:); %# fill in the elements defined in list2
Another way is
list3 = [list2, zeros(1,length(list1)-length(list2))];
Both of these ways assume that list2 is shorter than list1.
Here's a one-liner for the case where you know list2 is shorter than list1
list2(numel(list1)) = 0;
Assuming that you do not know which of the two lists is bigger. You can do the following:
dif = size(l2)-size(l1);
if dif(2) < 0
l2 = [l2, zeros(1, -dif(2))];
else
l1 = [l1, zeros(1, dif(2))];
end
(This works on octave)
l1 = list1
l2 = list2