Python: How do i use itertools? -
i trying make list containing possible variations of 1 , 0. example if have 2 digits want list this:
[[0,0], [0,1], [1,0], [1,1]] but if decide have 3 digits want have list this:
[[0,0,0], [0,0,1], [0,1,0], [0,1,1], [1,0,0], [1,0,1], [1,1,0], [1,1,1]] someone told me use itertools, cannot work way want.
>>> list(itertools.permutations((range(2)))) [(0, 1), (1, 0)] >>> [list(itertools.product((range(2))))] [[(0,), (1,)]] is there way this? , question number two, how find documentation on modules this? flailing blindly here
itertools.product() can take second argument: length. defaults one, have seen. simply, can add repeat=n function call:
>>> list(itertools.product(range(2), repeat=3)) [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] to find docs, can either use help(itertools) or quick google (or whatever search engine is) search "itertools python".
Comments
Post a Comment