Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

addRule

16 views
Skip to first unread message

clansa

unread,
Apr 4, 2012, 4:34:40 AM4/4/12
to
Hello,
I have these data
{{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}}
and I want
{[1->38,1->35,1->37},{2->21},{3->24,3->14}}
The solution is simple when list of data have the same length
But what is the solution for my example
Thanks

Andrew

A Retey

unread,
Apr 5, 2012, 5:44:52 AM4/5/12
to
Hi,
unequal lengths are not that complicated either, thanks to Thread:

Thread[#1 -> #2] & @@@ {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}}

hth,

albert

Adriano Pascoletti

unread,
Apr 5, 2012, 5:46:25 AM4/5/12
to
In[1]:= Thread /@ Apply[Rule, {{1, {38, 35, 37}}, {2, {21}}, {3, {24,
14}}}, {1}]
Out[1]= {{1 -> 38, 1 -> 35, 1 -> 37}, {2 -> 21}, {3 -> 24, 3 -> 14}}


Adriano Pascoletti

2012/4/4 clansa <dauphi...@gmail.com>

> Hello,
> I have these data
> {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}}
> and I want
> {[1->38,1->35,1->37},{2->21},{3->24,3->14}}
> The solution is simple when list of data have the same length
> But what is the solution for my example
> Thanks
>
> Andrew
>
>

Thomas Dowling

unread,
Apr 5, 2012, 5:42:49 AM4/5/12
to
One possibility:

Thread[#[[1]] -> #[[2]]] & /@ mylist

Tom Dowling

Murta

unread,
Apr 5, 2012, 5:43:51 AM4/5/12
to
Hi Andrew!

There is one suggestion
l = {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}};
Rule @@@ Distribute[#, List] & /@ l

{{1 -> 38, 1 -> 35, 1 -> 37}, {2 -> 21}, {3 -> 24, 3 -> 14}}

Best Regards
Murta

Fred Simons

unread,
Apr 5, 2012, 5:43:20 AM4/5/12
to
This works:

Function[{x, y}, Rule[x, y], Listable] @@@ data

Regards,

Fred Simons
Eindhoven University of Technology


Op 4-4-2012 10:31, clansa schreef:

Bob Hanlon

unread,
Apr 5, 2012, 5:47:57 AM4/5/12
to
data = {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}};

Thread[Rule @@ #] & /@ data

{{1 -> 38, 1 -> 35, 1 -> 37}, {2 -> 21}, {3 -> 24, 3 -> 14}}


Bob Hanlon

Ulrich Arndt

unread,
Apr 5, 2012, 5:48:58 AM4/5/12
to
maybe there are more elegant ways but this will do

dat = {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}}
Clear[addRule]
addRule[{a_Integer, b_List}] :=
Apply[Rule, Transpose[{ConstantArray[a, Length[b]], b}], {1}];
Map[addRule, dat]

Ulrich

Ray Koopman

unread,
Apr 5, 2012, 5:53:35 AM4/5/12
to
data = {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}};
Thread /@ Rule @@@ data
Thread[Rule @@ #]& /@ data
Thread @ Rule @ ## & @@@ data

Bob Hanlon

unread,
Apr 5, 2012, 5:58:11 AM4/5/12
to
A slight variation

data = {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}};

Thread /@ Rule @@@ data

{{1 -> 38, 1 -> 35, 1 -> 37}, {2 -> 21}, {3 -> 24, 3 -> 14}}


Bob Hanlon


On Wed, Apr 4, 2012 at 10:19 AM, Bob Hanlon <hanlo...@gmail.com> wrote:
> data = {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}};
>
> Thread[Rule @@ #] & /@ data
>
> {{1 -> 38, 1 -> 35, 1 -> 37}, {2 -> 21}, {3 -> 24, 3 -> 14}}
>
>
> Bob Hanlon

Barrie Stokes

unread,
Apr 5, 2012, 5:59:12 AM4/5/12
to
Hello Andrew

My solution is

data = {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}};

Map[ (t \[Function] Map[ (s \[Function] \!\(\*SubscriptBox[\(t\), \(\(\[LeftDoubleBracket]\)\(\ \)\(1\)\(\ \)\
\(\[RightDoubleBracket]\)\)]\) -> s), \!\(\*SubscriptBox[\(t\), \(\(\[LeftDoubleBracket]\)\(\ \)\(2\)\(\ \)\
\(\[RightDoubleBracket]\)\)]\) ]), data ]

(I know it looks horrible in an email, but it looks fine when pasted into Mathematica.)

Dare I suggest you'll get some more concise code suggested, but if you know how Map works, I think this is transparent.

I use the

Map[ (t \[Function] f[t]), data ]

construction as often as possible. In my code the f[t] is another use of the same construction.

Cheers

Barrie

>>> On 04/04/2012 at 6:31 pm, in message <2012040408...@smc.vnet.net>,

Christoph Lhotka

unread,
Apr 5, 2012, 6:00:14 AM4/5/12
to
hi,

in your example

data = {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}}

I would use a rule of the form

data /. {j_Integer, k_List} :> Thread[j -> k]

to get

{{1 -> 38, 1 -> 35, 1 -> 37}, {2 -> 21}, {3 -> 24, 3 -> 14}}

best,

christoph

Yi Wang

unread,
Apr 5, 2012, 6:01:15 AM4/5/12
to
Hi, Andrew,

You may try

subMap[sub_] := Map[(sub[[1]] -> #) &, sub[[2]] ];
Map[subMap, {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}}]
(* gives {{1 -> 38, 1 -> 35, 1 -> 37}, {2 -> 21}, {3 -> 24, 3 -> 14}} *)

Best,
Yi

Alexei Boulbitch

unread,
Apr 6, 2012, 5:51:57 AM4/6/12
to
Hello,
I have these data
{{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}}
and I want
{[1->38,1->35,1->37},{2->21},{3->24,3->14}}
The solution is simple when list of data have the same length
But what is the solution for my example
Thanks

Andrew


Hi, Andrew,
Try this:

lst = {{1, {38, 35, 37}}, {2, {21}}, {3, {24, 14}}};

Table[Rule[lst[[i, 1]], #] & /@ lst[[i, 2]], {i, 1, Length[lst]}]

{{1 -> 38, 1 -> 35, 1 -> 37}, {2 -> 21}, {3 -> 24, 3 -> 14}}

or this:

g[{a_, list_List}] := Rule[a, #] & /@ list;
Map[g, lst]

{{1 -> 38, 1 -> 35, 1 -> 37}, {2 -> 21}, {3 -> 24, 3 -> 14}}

There should be also a better way, but I failed to guess fast.

Have fun. Alexei


Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.b...@iee.lu





0 new messages