Re: Making a lists with conditions
- To: mathgroup at smc.vnet.net
- Subject: [mg85721] Re: [mg85676] Making a lists with conditions
- From: János <janos.lobb at yale.edu>
- Date: Wed, 20 Feb 2008 06:54:41 -0500 (EST)
- References: <200802190653.BAA27541@smc.vnet.net>
On Feb 19, 2008, at 1:53 AM, Guillermo Sanchez wrote:
> Dear group
>
> I have two list (list1:{a1, ...ai, aj, ..., an}, list2:{{ai,
> bi}, ..., {aj, bj}}.
>
> I wish to obtain a new list (list3} where be included all members of
> list1 and list2. If elements ak is not included in list2 then write
> {ak, 0}.
>
> Here is an example:
>
> list1 = {3.8, 3.85, 3.9, 3.95, 4., 4.05, 4.1};
> list2 = {{3.85, 120}, { 4.0 , 111}, {4.1, 314}};
>
> ff[list1_, list2_] := =BFlist1, list2?
>
>
> Here is the ouput that I wist to obtain
>
> Out:={{3.8, 0} , {3.85`, 120}, {3.9`, 0}, {3.95, 0}, {4.`, 111},
> {4.05`, 0}, {4.1`, 314}}
>
> I have a method but it is two complicate. I am sure that some members
> of the group will find a elegant solution
>
> Thanks
>
> Guillermo
Here is a newbie approach :
In[6]:=
lst3 = Table[If[MemberQ[
(First[#1] & ) /@ lst2,
lst1[[i]]], Select[lst2,
First[#1] == lst1[[
i]] & ], {lst1[[i]],
0}], {i, 1, Length[
lst1]}]
Out[6]=
{{3.8, 0}, {{3.85, 120}},
{3.9, 0}, {3.95, 0},
{{4., 111}}, {4.05, 0},
{{4.1, 314}}}
You can take the Map out into a tmp variable, that way it will be
executing just once.
J=E1nos
----------------------------------------------
Trying to argue with a politician is like lifting up the head of a
corpse.
(S. Lem: His Master Voice)
- References:
- Making a lists with conditions
- From: Guillermo Sanchez <guillermo.sanchez@hotmail.com>
- Making a lists with conditions