Adding zeros to a list - list

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

Related

How do you multiply each number in a list with each number in another list

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])

How can I calculate the sums of all possible combinations of elements of a set? [Python]

For example, I have the list L = [1,2,4]. For the sake of comprehension, I want to list all the combinations for two pulls with a put back (the order does not matter):
[1,1]
[1,2]
[2,2]
[1,4]
[2,4]
The sum of the respective lists is then: [2,3,4,5,6].
So I want to get the output [2,3,4,5,6] from the input [1,2,4] with a function, so to speak. In the general case, the list has k elements and N elements are taken for the combinations. Say:
def fun(L,N):
...
(do the thing)
return sum_list
something like below
L = [1, 2, 4]
k = []
for i in range(len(L)):
for j in range(len(L)-i):
k.append(L[j+i] + L[i])

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

OCaml -- how to check if two lists match

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...

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(_)))