• Home
  • About Us
  • Contact Us
  • Privacy Policy
  • Special Offers
Business Intelligence Info
  • Business Intelligence
    • BI News and Info
    • Big Data
    • Mobile and Cloud
    • Self-Service BI
  • CRM
    • CRM News and Info
    • InfusionSoft
    • Microsoft Dynamics CRM
    • NetSuite
    • OnContact
    • Salesforce
    • Workbooks
  • Data Mining
    • Pentaho
    • Sisense
    • Tableau
    • TIBCO Spotfire
  • Data Warehousing
    • DWH News and Info
    • IBM DB2
    • Microsoft SQL Server
    • Oracle
    • Teradata
  • Predictive Analytics
    • FICO
    • KNIME
    • Mathematica
    • Matlab
    • Minitab
    • RapidMiner
    • Revolution
    • SAP
    • SAS/SPSS
  • Humor

How to combine a lists of two lists with a list of two lists of lists of varying length?

January 10, 2020   BI News and Info
 How to combine a lists of two lists with a list of two lists of lists of varying length?

This is probably asked before but I don’t know how to begin searching for it.

Some context: I’m using TemporalData objects that share a number of common features but also have distinct characteristics. I use their MetaInformation to catalog these features for future reference. The output of the desired function will be used as the second argument in Rule[Metainformation, #].

I’m trying to accomplish the following: given a list of common keys and values (common:{keys_, values_}) and another list consisting of two elements namely a list of lists of distinct keys and another list of lists of their corresponding distinct values (unique:{allKeys_, allValues_}) I want to produce a list of lists of rules where all lists of rules will contain both the common and the corresponding distinctive features.

An example: Assuming f is the desired function, then

in = {
  { {"a","b","c"}, {1,2,3} },
  { {{"d"}, {"e","f"}, {"g","h","i"}}, {{4}, {5,6}, {7,8,9}} }
 };

f @@ in

is expected to produce out, where out is defined as follows:

out = {
  {"a"->1, "b"->2, "c"->3, "d"->4},
  {"a"->1, "b"->2, "c"->3, "e"->5, "f"->6},
  {"a"->1, "b"->2, "c"->3, "g"->7, "h"->8, "i"->9}
 }

(Please note that the number of elements in the lists of common and the corresponding elements in the lists of unique is expected to be random. I used the example above just for ease of exposition.)

I have developed 3 function versions that seem to do what I want but they all feel a bit clunky; also, I think they are too similar. Furthermore, only the second one is a proper function.

If there are any suggestions on the presented code or-better yet-if there are any other ideas on how to code f, that would be great!

Some f‘s

  • 1.

    f[common:{keys_, values_}, unique:{allKeys_, allValues_}] := 
      h[Apply[g /* List, common], Thread[Apply[g, unique]]]
    

    and

    {out} === (f[Sequence @@ in] /. h[x__] :> 
     Outer[j /* (Thread[#, g] &), x] /. {j -> Join, g -> Rule /* Thread})
    

    evaluates to

    True
    
  • 2.

    f[common:{keys_, values_}, unique:{allKeys_, allValues_}] := Outer[
      List /* Transpose /* Apply[Rule /* (Apply[Join, #, 1] &) /* Thread], 
        {common}, Transpose[unique], 1]
    

    and

    {out} == f @@ in
    

    evaluates to

    True
    
  • 3.

    f[common:{keys_, values_}, unique:{allKeys_, allValues_}] := 
      Apply[Rule][Thread[g[common, unique]]]
    

    and

    out === (
      ( f @@ in /. g[x_, y_] :> ReleaseHold[
          Distribute[Hold[Join][{x}, y], List]
         ] ) // Thread /* Map[Thread]
     )
    

    evaluates to

    True
    

Let’s block ads! (Why?)

Recent Questions – Mathematica Stack Exchange

combine, length, List, Lists, varying
  • Recent Posts

    • The Easier Way For Banks To Handle Data Security While Working Remotely
    • 3 Ways Data Virtualization is Evolving to Meet Market Demands
    • Did you find everything you need today?
    • Missing Form Editor through command bar in Microsoft Dynamics 365
    • I’m So Excited
  • Categories

  • Archives

    • April 2021
    • March 2021
    • February 2021
    • January 2021
    • December 2020
    • November 2020
    • October 2020
    • September 2020
    • August 2020
    • July 2020
    • June 2020
    • May 2020
    • April 2020
    • March 2020
    • February 2020
    • January 2020
    • December 2019
    • November 2019
    • October 2019
    • September 2019
    • August 2019
    • July 2019
    • June 2019
    • May 2019
    • April 2019
    • March 2019
    • February 2019
    • January 2019
    • December 2018
    • November 2018
    • October 2018
    • September 2018
    • August 2018
    • July 2018
    • June 2018
    • May 2018
    • April 2018
    • March 2018
    • February 2018
    • January 2018
    • December 2017
    • November 2017
    • October 2017
    • September 2017
    • August 2017
    • July 2017
    • June 2017
    • May 2017
    • April 2017
    • March 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    • October 2016
    • September 2016
    • August 2016
    • July 2016
    • June 2016
    • May 2016
    • April 2016
    • March 2016
    • February 2016
    • January 2016
    • December 2015
    • November 2015
    • October 2015
    • September 2015
    • August 2015
    • July 2015
    • June 2015
    • May 2015
    • April 2015
    • March 2015
    • February 2015
    • January 2015
    • December 2014
    • November 2014
© 2021 Business Intelligence Info
Power BI Training | G Com Solutions Limited