Re: Clever way to manipulate lists
- To: mathgroup at smc.vnet.net
- Subject: [mg94395] Re: Clever way to manipulate lists
- From: Raffy <raffy at mac.com>
- Date: Thu, 11 Dec 2008 07:28:48 -0500 (EST)
- References: <ghqjve$14i$1@smc.vnet.net>
(* Create some random data *) list1 = Transpose@{Range[10], RandomInteger[{1, 100}, 10]} list2 = Transpose@{RandomSample[Range[10], 6], RandomInteger[{101, 200}, 6]} (* Just for the sake of testing, verify that list2 keys are contained in list1 *) Complement[list2[[All, 1]], list1[[All, 1]]] (* Create a map from list1 keys to values *) map = Rule @@@ list1 (* Get list2 keys *) keys = list2[[All, 1]] (* Map list2 keys to list1 values *) values = keys /. map (* Build up a new list1 *) list1new = Transpose@{keys,values } Or, as one line: Transpose@{list2[[All, 1]], list2[[All, 1]] /. Rule @@@ list1} Dispatch[] can be used if you apply the rules more than once.