Converting a List into a Nested List in Scala - list

How can I convert for example
List(2, 5, 24, 7, 34, 8)
into
List(2, List(5, List(24, List(7, List(34, List(8))))))
I guess it is about folding right but couldn't figure it out.

x.foldRight(List[Any]())((a,b) => List(a,b)) will produce (2, (5, (24, (7, (34, (8, Nil)))))). If you really need the (34, 8) at the end, you can change the folding function to use some pattern matching.

Related

how to concatenate the coordinates of two sublist as pair list?

I have the following sublist format:
x = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
y = [[11, 22, 33, 44], [55, 66, 77, 88], [99,100, 111, 122]...]
Above is a sublist containing the information of the "x" and "y" coordinates (the length can be changed accordingly).
Now I like take two sets and make pair of coordinates as following:
x_y = [[(1,11),(2,22),(3,33),(4,44)],[(5,55),(6,66),(7,77),(8,88)],[(9,99),(10,100),(11,111),(12,122)...]
any help would be appreciated. Thanks.
You could use zip twice:
x_y = [zip(a, b) for a, b in zip(x, y)]
print(x_y)
Output:
[[(1, 11), (2, 22), (3, 33), (4, 44)], [(5, 55), (6, 66), (7, 77), (8, 88)], [(9, 99), (10, 100), (11, 111), (12, 122)]]

Zip over a 2D List in Haskell

I want to combine two 2-dimensional lists in Haskell using zip, i.e. achieve something like this
[[1,2,3], [[10, 11, 12], [[(1, 10), (2, 11), (3, 12)],
[4,5,6], `zip'` [13, 14, 15], -> [(4, 13), (5, 14), (6, 15)],
[7,8,9]] [16, 17, 18]] [(7, 16), (8, 17), (9, 18)]]
without using any functions outside the Prelude. Is there a way this can be done, using map perhaps? The problem is that one cannot map zip over two lists.
zip' = zipWith zip
Nice, isn't it?

Scala, prepend a list of variables to the beginning of each list in a list

I am new to scala and want to get the following thing done using map, flatMap, and/or for comprehension.
I have a list of lists l = List[List[T]]. For example, l = [[1,2,3],[2,4,6,4],[3,4,6,2,3]]. Note that each list inside l can have varying length.
Now I have val x: List[Int] = [1,2,3] and I want to do some operation on x and l that returns [[1,1,2,3], [1,2,4,6,4], [1,3,4,6,2,3], [2,1,2,3], [2,2,4,6,4], [2,3,4,6,2,3], [3,1,2,3], [3,2,4,6,4], [3,3,4,6,2,3]] (the order of sublists doesn't matter).
I feel like I should use map or flatMap or for-loop to do this but after a long time of trial I can't even get the type correct. Can anyone help me on it?
scala> val ls = List(List(1,2,3),List(2,4,6,4),List(3,4,6,2,3))
ls: List[List[Int]] = List(List(1, 2, 3), List(2, 4, 6, 4), List(3, 4, 6, 2, 3))
scala> val xs: List[Int] = List(1,2,3)
xs: List[Int] = List(1, 2, 3)
scala> for(x <- xs; l <- ls) yield x +: l
res22: List[List[Int]] = List(List(1, 1, 2, 3), List(1, 2, 4, 6, 4), List(1, 3, 4, 6, 2, 3), List(2, 1, 2, 3), List(2, 2, 4, 6, 4), List(2, 3, 4, 6, 2, 3), List(3, 1, 2, 3), List(3, 2, 4, 6, 4), List(3, 3, 4, 6, 2, 3))
x.flatMap(i => l.map(i::_))

how to iterate through lists vertically?

I have multiple lists to work with. What I'm trying to do is to take a certain index for every list(in this case index 1,2,and 3), in a vertical column. And add those vertical numbers to an empty list.
line1=[1,2,3,4,5,5,6]
line2=[3,5,7,8,9,6,4]
line3=[5,6,3,7,8,3,7]
vlist1=[]
vlist2=[]
vlist3=[]
expected output
Vlist1=[1,3,5]
Vlist2=[2,5,6]
Vlist3=[3,7,3]
Having variables with numbers in them is often a design mistake. Instead, you should probably have a nested data structure. If you do that with your line1, line2 and line3 lists, you'd get a nested list:
lines = [[1,2,3,4,5,5,6],
[3,5,7,8,9,6,4],
[5,6,3,7,8,3,7]]
You can then "transpose" this list of lists with zip:
vlist = list(zip(*lines)) # note the list call is not needed in Python 2
Now you can access the inner lists (which in are actually tuples this now) by indexing or slicing into the transposed list.
first_three_vlists = vlist[:3]
in python 3 zip returns a generator object, you need to treat it like one:
from itertools import islice
vlist1,vlist2,vlist3 = islice(zip(line1,line2,line3),3)
But really you should keep your data out of your variable names. Use a list-of-lists data structure, and if you need to transpose it just do:
list(zip(*nested_list))
Out[13]: [(1, 3, 5), (2, 5, 6), (3, 7, 3), (4, 8, 7), (5, 9, 8), (5, 6, 3), (6, 4, 7)]
Use pythons zip() function, index accordingly.
>>> line1=[1,2,3,4,5,5,6]
>>> line2=[3,5,7,8,9,6,4]
>>> line3=[5,6,3,7,8,3,7]
>>> zip(line1,line2,line3)
[(1, 3, 5), (2, 5, 6), (3, 7, 3), (4, 8, 7), (5, 9, 8), (5, 6, 3), (6, 4, 7)]
Put your input lists into a list. Then to create the ith vlist, do something like this:
vlist[i] = [];
for l in list_of_lists:
vlist[i].append(l[i])

Accessing a particular column in scala [duplicate]

This question already has answers here:
Scala Lists of lists of integers
(2 answers)
Closed 8 years ago.
I have a list of list (of type int) and I need to return the first / second column etc depending on the input given as column index. Any ideas on how to access the item of each row of that particular column? I have attempted it but all I came to is using a map function but I don't know how it works exactly. Thanks in advance
One option:
def getColumn[T](list: List[List[T]], column: Int): List[T] = {
list.map(row => row(column))
}
Then:
scala> val l = List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9))
l: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9))
scala> getColumn(l, 2)
res0: List[Int] = List(3, 6, 9)
List types can be accessed by index number
scala> val a=List(1,3,5,7,11,13);
a: List[Int] = List(1, 3, 5, 7, 11, 13)
scala> a(1)
res0: Int = 3
scala> a(2)
res1: Int = 5
Multidimensional Lists work too
scala> val a=List(List(1,3,5,7,11,13), List(2,4,6,8,10,12));
a: List[List[Int]] = List(List(1, 3, 5, 7, 11, 13), List(2, 4, 6, 8, 10, 12))
scala> a
res2: List[List[Int]] = List(List(1, 3, 5, 7, 11, 13), List(2, 4, 6, 8, 10, 12))
scala> a(1)
res3: List[Int] = List(2, 4, 6, 8, 10, 12)
scala> a(1)(2)
res4: Int = 6
update to answer your comment, function that takes a List of Lists of Ints and a column number
scala> val a=List(List(1,3,5,7,11,13), List(2,4,6,8,10,12), List(0,10,20,30,40,50,60));
a: List[List[Int]] = List(List(1, 3, 5, 7, 11, 13), List(2, 4, 6, 8, 10, 12), List(0, 10, 20, 30, 40, 50, 60))
scala> def getcol(l: List[List[Int]], n: Int) = (for (i <- l) yield i(n)).toList
getcol: (l: List[List[Int]], n: Int)List[Int]
scala> getcol(a,0)
res17: List[Int] = List(1, 2, 0)
scala> getcol(a,1)
res18: List[Int] = List(3, 4, 10)
val nums = List(List(1,2,3),List(4,5,6))
nums map(_(1)) // gives List(2,5)
Or in a method with colIndex parameter:
def sliceCol[T](ls: List[List[T]], colIndex: Int): List[T] =
ls map(_(colIndex))
sliceCols(1) // same result