Is there an algorithm to merge this kind of list?

I have a list like this:

a   a   .   a
b   .   .   a
a   .   a   .
c   .   a   .

or in list format

[['a', 'a', '.', 'a'],
 ['b', '.', '.', 'a'],
 ['a', '.', 'a', '.'],
 ['c', '.', 'a', '.']]

and I want to merge it into [['a','b','c'],['a'],['a'],['a']], or

a,b,c    a    a    a

so that when two consecutive rows share the same letter at any of the four columns, their elements will be merged non-redundantly. I can do it by pairwise comparisons, but wonder if there are formal algorithms to do this?

Thanks.

Answers


You didn't specify the language but you can create a HashMap / HashTable for each column and populate it with the column values. (Your key and value will be the same thing.) Populating a HashMap means you cannot have duplicate keys so you will end uo with a list of unique values in each collection. Then pull out the values from each hashMap into an array, or arrays. If the periods in your sample data are actually periods you will have to ignore them as you loop through the array otherwise you will get them as output.

Take a look at Python dictionaries.

the pseudo code for this solution (Python will look similar ;-)

  • Create a list of dictionaries (list length = # of columns)
  • Loop over columns
  • Loop over rows
  • Insert data into appropriate dictionary *
  • Loop over list of dictionaries
  • Loop over dictionary values
  • Create new set of arrays with unique values

Need Your Help

Moving Dotted line for cropping

c# wpf marching-ants

I know that I can create the dashed line with the following code.

Export JSP to Word With External CSS

html css jsp

I am trying to export a JSP generated document webpage to Microsoft Word. In the web browser, everything looks great. However, when I change

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.