Say I've got two 1-dimensional lists
A = {1,2,3}
B = {a,b,c}
How do I
- create a list of all pairs from list
A
such that the result looks like{{1,2},{1,3},{2,3}}
? - create a list of all pairs from lists
A
andB
such that the result looks like{{1,a},{1,b},{1,c},{2,a},{2,b},{2,c},{3,a},{3,b},{3,c}}
?
I would also need both options, one with no duplicates as above and one with duplicates, e.g. one where the result from case 1 also includes {{2,1},{3,1},{3,2}}
.
Answer
Ad. 1
ordering not valid:
Subsets[A, {2}]
{{1, 2}, {1, 3}, {2, 3}}
or with ordering
Permutations[A, {2}]
Ad. 2
Outer[List, A, B]
{{{1, a}, {1, b}, {1, c}}, {{2, a}, {2, b}, {2, c}}, {{3, a}, {3, b}, {3, c}}}
or exactly
Outer[List, A, B] // Flatten[#, 1] &
{{1, a}, {1, b}, {1, c}, {2, a}, {2, b}, {2, c}, {3, a}, {3, b}, {3, c}}
Comments
Post a Comment