Re: Making a lists with conditions

• To: mathgroup at smc.vnet.net
• Subject: [mg85710] Re: Making a lists with conditions
• From: Albert Retey <awnl at arcor.net>
• Date: Tue, 19 Feb 2008 07:09:25 -0500 (EST)
• References: <fpduuo\$rd3\$1@smc.vnet.net>

Hi,

> 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}}

This uses pattern matching, so be aware that it does not handle numerical
equivalence as you might expect:

In[44]:= Transpose[{list1,Replace[list1,Append[Rule@@@list2,_->0],{1}]}]

Out[44]= {{3.8,0},{3.85,120},{3.9,0},{3.95,0},{4.,111},{4.05,0},{4.1,314}}

This is what it does: make a list of rules from list2, then replace
everything that is in list2 as lhs with the corresponding rhs, if
nothing matches, replace with 0. Finally it combines the result with the
original list1 by transposing the two lists. The Replace with level
specification is necessary to avoid unwanted matches of e.g. the whole list.

hth,

albert

• Prev by Date: Re: Making a lists with conditions
• Next by Date: RE: Multiple data sets with ListPlot and different PointSizes - Mesh
• Previous by thread: Re: Re: Making a lists with conditions
• Next by thread: Re: Making a lists with conditions