Splitting a list into two lists in scala -
i have list containing like:
val lines: list[string] = list("bla blub -- id_1", "sdkfjdf -- id_2", "blubber blab -- id_1", "foo -- id_3", "ieriuer -- id_2", "bar -- id_3")
so list contains identifier exists twice (id_x) , string belongs 1 of identifiers.
i want split 1 list 2 lists each contains unique set of id_s belonging strings, this:
l1("bla blub -- id_1", "sdkfjdf -- id_2", "foo -- id_3") l2("blubber blab -- id_1", "ieriuer -- id_2", "bar -- id_3")
how in functional way?
best regards, sven
lines.groupby(_.split(" -- ")(1)).tolist.map(_._2).transpose
that's rough , ready way it; in reality if want more data it'll better parse items case class, la:
case class item(id: string, text: string) val items = { line <- lines array(text, id) = line.split(" -- ") } yield item(id, text)
then same above, except groupby(_.id)
, , conveniently sortby(_.id)
well.
Comments
Post a Comment