combinations of nested lists

  • Last Update :
  • Techknowledgy :

What you're looking for is the Cartesian product, in Python itertools.product:

>>>
import itertools
   >>>
   list_of_lists = [
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9]
   ] >>>
   all_possibility = list(itertools.product( * list_of_lists)) >>>
   print(all_possibility)[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8),
      (1, 6, 9), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7),
      (2, 6, 8), (2, 6, 9), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9),
      (3, 6, 7), (3, 6, 8), (3, 6, 9)]

If you want permutations based on the indices rather than the values, you can use itertools.combinations to get the possible indices, then use those indices to get the respective values from the sub-lists, like this:

>>> list_of_lists = [
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9]
   ] >>>
   length = 3 >>>
   all_indices = list(itertools.permutations(range(length), length)) >>>
   all_possibility = [
      [l[i]
         for l, i in zip(list_of_lists, indices)
      ]
      for indices in all_indices
   ] >>>
   print(all_possibility)[[1, 5, 9], [1, 6, 8], [2, 4, 9], [2, 6, 7], [3, 4, 8], [3, 5, 7]]

So you actually just want to get the possible permutations of a “list selector” for each index in the output, i.e. these are what you are trying to get:

>>> list(itertools.permutations(range(3), 3))[(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]

And once you have that, you just need to translate into your list_of_lists where you access each index from the specified sublist:

>>> [
   [list_of_lists[k][i]
      for i, k in enumerate(comb)
   ]
   for comb in itertools.permutations(range(3), 3)
]
[
   [1, 5, 9],
   [1, 8, 6],
   [4, 2, 9],
   [4, 8, 3],
   [7, 2, 6],
   [7, 5, 3]
]

In a spirit of @poke's approach, here is the cases for number of elements differ than the number of the list in the lists. (Previously, there are 3 elements in individual list where 3 sub-lists in the list).

list_of_lists = [
   [1, 2],
   [3, 4],
   [5, 6],
   [7, 8]
]

We expect every (0, 1) pairs from the list of lists, or

all_possibility = [
   [1, 4],
   [1, 6],
   [1, 8],
   [3, 2],
   [3, 6],
   [3, 8], \
   [5, 2],
   [5, 4],
   [5, 8],
   [7, 2],
   [7, 4],
   [7, 6]
]

The code:

permutation_cases = list(itertools.permutations(range(2), 2))
select_from = list(itertools.combinations(range(len(list_of_lists)), 2))

all_possibility = []
for selecting_index in select_from:
   selected = [list_of_lists[i]
      for i in selecting_index
   ]
cases = list([selected[k][i]
      for i, k in enumerate(comb)
   ]
   for comb in permutation_cases)
for item in cases:
   all_possibility.append(item)
print(all_possibility)

Suggestion : 2

Last Updated : 16 Jun, 2021,GATE CS 2021 Syllabus

Example:

List_1 = ["a", "b"]
List_2 = [1, 2]
Unique_combination = [
   [('a', 1), ('b', 2)],
   [('a', 2), ('b', 1)]
]

Suggestion : 3

Hello, I am trying to figure out how to get the result on the right given the list on the left. The list on the left will always contain 4 lists of points ( every list can have more than 1 point ) from which I want to create all possible combinations to use for vertices for quadrilaterals. Thanks!,Thanks @Alien you can check the full graph that I needed this for here Grid subdivision from boundary of polygon ,I’m pretty sure there’s a far simpler way but I did it like this:, Resources Primer Dictionary Developer Builds Events DesignScript Guide Help Center

A = IN[0]

OUT = [reduce(lambda a, b: a + b, i) for i in A]

Suggestion : 4

You can use itertools.chain or anycodings_tuples itertools.chain.from_iterable to combine anycodings_tuples the results from sublists.,How can I iterate over all nested lists? in anycodings_python order to get this expected output (e.g. for anycodings_python first two nested lists): [('a','b'), anycodings_python ('a','c'), ('a','d'), ('b','c'), ('b','d'), anycodings_python ('c','d'), ('a','c'), ('a','d'), ('c','d'), anycodings_python ...],In Python, getting size of images tensorflow2 model trained on?,Docker compose - how to wait for a file to appear or a folder to appear before another service starts to build?

I have a list of the form anycodings_python [['a','b','c','d'], ['a','c','d'], anycodings_python ['b','d','e','f','g','h'], ... anycodings_python ,['c','d','a','b']] and I have to combine anycodings_python the elements of each nested list anycodings_python (separately) in 2-tuples to form a single anycodings_python 2-tuples' list. I have tried to do it with anycodings_python the following code but it only combines the anycodings_python elements of the first list:

def totwotuples(my_list):
   for i in range(len(my_list)):
   for j in range(len(my_list[i])):
   twotuples = itertools.combinations(my_list[i], 2)
return [k
   for k in twotuples
]

You can use itertools.chain or anycodings_tuples itertools.chain.from_iterable to combine anycodings_tuples the results from sublists.

import itertools

lst = [
   ['a', 'b', 'c', 'd'],
   ['a', 'c', 'd']
]

output = itertools.chain.from_iterable(itertools.combinations(sublst, r = 2) for sublst in lst)
output = list(output) # convert to a list
print(output) #[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd'), ('a', 'c'), ('a', 'd'), ('c', 'd')]

Suggestion : 5

Let’s say you have a list that looks like this: ['a', 'b', 'c']. When you create a list of all possible combinations, you’ll end up with a list that looks like this: [ (), ('a',), ('b',), ('c',), ('a', 'b'), ('a', 'c'), ('b', 'c'), ('a', 'b', 'c')]. ,Given a nested list, how to create all possible lists from its elements, while preserving the structure of the nested list?,Note that since the structure stays the same, I need to supply the same amount of elements as in the original list. This is why used rep to repeat the element twice. One could also fill it with NA, I guess., 1 week ago Given a nested list, how to create all possible lists from its elements, while preserving the structure of the nested list? Nested list: ... Putting together the great answers from Ben Nutzer and Joris Chau, we have a way to create all possible combinations from a nested list, regardless of whether some sublist components are of unequal length. ...


l = list(a = list(b = 1: 2), c = list(d = list(e = 3: 4, f = 5: 6)), g = 7)

rl < -as.relistable(l) r < -expand.grid(data.frame(rl), KEEP.OUT.ATTRS = F) > head(r, 5) b c.d.e c.d.f g 1 1 3 5 7 2 2 3 5 7 3 1 4 5 7 4 2 4 5 7 5 1 3 6 7
1._
l = list(a = list(b = 1: 2), c = list(d = list(e = 3: 4, f = 5: 6)), g = 7)
l = list(
a = list(
b = 1:2
),
c = list(
d = list( e = 3:4, f = 5:6
)
),
g = 7 )
# One possible output: list(a = list(b = 1), c = list(d = list(e = 3, f = 5)), g = 7) # Another possible output: list(a = list(b = 1), c = list(d = list(e = 4, f = 5)), g = 7)
rl < -as.relistable(l) r < -expand.grid(data.frame(rl), KEEP.OUT.ATTRS = F) > head(r, 5) b c.d.e c.d.f g 1 135 7 2 235 7 3 145 7 4 245 7 5 136 7
r < -rep(unname(unlist(r[1, ])), each = 2) l2 < -relist(r, skeleton = rl) > l2 $a $a$b[1] 1 1 $c $c$d $c$d$e[1] 3 3 $c$d$f[1] 5 5 $g[1] 7 attr(, "class")[1]
"relistable"
"list"
lapply(1: nrow(r), function(x) relist(rep(unname(unlist(r[x, ])), each = 2), skeleton = rl))