Hello I need all permutations of a List l = [1,2,...,n] with length m.
from itertools import permutations
def calcPerm(l:list,m: int)
perm=[]
for i in permutations(l, m):
perm.append(list((i)))
But like u see for n great enough the memory will explode.
Is there a way where I do not have to save does permutations, so i can use every single one
immediately but not get them twice( e.g [1,2] and [2,1] ?
You must use combinations instead of permutations:
from itertools import combinations, permutations
my_list = [1,2,3]
print(list(permutations(my_list, 2)))
#[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
print(list(combinations(my_list, 2)))
#[(1, 2), (1, 3), (2, 3)]
Use itertools.combinations and to save memory use yield:
from itertools import combinations
def calcPerm(l: list, m: int):
for i in combinations(l, m):
yield list((i))
for p in calcPerm(range(10), 5):
print(p)
Related
I have this code that creates permutations of a given number. It also give the permuatations based on the number of specified digits, so 2 would give the permutations of all possible 2 digit values. I have this looped so for a 4 digit number, it would loop giving all permutations scenarios, like 4,3,2 and 1 digit permutations scenarios. The problem im having is how to store the perm variable which stores the permutations. I tried making a multi array perm, then as the loop iterates it adds the new array to the perm. Didn't work because the arrays are different sizes. How can I continue?
def fp(number):
# A Python program to print all
# permutations using library function
from itertools import permutations
# Get all permutations of [1, 2, 3]
c= list(map(int,str(number)))
print(c, len(c))
i=1
while i <= len(c):
perm= permutations(c,i) #permuate the number c to the number of specified digits i
i+=1
# Print the obtained permutations
for i in list(perm):
print (i)
You are searching for a powerset, search in these functions by itertools for powerset. I just changed the combinations to permutations.
Then loop through all permutations and append them to a list (you could also use a dictionary)
import itertools
def powerset(iterable):
s = list(iterable)
return itertools.chain.from_iterable(itertools.permutations(s, r) for r in range(1,len(s)+1))
lst_of_numbers = [1, 2, 3]
out = []
for perm in powerset(lst_of_numbers):
out.append(perm)
print(out)
[(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2), (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
Use another variable to hold all the permutations. Extend it with the list returned by each call to permutations().
def fp(number):
# A Python program to print all
# permutations using library function
from itertools import permutations
all_perms = []
c= list(map(int,str(number)))
print(c, len(c))
i=1
while i <= len(c):
perm= permutations(c,i) #permuate the number c to the number of specified digits i
all_perms.extend(perm)
i+=1
# Print the obtained permutations
for i in all_perms:
print (i)
I am trying to write a program to return sublist of a list i.e. for a list [1,2,3], the program should return [1],[2],[3],[1,2],[2,3] and [1,2,3].
I know of the concept of dictionary as well apart from list. So, can somebody instruct me on how i can solve this problem so that I could implement the same concept in other similar problems?
There is an implementation here
from itertools import chain, combinations
def powerset(iterable):
xs = list(iterable)
# note we return an iterator rather than a list
return chain.from_iterable( combinations(xs,n) for n in range(len(xs)+1) )
>>> list(powerset([1,2,3]))
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
If you don't want the empty element:
>>> list(filter(lambda x: len(x) >= 1, powerset([1,2,3])))
[(1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
I would like to change my data-structure that get from my data-files in such a way that I get a list of all coordinate-values for every coordinates (so a list for all coordinates filled with values)
e.g.
for i in range (files):
open file
file_output = [[0,4,6],[9,4,1],[2,5,3]]
second loop
file_output = [[6,1,8],[4,7,3],[3,7,0]]
to
coordinates = [[0,6],[4,1],[6,8],[9,4],[4,7],[1,3],[2,3],[5,7],[3,0]]
It should be noted that I use over 1000 files of this format, which I should merge.
You could also explore the built-in zip() function
>>> l = []
>>> for k,v in zip(a,b):
l.append(zip(k,v))
>>> print l
[[0,6],[4,1],[6,8],[9,4],[4,7],[1,3],[2,3],[5,7],[3,0]]
>>> a = [[0,4,6],[9,4,1],[2,5,3]]
>>> b = [[6,1,8],[4,7,3],[3,7,0]]
>>> from itertools import chain
>>> zip(chain(*a),chain(*b))
[(0, 6), (4, 1), (6, 8), (9, 4), (4, 7), (1, 3), (2, 3), (5, 7), (3, 0)]
>>>
This should be useful.
[zip(i,j) for i in a for j in b]
However it provides list of tuples, which should satisfy your needs.
If there will only be two lists, you can use this as well.
[[i, j] for i in a for j in b]
I have n lists of different lengths of wich I want to create all possible permutations.
so e.g. if a=[1,2] and b=[3,4,5] then I would love to obtain res=[[1,3],[1,4],[1,5],[2,3],[2,4],[2,5]]
I've been trying to achieve this using a recursive function, which turned out to be neither very efficient nor very pythonic.
How would an experienced python programmer tackle the problem?
It's called the Cartesian product of two sequences.
This is already available in Python as a library function: itertools.product.
Example:
>>> import itertools
>>> a = [1, 2]
>>> b = [3, 4, 5]
>>> list(itertools.product(a, b))
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5)]
you can do this by product function in itertools,
import itertools
a = [1,2]
b = [3, 4, 5]
out = list(itertools.product(a,b))
print out
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5)]
itertools is definitely the way to go but if you don't want to go the easy route.....
def print_permutations(lists, perms=[]):
if not lists:
print perms
else:
current_layer = lists[0]
remaining_layers = lists[1:]
for word in current_layer:
print_permutations(remaining_layers, perms + [word])
l = (('quick', 'lazy'), ('brown', 'black', 'grey'), ('fox', 'dog'))
print_permutations(l)
from itertools import combinations
a = [1,2,3]
combinations(a,2) #will give me ((1,2),(1,3),(2,3))
combinations(a,3) #will give me ((1,2,3),)
but what if I want results of different length which is in a array
e.g.
I want to find all combinations of given array[1,2,3] of length more than or equal to 2
so result should be ((1,2),(1,3),(2,3),(1,2,3))
something like c = combinations(a,>=2)
I tried to use lambda but its not working
c = combinations(a,lambda x: x for x in [2,3])
as well list comprehensive c = combinations(a,[x for x in [2,3]])
I know I can use a simple loop and then find out the combinations of diff length.
for l in [2,3]:
combinations(a,l)
But Is there any pythonic way to do this?
You could combine combinations and chain.from_iterable:
>>> from itertools import chain, combinations
>>> a = [1,2,3]
>>> n = 2
>>> cc = chain.from_iterable(combinations(a, i) for i in range(n, len(a)+1))
>>> list(cc)
[(1, 2), (1, 3), (2, 3), (1, 2, 3)]
chain.from_iterable here is flattening what the generator expression (combinations(a, i) for i in range(n, len(a)+1)) is producing. Otherwise you'd wind up with something like
>>> [list(combinations(a,i)) for i in range(n, len(a)+1)]
[[(1, 2), (1, 3), (2, 3)], [(1, 2, 3)]]
which is fine, but not quite in the format you were looking for.