Python - New list of permutations with omissions - python

Lets say I have a list of values
my_list = [1,2,3,4]
I user itertools.permutations to find all combinations of this list
perms = itertools.permutations(my_list)
Which creates
[(1, 2, 3, 4),
(1, 2, 4, 3),
(1, 3, 2, 4),
(1, 3, 4, 2),
(1, 4, 2, 3),
(1, 4, 3, 2),
(2, 1, 3, 4),
(2, 1, 4, 3),
(2, 3, 1, 4),
(2, 3, 4, 1),
(2, 4, 1, 3),
(2, 4, 3, 1),
(3, 1, 2, 4),
(3, 1, 4, 2),
(3, 2, 1, 4),
(3, 2, 4, 1),
(3, 4, 1, 2),
(3, 4, 2, 1),
(4, 1, 2, 3),
(4, 1, 3, 2),
(4, 2, 1, 3),
(4, 2, 3, 1),
(4, 3, 1, 2),
(4, 3, 2, 1)]
I begin iterating through this and find that I no longer need any items in perms which begin with (4,1... or (3,1....
How can I recreate this list with those specific omissions? Iterating through and removing items is not viable as this needs to scale to very large sizes.
EDIT: For clarification (4,1,2,3) should be removed as it starts with (4,1...), but not (4,2,1,3) as it starts with (4,2...).

>>> from itertools import permutations
>>> my_list = [1,2,3,4]
>>> perms = permutations(my_list)
>>> perms
<itertools.permutations object at 0x107a63ad0>
>>> perms = filter(lambda x: x[:2] != (4,1) and x[:2] != (3,1), perms)
>>> perms
[(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]

Since you state
Iterating through and removing items is not viable as this needs to
scale to very large sizes.
The best is to wrap the interator produced by permutations that will generate the tuples you want and skip the tuples you do not want:
my_list = [1,2,3,4]
def my_perms(my_list, f):
for e in permutations(my_list):
if f(e):
yield e
>>> list(my_perms(my_list, lambda t: t[:2] not in {(4,1), (3,1)}))
[(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]
Or, use ifilter from itertools:
>>> list(ifilter(lambda t: t[:2] not in {(4,1), (3,1)}, permutations(my_list)))
Or, filter directly on Python 3+ since that also creates an iterator vs creating a list as filter on Python 2 does.

You can use a list comprehension to get the expected permutations :
>>> [i for i in perms if i[0] not in {3,4}]
[(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1)]
Note that using a set container has O(1) for check the membership.and it would be more efficient if you have more filter numbers!
If you don't want the tuples that starts with 3,1 and 4,1 you can just simply do :
>>> perms = it.permutations(my_list)
>>> [i for i in perms if i[:2] !=[4,1] and i[:2] !=[4,1]]
[(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]
>>>

Related

Rearrange the tuple by sorting in python

I have two lists. One list contains X coordinate values and second list contains Y coordinate values. Using these two lists, I want to make a tupple which is sorted by their first element.
X coordinate = [2, 3, 4, 4, 3, 2, 1, 0, 0, 1, 1, 1, 1, 2, 2, 2]
Y coordinate = [3, 3, 3, 4, 4, 4, 4, 4, 3, 3, 2, 1, 0, 0, 1, 2]
I want my output like this:
[(0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 3), (4, 4)]
To achieve this result, I wrote below code and got an output mentioend below.
merged_list = list(tuple(zip(X3_coordinate, Y3_coordinate)))
merged_list.sort(key=lambda x: x[0])
merged_list
Output:
[(0, 4), (0, 3), (1, 4), (1, 3), (1, 2), (1, 1), (1, 0), (2, 3), (2, 4), (2, 0), (2, 1), (2, 2), (3, 3), (3, 4), (4, 3), (4, 4)]
Kindly let me know what I am doing wrong and give some suggestions of code.
instead of list(tuple()) just do list()
sort with no key, it'll do element-wise by default
use sorted do both generate the list and sort
X_coordinate = [2, 3, 4, 4, 3, 2, 1, 0, 0, 1, 1, 1, 1, 2, 2, 2]
Y_coordinate = [3, 3, 3, 4, 4, 4, 4, 4, 3, 3, 2, 1, 0, 0, 1, 2]
merged_list = sorted(zip(X_coordinate, Y_coordinate))
print(merged_list)
You've sorted by x, but not later by y:
merged_list.sort(key=lambda x: (x[0], x[1]))
[(0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 3), (4, 4)]
By using "key=lambda x: x[0]", you are forcing it to be sorted only by the first element, what you seek is a sort where-in you give priority to the first element, but if the values are same you wish to sort it by the subsequent elements.
X3_coordinate = [2, 3, 4, 4, 3, 2, 1, 0, 0, 1, 1, 1, 1, 2, 2, 2]
Y3_coordinate = [3, 3, 3, 4, 4, 4, 4, 4, 3, 3, 2, 1, 0, 0, 1, 2]
merged_list = list(zip(X3_coordinate, Y3_coordinate))
merged_list.sort()
print(merged_list)
Output:
[(0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 3), (4, 4)]

Summing over functions between lists

Working in Python.
Take k and n positive integers.
I have a function F with input a list L of positive integers where len(L) = k, max(L) < n+1. F returns an integer.
I want to return sum([F(L) for L a list of positive integers with len(L) = k, max(L)<n+1])
The naive approach is to have k nested for loops.
value = 0
for jj1 in range(1,n+1):
for jj2 in range(1,n+1):
...
for jjk in range(1,n+1)
value = value + F([jj1,jj2,...,jjk])
return value
This is not satisfactory because I want to take this sum in a loop over various values of k (so sometimes k =1, sometimes k=10).
I'm hoping there is some standard trick to do this?
You want itertools.product.
>>> from itertools import product
>>> n, k = 4, 3
>>> list(product(range(1, n+1), repeat=k))
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (1, 4, 1), (1, 4, 2), (1, 4, 3), (1, 4, 4), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4), (2, 4, 1), (2, 4, 2), (2, 4, 3), (2, 4, 4), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 1, 4), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 2, 4), (3, 3, 1), (3, 3, 2), (3, 3, 3), (3, 3, 4), (3, 4, 1), (3, 4, 2), (3, 4, 3), (3, 4, 4), (4, 1, 1), (4, 1, 2), (4, 1, 3), (4, 1, 4), (4, 2, 1), (4, 2, 2), (4, 2, 3), (4, 2, 4), (4, 3, 1), (4, 3, 2), (4, 3, 3), (4, 3, 4), (4, 4, 1), (4, 4, 2), (4, 4, 3), (4, 4, 4)]
Given F, n, and k, you can therefore do:
sum(F(list(L)) for L in product(range(1, n+1), repeat=k))
(or leave off the list call if it's fine for L to be a tuple instead of a list)

Dice Statistics

I wanted to build a dice Stats Simulator in Python. The idea is that you have a starting array, containing [1,...,1] for the lowest possible dice rolls, a one. I already have one example for 4d4 and this works so I want my code to do it for every combination. So I wrote Variables and Functions, for recursive use. But it gives me an output, which is not right and much too high. Maybe somebody can help me and if you need explanation, then you can ask.
sides = 4
rolls = 4
constant = sides**rolls
beginningtable = []
for i in range(rolls):
beginningtable.append(1)
values = {rolls : 0}
check = True
print("-------------")
def checking(valuetable):
print(valuetable)
sum = 0
checker = 0
for i in valuetable:
sum = sum + i
for i in values.keys():
if sum == i:
values[i] = values [i] + 1
checker = 1
if checker == 0:
values[sum] = 1
def gothroughindex(table, index):
checking(table)
for i in range(sides-1):
table[index] += 1
checking(table)
return table
def loop(looptable, loopcounter):
for i in range(loopcounter):
if i != (loopcounter-1):
looptable[i] = 1
else:
looptable[i] += 1
return looptable
def recursiveloop(table,counter, loopvalue):
if counter == rolls:
for i in range(rolls):
print("\ncounter == counter\n")
recursiveloop(table, counter-1, loopvalue-1)
table = loop(table, loopvalue)
elif counter == 1:
for i in range(rolls-1):
print("\ncounter == 1\n")
print(loopvalue)
gothroughindex(table, 0)
table = loop(table, loopvalue+1)
else:
for i in range(rolls):
print("\nelse\n")
print(str(loopvalue) + " here I am")
recursiveloop(table, counter-1, loopvalue-1)
gothroughindex(table, 0)
table = loop(table, loopvalue)
def mainthing(maintable):
counter = rolls
loopvalue = rolls
recursiveloop(maintable, counter, loopvalue)
"""
for i in range(rolls): #Das sind die ersten vier
for i in range(rolls): #Das sind die ersten drei
for i in range(rolls-1): #Das sind die ersten beiden
gothroughindex(maintable, 0)
maintable = loop(maintable, 2)
gothroughindex(maintable, 0)
maintable = loop(maintable, 3)
maintable = loop(maintable, 4)
"""
print("---------")
print(maintable)
print(values)
sum = 0
for i in values.keys():
sum = values[i] + sum
print(sum)
mainthing(beginningtable)
Couldnt you just simplyfiy all this and use itertools product method to generate these?
generate a range for the number of sides of the dice, and then use that as the iterator for product to pick from. specify the number of dice as the repeat so that product will generate that many items
you can read more on itertools here https://docs.python.org/3/library/itertools.html#itertools.product
itertools.product(*iterables, repeat=1)
Cartesian product of input
iterables.
To compute the product of an iterable with itself, specify the number
of repetitions with the optional repeat keyword argument. For example,
product(A, repeat=4) means the same as product(A, A, A, A).
from itertools import product
def roll_combos(num_dice, num_sides):
return product(range(1, num_sides + 1), repeat=num_dice)
print(list(roll_combos(2, 3)))
print(list(roll_combos(4, 4)))
OUTPUT
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
[(1, 1, 1, 1), (1, 1, 1, 2), (1, 1, 1, 3), (1, 1, 1, 4), (1, 1, 2, 1), (1, 1, 2, 2), (1, 1, 2, 3), (1, 1, 2, 4), (1, 1, 3, 1), (1, 1, 3, 2), (1, 1, 3, 3), (1, 1, 3, 4), (1, 1, 4, 1), (1, 1, 4, 2), (1, 1, 4, 3), (1, 1, 4, 4), (1, 2, 1, 1), (1, 2, 1, 2), (1, 2, 1, 3), (1, 2, 1, 4), (1, 2, 2, 1), (1, 2, 2, 2), (1, 2, 2, 3), (1, 2, 2, 4), (1, 2, 3, 1), (1, 2, 3, 2), (1, 2, 3, 3), (1, 2, 3, 4), (1, 2, 4, 1), (1, 2, 4, 2), (1, 2, 4, 3), (1, 2, 4, 4), (1, 3, 1, 1), (1, 3, 1, 2), (1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 1), (1, 3, 2, 2), (1, 3, 2, 3), (1, 3, 2, 4), (1, 3, 3, 1), (1, 3, 3, 2), (1, 3, 3, 3), (1, 3, 3, 4), (1, 3, 4, 1), (1, 3, 4, 2), (1, 3, 4, 3), (1, 3, 4, 4), (1, 4, 1, 1), (1, 4, 1, 2), (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 1), (1, 4, 2, 2), (1, 4, 2, 3), (1, 4, 2, 4), (1, 4, 3, 1), (1, 4, 3, 2), (1, 4, 3, 3), (1, 4, 3, 4), (1, 4, 4, 1), (1, 4, 4, 2), (1, 4, 4, 3), (1, 4, 4, 4), (2, 1, 1, 1), (2, 1, 1, 2), (2, 1, 1, 3), (2, 1, 1, 4), (2, 1, 2, 1), (2, 1, 2, 2), (2, 1, 2, 3), (2, 1, 2, 4), (2, 1, 3, 1), (2, 1, 3, 2), (2, 1, 3, 3), (2, 1, 3, 4), (2, 1, 4, 1), (2, 1, 4, 2), (2, 1, 4, 3), (2, 1, 4, 4), (2, 2, 1, 1), (2, 2, 1, 2), (2, 2, 1, 3), (2, 2, 1, 4), (2, 2, 2, 1), (2, 2, 2, 2), (2, 2, 2, 3), (2, 2, 2, 4), (2, 2, 3, 1), (2, 2, 3, 2), (2, 2, 3, 3), (2, 2, 3, 4), (2, 2, 4, 1), (2, 2, 4, 2), (2, 2, 4, 3), (2, 2, 4, 4), (2, 3, 1, 1), (2, 3, 1, 2), (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 1), (2, 3, 2, 2), (2, 3, 2, 3), (2, 3, 2, 4), (2, 3, 3, 1), (2, 3, 3, 2), (2, 3, 3, 3), (2, 3, 3, 4), (2, 3, 4, 1), (2, 3, 4, 2), (2, 3, 4, 3), (2, 3, 4, 4), (2, 4, 1, 1), (2, 4, 1, 2), (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 1), (2, 4, 2, 2), (2, 4, 2, 3), (2, 4, 2, 4), (2, 4, 3, 1), (2, 4, 3, 2), (2, 4, 3, 3), (2, 4, 3, 4), (2, 4, 4, 1), (2, 4, 4, 2), (2, 4, 4, 3), (2, 4, 4, 4), (3, 1, 1, 1), (3, 1, 1, 2), (3, 1, 1, 3), (3, 1, 1, 4), (3, 1, 2, 1), (3, 1, 2, 2), (3, 1, 2, 3), (3, 1, 2, 4), (3, 1, 3, 1), (3, 1, 3, 2), (3, 1, 3, 3), (3, 1, 3, 4), (3, 1, 4, 1), (3, 1, 4, 2), (3, 1, 4, 3), (3, 1, 4, 4), (3, 2, 1, 1), (3, 2, 1, 2), (3, 2, 1, 3), (3, 2, 1, 4), (3, 2, 2, 1), (3, 2, 2, 2), (3, 2, 2, 3), (3, 2, 2, 4), (3, 2, 3, 1), (3, 2, 3, 2), (3, 2, 3, 3), (3, 2, 3, 4), (3, 2, 4, 1), (3, 2, 4, 2), (3, 2, 4, 3), (3, 2, 4, 4), (3, 3, 1, 1), (3, 3, 1, 2), (3, 3, 1, 3), (3, 3, 1, 4), (3, 3, 2, 1), (3, 3, 2, 2), (3, 3, 2, 3), (3, 3, 2, 4), (3, 3, 3, 1), (3, 3, 3, 2), (3, 3, 3, 3), (3, 3, 3, 4), (3, 3, 4, 1), (3, 3, 4, 2), (3, 3, 4, 3), (3, 3, 4, 4), (3, 4, 1, 1), (3, 4, 1, 2), (3, 4, 1, 3), (3, 4, 1, 4), (3, 4, 2, 1), (3, 4, 2, 2), (3, 4, 2, 3), (3, 4, 2, 4), (3, 4, 3, 1), (3, 4, 3, 2), (3, 4, 3, 3), (3, 4, 3, 4), (3, 4, 4, 1), (3, 4, 4, 2), (3, 4, 4, 3), (3, 4, 4, 4), (4, 1, 1, 1), (4, 1, 1, 2), (4, 1, 1, 3), (4, 1, 1, 4), (4, 1, 2, 1), (4, 1, 2, 2), (4, 1, 2, 3), (4, 1, 2, 4), (4, 1, 3, 1), (4, 1, 3, 2), (4, 1, 3, 3), (4, 1, 3, 4), (4, 1, 4, 1), (4, 1, 4, 2), (4, 1, 4, 3), (4, 1, 4, 4), (4, 2, 1, 1), (4, 2, 1, 2), (4, 2, 1, 3), (4, 2, 1, 4), (4, 2, 2, 1), (4, 2, 2, 2), (4, 2, 2, 3), (4, 2, 2, 4), (4, 2, 3, 1), (4, 2, 3, 2), (4, 2, 3, 3), (4, 2, 3, 4), (4, 2, 4, 1), (4, 2, 4, 2), (4, 2, 4, 3), (4, 2, 4, 4), (4, 3, 1, 1), (4, 3, 1, 2), (4, 3, 1, 3), (4, 3, 1, 4), (4, 3, 2, 1), (4, 3, 2, 2), (4, 3, 2, 3), (4, 3, 2, 4), (4, 3, 3, 1), (4, 3, 3, 2), (4, 3, 3, 3), (4, 3, 3, 4), (4, 3, 4, 1), (4, 3, 4, 2), (4, 3, 4, 3), (4, 3, 4, 4), (4, 4, 1, 1), (4, 4, 1, 2), (4, 4, 1, 3), (4, 4, 1, 4), (4, 4, 2, 1), (4, 4, 2, 2), (4, 4, 2, 3), (4, 4, 2, 4), (4, 4, 3, 1), (4, 4, 3, 2), (4, 4, 3, 3), (4, 4, 3, 4), (4, 4, 4, 1), (4, 4, 4, 2), (4, 4, 4, 3), (4, 4, 4, 4)]

Finding all possible permutations of an unfixed length of numbers to reach a given sum or product

Using plain Python or any Python libraries, how would you go about finding all possible combinations of elements in a list l that equal a given value val using addition, subtraction, or multiplication? Assume the length of the list isn't always the same, assume each element in the list can only be used once in each combination, and assume there isn't any use of parentheses.
For example:
We're given a list of numbers: l = [1,2,3,4]
We're given a value equaling the combination of values: val = 6
The output would include the following:
[2,4], since 2+4=6
And [4,2], since 4+2=6
And [1,3,2], since 1+3+2=6
And [1,2,4], since 1*2+4=6
etc.
I've tried using itertools.permutations:
>>> from itertools import permutations
>>> l = [1,2,3,4]
>>> val = 6
>>> correct_combos = []
>>> for i in range(1, len(l)+1):
... for p in permutations(l, r=i):
... if sum(p) == val:
... correct_combos.append(p)
I'm able to only implement the code for testing the sum of all combinations of elements in the list.
>>> print(correct_combos)
[(2, 4), (4, 2)]
I'm stuck on finding permutations of elements in the list using a combination of addition, subtraction, and multiplication.
I don't know if this algorithm is efficient, but it works fine:
from itertools import permutations, product
l = [1,2,3,4]
val = 6
operator = ['+', '-', '*']
correct_combos=[]
for r in range(1, len(l)+1):
for item in permutations(l,r):
for unit in product(operator, repeat=r-1):
res=""
for idx in range(0,r-1):
res+=str(item[idx])+unit[idx]
res+=str(item[-1])
if(val==eval(res)):
if item not in correct_combos:
correct_combos.append(item)
print(correct_combos)
Output
[(2, 3), (2, 4), (3, 2), (4, 2), (1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 4, 2), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 4, 1), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 4, 1), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 3, 1), (1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]
Non recursive solution:
from itertools import permutations, product, accumulate
from collections import deque
from operator import add, sub, mul
l = [1, 2, 3, 4]
val = 6
correct_combos = []
def is_correct(p, val, ops=[add, sub, mul]):
if len(p) == 1:
return p[0] == val
for op in product(ops, repeat=len(p) - 1):
iop = iter(op)
l = deque(accumulate(p, lambda a, b: next(iop)(a, b)), maxlen=1)[0]
if l == val:
return True
return False
for i in range(1, len(l) + 1):
for p in permutations(l, r=i):
if is_correct(p, val):
correct_combos.append(p)
print(correct_combos)
Prints:
[(2, 3), (2, 4), (3, 2), (4, 2), (1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 4, 2), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 4, 1), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 4, 1), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]
You can use a recursive generator function:
from operator import add, sub, mul
l = [1,2,3,4]
val = 6
def combos(c = [], r_exp = None):
if r_exp == val:
yield c
else:
for i in filter(lambda x:x not in c, l):
if not c:
yield from combos(c=[i], r_exp = i)
else:
for s, f in [['+', add], ['-', sub], ['*', mul]]:
yield from combos(c=c+[s, i], r_exp = f(r_exp, i))
print([''.join(map(str, i)) for i in combos()])
Output:
['1+2+3', '1-2+3+4', '1-2+4+3', '1*2*3', '1*2+4', '1+3+2', '1+3-2+4', '1+3+4-2', '1*3*2', '1+4-2+3', '1+4+3-2', '1*4+2', '1*4-2*3', '2+1+3', '2*1*3', '2*1+4', '2+3+1', '2*3', '2+4', '2*4+1-3', '2*4-3+1', '3+1+2', '3+1-2+4', '3+1+4-2', '3-1+4', '3-1*4-2', '3*1*2', '3+2+1', '3-2+1+4', '3-2+4+1', '3*2', '3+4+1-2', '3+4-1', '3+4-2+1', '4+1-2+3', '4+1+3-2', '4-1*2', '4-1+3', '4*1+2', '4*1-2*3', '4+2', '4-2+1+3', '4-2*1*3', '4-2+3+1', '4-2*3', '4*2+1-3', '4*2-3+1', '4+3+1-2', '4+3-1', '4+3-2+1']
To just get the tuple results, only a small change needs to be made:
for f in [add, sub, mull]:
yield from combos(c=c+[i], r_exp = f(r_exp, i))
...
print(list(map(tuple, combos())))
Output:
[(1, 2, 3), (1, 2, 3, 4), (1, 2, 4, 3), (1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 2, 4), (1, 3, 4, 2), (1, 3, 2), (1, 4, 2, 3), (1, 4, 3, 2), (1, 4, 2), (1, 4, 2, 3), (2, 1, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3), (2, 4), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2), (3, 1, 2, 4), (3, 1, 4, 2), (3, 1, 4), (3, 1, 4, 2), (3, 1, 2), (3, 2, 1), (3, 2, 1, 4), (3, 2, 4, 1), (3, 2), (3, 4, 1, 2), (3, 4, 1), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 1, 2), (4, 1, 3), (4, 1, 2), (4, 1, 2, 3), (4, 2), (4, 2, 1, 3), (4, 2, 1, 3), (4, 2, 3, 1), (4, 2, 3), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 1), (4, 3, 2, 1)]
from itertools import chain, combinations
def subSet(l):
return chain.from_iterable(combinations(l, r) for r in range(len(l) + 1))
def whatWeWant(l, val):
x = 0
for i in l:
x += i
if x == val:
return True
return False
l = [1, 2, 3, 4]
val = 6
allSubSets = list(subSet(l))
ans = []
for subSet in allSubSets:
if whatWeWant(subSet, val):
ans.append(subSet)
With this code you can find what you want but you have to specifically say the possible combinations(in whatWeWant method).

