So, say I have a list of lists like
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
How do I get all possible permutations with the restriction that I can only pick 1 item per list? Meaning that 147 or 269 would be possible permutations, whereas 145 would be wrong since 4 and 5 are in the same list. Also, how does this work for a list containing any number of lists?
This works for me in Python 2.7 and 3.5
import itertools
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(itertools.product(*l)))
it returns
[(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)]
this worked in python 3, see the comment on the last line for python 2
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
row1, row2, row3 = l
# get all the permutations as lists [1,4,7], etc.
permutations = ([x, y, z] for x in row1 for y in row2 for z in row3)
# get strings to have a more easily readable output
permutation_strings = (''.join(map(str, permutation))
for permutation in permutations)
print(*permutation_strings)
# in python2 you can use: print list(permutation_strings)
I wouldn't call what you are looking for permutations, but the following recursive algorithm should return what I assume you would like to see
def get_all_possibilities(S, P=[]):
if S == []:
return P
s = S[0]
if P == []:
for x in s:
P.append(str(x))
return get_all_possibilities(S[1:], P)
else:
new_P = []
for x in s:
for p in P:
new_P.append(p + str(x))
return get_all_possibilities(S[1:], new_P)
print get_all_possibilities([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
My output was the following 27 items which could later be converted back to integers if you like;
['147', '247', '347', '157', '257', '357', '167', '267', '367', '148', '248', '348', '158', '258', '358', '168', '268', '368', '149', '249', '349', '159', '259', '359', '169', '269', '369']
You could use recursion.
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
def permutate(w, l):
for x in l[0]:
if len(l) > 1:
permutate(w + str(x), l[1:])
else:
print w + str(x)
permutate("", l)
You can use itertools for this!
from itertools import product
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(product(*l)))
A few things to note:
I'm passing *l
instead of simply l
as product
expects iterables as arguments, not a list of iterables; another way to write this would have been:
product([1, 2, 3], [4, 5, 6], [7, 8, 9])
ie, passing every list as a single argument. *l
unpacks the elements of l
into arguments for you.
product
does not return a list, but a generator. You can pass that to anything that expects an iterable. "Printing" the generator would not be helpful (you would not see the content of the resulting list, but <itertools.product object...>
which is only mildly interesting); this is why I'm forcing a conversion to a list using list()
print()
with parentheses allows this code to be compatible with Python 2 & 3.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.