3
$\begingroup$

I have two lists:

a={{"2021-01-04", 12.48, 12.49, 10.53, 11.01, 11.01,35826600}, {"2021-01-05", 10.7, 14.3, 10.69, 13.59, 13.59,61984100}, {"2021-01-06", 14.58, 17.58, 14.25, 16.96, 16.96,83119900}, {"2021-01-07", 20.18, 24.35, 18.55, 22.36, 22.36,118963600}, {"2021-01-08", 25.06, 28.37, 23.8, 26.39, 26.39,97428200}, {"2021-01-11", 20., 25.44, 19.6, 23.36, 23.36,93551100}, {"2021-01-12", 24., 26.92, 22.81, 26.15, 26.15,57414200}, {"2021-01-13", 22.39, 23.66, 21.65, 23., 23.,69463000}, {"2021-01-14", 25.91, 26.55, 23.73, 24.44, 24.44,55343600}};

and

b={{"2021-01-01", 28994., 29600.6, 28803.6, 29374.2, 29374.2,40730301359}, {"2021-01-02", 29376.5, 33155.1, 29091.2, 32127.3,32127.3, 67865420765}, {"2021-01-03", 32129.4,34608.6,32052.3,32782., 32782., 78665235202}, {"2021-01-04", 32810.9, 33440.2,28722.8, 31971.9, 31971.9, 81163475344}, {"2021-01-05", 31977.,34437.6, 30221.2, 33992.4, 33992.4, 67547324782}, {"2021-01-06",34013.6, 36879.7, 33514., 36824.4, 36824.4,75289433811}, {"2021-01-07", 36833.9, 40180.4, 36491.2, 39371.,39371., 84762141031}, {"2021-01-08", 39381.8, 41946.7, 36838.6,40797.6, 40797.6, 88107519480}, {"2021-01-09", 40788.6, 41436.4,38980.9, 40254.5, 40254.5, 61984162837}}

I would like to select the elements of b where the first element is a member of the first column of a; i.e., a[[All,1]].

$\endgroup$

3 Answers 3

6
$\begingroup$

Is this what you mean?

Select[b, MemberQ[a[[All, 1]], #[[1]]] &] 

Mathematica graphics

$\endgroup$
2
$\begingroup$
  1. Find the intersection of the first column of a and b called alist and blist respectively.
  2. Find the position of each of the common items in the first column of b.
  3. Extract the corresponding rows from b.
    alist = a[[All, 1]];
    blist = b[[All, 1]];
    res1 = Flatten[#, 1] &@
      Extract[b, Position[blist, #] & /@ Intersection[alist, blist]]

  1. Pick the row from b if the member in the first column of b is in the first column of a
    res2 = Pick[b, (MemberQ[alist, #] & /@ blist)]

    res3 = Cases[b, {x_String, __} /; MemberQ[a[[All, 1]], x]]

Result

$${{{"2021-01-04", 32810.9, 33440.2, 28722.8, 31971.9, 31971.9, 81163475344}}, {{"2021-01-05", 31977., 34437.6, 30221.2, 33992.4, 33992.4, 67547324782}}, {{"2021-01-06", 34013.6, 36879.7, 33514., 36824.4, 36824.4, 75289433811}}, {{"2021-01-07", 36833.9, 40180.4, 36491.2, 39371., 39371., 84762141031}}, {{"2021-01-08", 39381.8, 41946.7, 36838.6, 40797.6, 40797.6, 88107519480}}}$$


res1 == res2 == res3

True

$\endgroup$
1
$\begingroup$

Using Table and IntersectingQ:

First[#]&/@DeleteCases[Table[If[IntersectingQ[a[[i]], b[[j]]] === True, b[[j]], Nothing], {i, 1, Length[a]}, {j, 1, Length[b]}], {}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.