I'm quite new to Maple and I would like to create the following list:
U__N := u__1[0], u__2[0], u__1[1], u__2[1], u__1[2], u__2[2], u__1[3], u__2[3], u__1[4], u__2[4], u__1[5], u__2[5]
I came up with the following two options. For both I lack the knowledge for the last step
Option 1
U__N := seq([u__1[k], u__2[k]], k = 0 .. 5)
which gives me a nested list: U__N := [u__1[0], u__2[0]], [u__1[1], u__2[1]], [u__1[2], u__2[2]], [u__1[3], u__2[3]], [u__1[4], u__2[4]], [u__1[5], u__2[5]]. However, now I do not know how to "un-nest" the nested list.
Option 2:
Create two separate lists
U__N2 := seq(u__2[k], k = 0 .. 5 - 1)
which returns U__N1 := u__1[0], u__1[1], u__1[2], u__1[3], u__1[4]
U__N2 := seq(u__2[k], k = 0 .. 5 - 1)
which returns U__N2 := u__2[0], u__2[1], u__2[2], u__2[3], u__2[4].
Now I would like to concatenate/combine these two lists alternatively.
Do you have any suggestions for one of these two options or an alternative solution?
I would prefer to create the pair-wise portions and then utilize those directly, than to form the whole list-of-lists and Flatten it.
The special-evaluation rules of the seq command allows for its first argument to not be evaluated until after k attains concrete numeric values.
This allow you to adjust your first method and extract the operands of the pair-wise inner lists, using the op command.
seq(op([u__1[k], u__2[k]]), k = 0 .. 5);
u__1[0], u__2[0], u__1[1], u__2[1],
u__1[2], u__2[2], u__1[3], u__2[3],
u__1[4], u__2[4], u__1[5], u__2[5]
seq([u__1[k], u__2[k]][], k = 0 .. 5);
u__1[0], u__2[0], u__1[1], u__2[1],
u__1[2], u__2[2], u__1[3], u__2[3],
u__1[4], u__2[4], u__1[5], u__2[5]
The trailing [] in [...][] acts like op([...]).
using Flatten gave me the desired solution
Related
I have two lists and I want to return a result in the following way:
the result should contain elements that are in list one and list two
output should be same order as per first list
Input :
val first = listOf(1, 2, 3, 4, 5,7,9,15,11)
val second = listOf(2, 15 , 4,3, 11)
Output:
val output = listOf(2,3,4,15,11)
Please help me to learn how to get common values in both lists in order of list first in Kotlin.
You can do
val output = first.filter { second.contains(it) }
What you are looking for is the intersection of the two lists:
val output = first.intersect(second)
As pointed out by #Ivo the result is a Set which can be turned into a list with output.toList(). However, since the result is a set, it contains no duplicates, e.g. if first is listOf(1,2,3,1,2,3) and second is listOf(2,4,2,4), the result will be equal to setOf(2).
If this is not acceptable, the solution of #Ivo should be used instead.
I have a list like below and need to firs add items in each list and then multiply all results 2+4 = 6 , 3+ (-2)=1, 2+3+2=7, -7+1=-6 then 6*1*7*(-6) = -252 I know how to do it by accessing indexes and it works (as below) but I also need to do it in a way that it will work no matter how many sublist there is
nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]]
a= nested_lst[0][0] + nested_lst[0][1]
b= nested_lst[1][0] + nested_lst[1][1]
c= nested_lst[2][0] + nested_lst[2][1] + nested_lst[2][2]
d= nested_lst[3][0] + nested_lst[3][1]
def sum_then_product(list):
multip= a*b*c*d
return multip
print sum_then_product(nested_lst)
I have tried with for loop which gives me addition but I don't know how to perform here multiplication. I am new to it. Please, help
nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]]
for i in nested_lst:
print sum(i)
Is this what you are looking for?
nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]] # your list
output = 1 # this will generate your eventual output
for sublist in nested_lst:
sublst_out = 0
for x in sublist:
sublst_out += x # your addition of the sublist elements
output *= sublst_out # multiply the sublist-addition with the other sublists
print(output)
Assume I have the following matrix:
X = np.array([[1,2,3], [4,5,6], [7,8,9], [70,80,90], [45,43,68], [112,87,245]])
I want to draw a batch of 2 random rows at each time loop, and send it to a function. For instance, a batch in iteration i can be batch = [[4,5,6], [70,80,90]]
I do the following:
X = np.array([[1,2,3], [4,5,6], [7,8,9], [70,80,90], [45,43,68], [112,87,245]])
def caclulate_batch(batch):
pass
for i in range(X.shape[0]/2):
batch = np.array([])
for _ in range(2):
r = random.randint(0, 5)
batch = np.append(batch, X[r])
caclulate_batch(batch)
There are two problems here: (1) It returns appended array (2) The random number can be repeated which can choose the same row many times. How can modify the code to fit my requirement.
r = np.random.randint(0, len(x), 2) should get you the indices. That lets you use fancy indexing to get the subset: batch = x[r, :].
If you want to accumulate arrays along a new dimension, as your loop does, use np.stack or np.block instead of np.append.
(1) You can use numpy.stack instead of append. EDIT: But this function would be called when you have all your batch in a list like:
list = ([1,2], [3,4])
numpy.stack(list)
# gives [[1,2],
# [3,4]]
(2) You can shuffle X array, loop through the results and extract two by two. Look at numpy.random.shuffle
It would look like that:
S = np.random.shuffle(X)
for i in range(S.shape[0]/2):
batch = S[i*2:i*2+1]
caclulate_batch(batch)
I have a couple List<string>s, with the format like this:
List 1 List 2 List 3
1 A One
2 B Two
3 C Three
4 D Four
5 E Five
So in code form, it's like:
List<string> list1 = {"1","2","3","4","5"};
List<string> list2 = {"A","B","C","D","E"};
List<string> list3 = {"One","Two","Three","Four","Five"};
My questions are:
How do I transfom those three lists to a CSV format?
list1,list2,list3
1,A,one
2,b,two
3,c,three
4,d,four
5,e,five
Should I append , to the end of each index or make the delimeter its own index within the multidimensional list?
If performance is your main concern, I would use an existing csv library for your language, as it's probably been pretty well optimized.
If that's too much overhead, and you just want a simple function, I use the same concept in some of my code. I use the join/implode function of a language to create a list of comma separated strings, then join that list with \n.
I'm used to doing this in a dynamic language, but you can see the concept in the following pseudocode example:
header = {"List1", "List2", "List3"}
list1 = {"1","2","3","4","5"};
list2 = {"A","B","C","D","E"};
list3 = {"One","Two","Three","Four","Five"};
values = {header, list1, list2, list3};
for index in values
values[index] = values[index].join(",");
values = values.join("\n");
I am using arguments from command line which is in the form of an Array and I would like to convert that into a Map.
So for example when I run my code with scala abc.scala A 10 B 20 C 30 I want to have a Map(A->10, B->20, C->30). Also I can use only val so I cannot reassign it because it is immutable. I am using the following piece of code unsuccessfully:
val names = args.filter(x => for(i <- 0 to args.length-1) i%2==0)
val numbers = args.partition(args(i) => i%2==1)
names.zip(numbers).toMap
You want grouped:
args.grouped(2).map { case Array(n,v) => (n,v) }.toMap
You're using for in completely the wrong way. That runs a new iteration every element of your args. If you want an index that you can work off of, try args.zipWithIndex (which pairs an index with each element). Alternatively, look at args.grouped(2).