• Post Reply Bookmark Topic Watch Topic
  • New Topic

splitting up lists  RSS feed

 
rosin tuck
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a list composed of lists of lists, looking this:
[[[hello, one, two, three],[four, to, the][six]],[[k][block, three, nine]],...
and so on.
What i need to do is seperate each individual list of lists, as below,
[[hello, one, two, three],[four, to, the],[six]]
[[k],[block, three, nine]]....

I then need to merge the individual lists inside thes lists together, like below:
[hello, one, two, three, four, to, the, six]
[k, block, three, nine]

I have written some code to iterate through the list, and find the inner list of list, which i want to merge together.


I dont however know what to do from here, i cannot find a way to store just the individual inner lists in seperate lists, so they can then be iterated through and their inside lists merged together. Does anybody know how to do this, or something to the same effect? I would greatly appreciate any help
Thanks
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need to implement is recursive calls with function returning the tokens. Let's say you have a list like below
[a,b,[abc,def],c,[ghi,[klmn,opq]]]

You need to do depth first traversal. So you will have something



The above function returns list of elements in a string with comma seperated list. From the returned string value you can split and convert it back to List.
 
rosin tuck
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick response! That is very close to what i am trying to do. The output now is the entire list of lists, combined together forming one list. What i need to do however is have a seperate list for each of the lists inside the outer list.
So using if your example contained another list like below:
[[a,b,[abc,def],c],[[ghi,[klmn,opq]]]]

it would return
[a,b,[abc,def],c]
[ghi,[klmn,opq]]

Do you know how to make this distinction between the lists?
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the example as below:


The output is -
a,b,abc,def,c,ghi,klmn,opq,

suppose you want get the out like
(a,b,abc,def,c) and (ghi,klmn,opq)

then you have to call the splitList function with outer iterator.
something like



If this is not what you are expecting explain with an example what is the expected output.
 
rosin tuck
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry im just trying to test that code but i cant get it to work. Not sure i follow the last bit you sent

Iterator itr = fin.iterator();
while(itr.hasNext())
{
Object obj = itr.next();
if (obj instanceof List)
System.out.println(splitList(fin,1));
else
System.out.println(obj.toString());
}


how come there is a 1 in the line splitList(fin,1)?
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that, it's mistake remove the second parameter. It should work fine.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!