'Best way to get a list of lists output with itertools
Summary: Is there a way to change the output format of itertools from list of tuples to list of lists, similar to making parameter changes using matplotlib.rcParams for matplotlib?
I want to find all possible combinations of elements of a set A with repeats allowed as long as the specific combination is not an element of another set B, in the form of list of lists. The rest of the code works with lists rather than tuples, so it looks more suitable for my preference.
I have this short code snippet, utilising the list comprehension.
A=list(range(5))
B=[[0,1],[1,2]]
C=list([list(i) for i in itertools.product(A, A) if i not in B])
While this works alright and certainly presentable, I feel it might be slow. I also dropped itertools for double for looped list comprehension.
A=list(range(5))
B=[[0,1],[1,2]]
C=[[i,j] for i in A for j in A if [i,j] not in B]
This doesn't look efficient either. Also, even though I expect the set A to be small, I want to know if there is an efficient way to do it.
Is there a way to change the output format of itertools, similar to making parameter changes using matplotlib.rcParams for matplotlib?
Thanks in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
