Hi how can i get a mapped list to print all possible combinations
say the dict mapping is = {1:[a,b],2:[c,d]......
so with the list [1,2] and the sample mapping above I would like to print out all possible combinations of the pairs a,d against c,d into a list
Have a look at the combinatoric functions in the itertools module.
If you're looking for all the pairings of ab against cd, the product function should help:
>>> d = {1: ['a','b'], 2: ['c', 'd']}
>>> for t in product(*d.values()):
print t
('a', 'c')
('a', 'd')
('b', 'c')
('b', 'd')
If you're looking all combinations of abcd taken r at a time for the various sizes of r, then tthe combinations function should get the job done:
>>> for r in range(5):
for t in combinations('abcd', r):
print t
()
('a',)
('b',)
('c',)
('d',)
('a', 'b')
('a', 'c')
('a', 'd')
('b', 'c')
('b', 'd')
('c', 'd')
('a', 'b', 'c')
('a', 'b', 'd')
('a', 'c', 'd')
('b', 'c', 'd')
('a', 'b', 'c', 'd')
from itertools import product
mapping = {1:['a','b'], 2:['c','d']}
data = [1, 2]
for combo in product(*(mapping[d] for d in data)):
print combo
results in
('a', 'c')
('a', 'd')
('b', 'c')
('b', 'd')
Edit it sounds like what you actually want is
strings = [''.join(combo) for combo in product(*(mapping[d] for d in data))]
which gives strings == ['ac', 'ad', 'bc', 'bd'].
Related
My goal is to create 3 lists.
The 1st one is the input: choose 3 from ABCD to create AAA, ABC...etc
The 2nd one is the output: change the middle letter of each input and create a new list. eg: for AAA -> ABA,ACA,ADA. So 3 times the length of the input.
The third one is the Change: I want to name each change as c_i, for example, AAA->ABA is C1.
For Input,
>>> lis = ["A","B","C","D"]
>>> import itertools as it
>>> inp = list(it.product(lis, repeat = 3))
>>> print(inp)
[('A', 'A', 'A'), ('A', 'A', 'B'), ... ('D', 'D', 'C'), ('D', 'D', 'D')]
>>> len(inp)
64
But I am stuck on how to create the output list. Any idea is appreciated!
Thanks
You can use list comprehension:
import itertools
lst = ['A', 'B', 'C', 'D']
lst_input = list(itertools.product(lst, repeat=3))
lst_output = [(tup[0], x, tup[2]) for tup in lst_input for x in lst if tup[1] is not x]
lst_change = [f'C{i}' for i in range(1, len(lst_output) + 1)]
print(len(lst_input), len(lst_output), len(lst_change))
print(lst_input[:5])
print(lst_output[:5])
print(lst_change[:5])
# 64 192 192
# [('A', 'A', 'A'), ('A', 'A', 'B'), ('A', 'A', 'C'), ('A', 'A', 'D'), ('A', 'B', 'A')]
# [('A', 'B', 'A'), ('A', 'C', 'A'), ('A', 'D', 'A'), ('A', 'B', 'B'), ('A', 'C', 'B')]
# ['C1', 'C2', 'C3', 'C4', 'C5']
For each tuple in lst_input, the middle item is replaced by all the candidate characters, but the replacement is thrown out if that replacement character is the same as the original character (if tup[1] is not x).
For example, I have a list like ('A', 'B', 'C', 'D', ('A', 'B'), ('A', 'C'), ('C', 'D'))
In this list, I want to take elements that form () with 'A' elements.
This means, I want to take elements ('A', 'B') and ('A', 'C')
Not 'A', 'B' or ('C', 'D')
How can I get this?
l = ('A', 'B', 'C', 'D', ('A', 'B'), ('A', 'C'), ('C', 'D'))
[x for x in l if type(x) == tuple and 'A' in x]
I would use a conditional list comprehension. I check if 'A' is in the list and if it is and the content isn't only A (hence the len check), then add it to the list.
temp = ('A', 'B', 'C', 'D', ('A', 'B'), ('A', 'C'), ('C', 'D'))
results = [i for i in temp if 'A' in i and len(i) > 1]
print(results)
You can loop through like this:
new_lst = []
lst = ('A', 'B', 'C', 'D', ('A', 'B'), ('A', 'C'), ('C', 'D'))
for i in lst:
if len(i) > 1 and i[0] == "A":
new_lst.append(i)
print new_lst
I have a list like this:
list_input = [(a,b), (a,c), (a,d), (z,b), (z,e)]
I want to extract b, c and d when start it with "a" not with "z" and put in a list
I could not figure out how to do it, any advice?
Filter your list items on the first value, collecting the second:
[second for first, second in list_input if first == 'a']
Demo:
>>> list_input = [('a', 'b'), ('a', 'c'), ('a', 'd'), ('z', 'b'), ('z', 'e')]
>>> [second for first, second in list_input if first == 'a']
['b', 'c', 'd']
You could also do it explicitly:
In [8]: [list_input[i][1] for i in xrange(len(list_input)) if list_input[i][0] =='a']
Out[8]: ['b', 'c', 'd']
Or;
list_input = [("a","b"), ("a","c"), ("a","d"), ("z","b"), ("z","e")]
print ([x[1] for x in list_input if x[0]=="a"])
>>>
['b', 'c', 'd']
>>>
Manipulate it with indices. You can display that specific pairs too;
print ([(x,x[1]) for x in list_input if x[0]=="a"])
output;
>>>
[(('a', 'b'), 'b'), (('a', 'c'), 'c'), (('a', 'd'), 'd')]
>>>
What is the fastest way of counting the number of permutations? I have the following problem:
First I have this:
ncombos = itertools.combinations_with_replacement(['a1', 'a2', 'a3'], years*n)
('a1', 'a1', 'a1')
('a1', 'a1', 'a2')
('a1', 'a1', 'a3')
('a1', 'a2', 'a2')
.... etc.....
('a3', 'a3', 'a3')
The aim is to go through each one and calculate the number of permutations that each one has and construct an array with these values. I implemented this using:
nodes = np.ones(len(leafs)); i=0 #This will store the number of permutations
for j in ncombos:
nodes[i] =len(list(set(itertools.permutations(np.asanyarray(j), n))))
i = i+1
np.asanyarray(j) converts the ('a1','a1','a1') into formal ['a1','a1', 'a1'] which is need for permutations() to work. set erases the permutations which are identical. list makes a list of this. len calculates how many permutations can i make with a1, a1, a1.
So basically all I want is to count the number of permutations... However my code is extremely!!! slow ! Thank you!
Use math. The number of permutations of a list is the factorial of the length of the list, divided by the product of the factorials of the multiplicity of each element (since sets of repeated elements are permuted with no effect).
import operator
from collections import Counter
from functools import reduce
from math import factorial
def npermutations(l):
num = factorial(len(l))
mults = Counter(l).values()
den = reduce(operator.mul, (factorial(v) for v in mults), 1)
return num / den
Examples:
>>> npermutations([1,1,1])
1
>>> npermutations([1,2,3])
6
>>> npermutations([1,3,1,2,1,3,1,2])
420
If you want permutations with replacement, this exists and is called the cartesian product. Itertools has a function for this, product():
>>> for i in itertools.product('ABC', repeat=3):
... print i
...
('A', 'A', 'A')
('A', 'A', 'B')
('A', 'A', 'C')
('A', 'B', 'A')
('A', 'B', 'B')
('A', 'B', 'C')
('A', 'C', 'A')
('A', 'C', 'B')
('A', 'C', 'C')
('B', 'A', 'A')
('B', 'A', 'B')
('B', 'A', 'C')
('B', 'B', 'A')
('B', 'B', 'B')
('B', 'B', 'C')
('B', 'C', 'A')
('B', 'C', 'B')
('B', 'C', 'C')
('C', 'A', 'A')
('C', 'A', 'B')
('C', 'A', 'C')
('C', 'B', 'A')
('C', 'B', 'B')
('C', 'B', 'C')
('C', 'C', 'A')
('C', 'C', 'B')
('C', 'C', 'C')
I have 2 variables - a and b. I need to fill up k places using these variables. So if k = 3 output should be
[a,a,a], [a,a,b] , [a,b,a], [b,a,a], [a,b,b], [b,a,b], [b,b,a] and [b,b,b]
Input - k
Output - All the combinations
How do I code this in Python? Can itertools be of any help here?
>>> import itertools
>>> list(itertools.product('ab', repeat=3))
[('a', 'a', 'a'), ('a', 'a', 'b'), ('a', 'b', 'a'), ('a', 'b', 'b'), ('b', 'a', 'a'), ('b', 'a', 'b'), ('b', 'b', 'a'), ('b', 'b', 'b')]
def genPerm(varslist, pos,resultLen, result, resultsList)
if pos>resultLen:
return;
for e in varslist:
if pos==resultLen:
resultsList.append(result + [e]);
else
genPerm(varsList, pos+1, resultLen, result + [e], resultsList);
Call with:
genPerm([a,b], 0, resLength, [], resultsList);