I have two lists: list1 and list2:
list1 = {{"TSC" \[DirectedEdge] "FIN", 22}, {"CO12" \[DirectedEdge] "AGF",
21}, {"MA2" \[DirectedEdge] "EGW", 20}, {"MA1" \[DirectedEdge] "FIN",
19}, {"FIN" \[DirectedEdge] "CO12", 18}, {"EGW" \[DirectedEdge] "TSC",
17}, {"FIN" \[DirectedEdge] "MA2", 16}, {"CO12" \[DirectedEdge] "MA1",
14}, {"AGF" \[DirectedEdge] "MA2", 13}, {"MA2" \[DirectedEdge] "AGF",
12}, {"AGF" \[DirectedEdge] "CO12", 11}, {"CST" \[DirectedEdge] "MA2",
10}, {"EST" \[DirectedEdge] "CO12", 9}, {"MA2" \[DirectedEdge] "CST",
8}, {"FIN" \[DirectedEdge] "TSC", 7}, {"EST" \[DirectedEdge] "MA1",
6}, {"EGW" \[DirectedEdge] "CST", 5}, {"CST" \[DirectedEdge] "EGW",
4}, {"AGF" \[DirectedEdge] "CST", 2}};
list2 = {"MA2" [DirectedEdge] "AGF", "AGF" [DirectedEdge] "MA2",
"AGF" [DirectedEdge] "CST", "FIN" [DirectedEdge] "CO12",
"MA1" [DirectedEdge] "FIN", "FIN" [DirectedEdge] "MA2",
"EGW" [DirectedEdge] "TSC"};
I want to select those {x, y} pairs from list1 using elements (x) in list2, and then take the average of the ys that are already selected. I like to do it in one-liner code.
I tried:
Mean[Select[list1, MemberQ[#, list2] &][[All,2]]]
with no success. I tried many other codes available in this forum but I cannot get what I want.
Select[list1,MemberQ[list2,#[[1]]]&][[All,2]]– Lacia Aug 08 '23 at 14:14