0
$\begingroup$

I've been trying some different approach but all are not working:

I've got 2 polls, the first with 2 options (and 2 list of people which voted each option), the second with 3 options (and 3 list of people). pollA = {pMenoDi25Anni, pPiùDi25Anni}; pollB = {pLavoroFullTime, pStudioEBasta, pLavoroPartTime};

I'd like to calculate the intersection between each element of pollB (each of which is a set) and pMenoDi25Anni.

I've tried using Thread

In: Thread[Intersection[
  pMenoDi25Anni, {pLavoroFullTime, pLavoroPartTime, pStudioEBasta}] ]
Out: {pMenoDi25Anni \[Intersection] pLavoroFullTime, 
 pMenoDi25Anni \[Intersection] pLavoroPartTime, 
 pMenoDi25Anni \[Intersection] pStudioEBasta}

enter image description here

It looks like exactly what I am looking for , but after assigning the real values to the this variables I get an unexpected result pLavoroFullTime = {1, 2}; pLavoroPartTime = {3, 4}; pStudioEBasta = {5, 6}; pMenoDi25Anni = {1, 3, 5};

In: Thread[Intersection[ pMenoDi25Anni, {pLavoroFullTime, pLavoroPartTime, pStudioEBasta}] ] Out: {}

Can you explain me what's going on?

$\endgroup$

1 Answer 1

2
$\begingroup$

In MMA arguments are evaluated before feeding them to a function. This means

Intersection[pMenoDi25Anni, {pLavoroFullTime, pLavoroPartTime, 
  pStudioEBasta}]

is evaluated before feeding to Thread. Now, Intersection takes lists as arguments. This is fine for the first argument, but the second argument is a list of lists. Therefore, the intersection is empty.

What you want is "Map", for short "/@":

Clear[pLavoroFullTime, pLavoroPartTime, pStudioEBasta, pMenoDi25Anni];
pLavoroFullTime = {1, 2}; pLavoroPartTime = {3, 
  4}; pStudioEBasta = {5, 6}; pMenoDi25Anni = {1, 3, 5};
Intersection[pMenoDi25Anni, #] & /@ {pLavoroFullTime, pLavoroPartTime,
   pStudioEBasta}

(*{{1}, {3}, {5}}*)
$\endgroup$
4
  • $\begingroup$ Thanks a lot. To better understand the Wolfram Language, is there any method to generalize this intersection to any List of Lists using Tuples or Outer? mathematica.stackexchange.com/questions/257645/… @Daniel Huber $\endgroup$
    – Revious
    Commented Nov 3, 2021 at 10:34
  • $\begingroup$ I mean Intersection[{pMenoDi25Anni, pMoreThan25YearsOld}, {pLavoroFullTime, pLavoroPartTime, pStudioEBasta}] the 1st element of the 1st list intersect 1st element of second list, then with the second, then with the 3rd and so on $\endgroup$
    – Revious
    Commented Nov 3, 2021 at 10:36
  • 1
    $\begingroup$ Look at "MapThread" $\endgroup$ Commented Nov 3, 2021 at 10:38
  • $\begingroup$ thanks a lot!!! $\endgroup$
    – Revious
    Commented Nov 3, 2021 at 11:18

Your Answer

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

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