How to randomly pick k items from a list of n size in python

I want to know how to randomly pick k items from a list of n size for ALL possible combinations.
For example, let A = [1, 2, 3, 4] and K = 3. Then it should return [1, 2, 3], [1, 2, 4], [1, 3, 4], and [2, 3, 4].
You are looking for itertools.combinations. For example:
>>> my_list = A = [1, 2, 3, 4]
>>> k = 3
>>> from itertools import combinations
>>> list(combinations(my_list, k))
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
If the ordering inside the results matters you might use itertools.permutations to get all permutations as well - putting them into a set removes duplicates stemming from duplicate numbers in your list
import itertools
n = 3
my_list = [1, 2, 3, 2 ,1]
print( set(itertools.permutations(my_list, n))) # or list, see below
Output:
{(1, 2, 1), (3, 1, 2), (2, 3, 2), (1, 3, 2), (3, 1, 1), (3, 2, 1), (1, 2, 2),
(1, 1, 3), (2, 2, 1), (2, 1, 1), (1, 3, 1), (2, 3, 1), (1, 2, 3), (1, 1, 2), ...}
# with set: 17 items
[(1, 2, 3), (1, 2, 2), (1, 2, 1), (1, 3, 2), (1, 3, 2), (1, 3, 1), (1, 2, 2),
(1, 2, 3), (1, 2, 1), (1, 1, 2), (1, 1, 3), (1, 1, 2), (2, 1, 3), (2, 1, 2), ...]
# with list: 60 items
Difference between permutations and combinations:
# permutations - based on all values on any position in the resulttuple
[(1, 2, 3), (1, 2, 2), (1, 2, 1), (1, 3, 2), (1, 3, 2), (1, 3, 1), (1, 2, 2), (1
, 2, 3), (1, 2, 1), (1, 1, 2), (1, 1, 3), (1, 1, 2), (2, 1, 3), (2, 1, 2), (2, 1
, 1), (2, 3, 1), (2, 3, 2), (2, 3, 1), (2, 2, 1), (2, 2, 3), (2, 2, 1), (2, 1, 1
), (2, 1, 3), (2, 1, 2), (3, 1, 2), (3, 1, 2), (3, 1, 1), (3, 2, 1), (3, 2, 2),
(3, 2, 1), (3, 2, 1), (3, 2, 2), (3, 2, 1), (3, 1, 1), (3, 1, 2), (3, 1, 2), (2,
1, 2), (2, 1, 3), (2, 1, 1), (2, 2, 1), (2, 2, 3), (2, 2, 1), (2, 3, 1), (2, 3,
2), (2, 3, 1), (2, 1, 1), (2, 1, 2), (2, 1, 3), (1, 1, 2), (1, 1, 3), (1, 1, 2)
, (1, 2, 1), (1, 2, 3), (1, 2, 2), (1, 3, 1), (1, 3, 2), (1, 3, 2), (1, 2, 1), (
1, 2, 2), (1, 2, 3)]
# combinations - based on position in source list not on value order in tuple preserved
[(1, 2, 3), (1, 2, 2), (1, 2, 1), (1, 3, 2), (1, 3, 1), (1, 2, 1), (2, 3, 2), (2
, 3, 1), (2, 2, 1), (3, 2, 1)]
Press any key to continue . . .

Categories