0

I have a list containing ['a', 'bill', 'smith'] and I would like to write a python code in order to obtain all possible combinations applying a certain criteria. To be more precise, I would like to obtain combination of those three element in the list plus the first letter of each element if that element isn't yet present in the output list. For example, given the list ['a', 'bill', 'smith'], one part of the expected output would be: ['a', 'bill', 'smith'], ['bill', 'smith'], ['a', 'smith'] but as well, ['a', 'b, 'smith'], ['bill, 's'], ['a', 's']. What I'm not expected to obtain is output like this ['s', 'bill, 'smith'] as the first element (s) is already taken into account by the third element ('smith'). Can someone help me?

This is what I've done so far:

mapping = dict(enumerate(['a', 'bill', 'smith']))
for i in mapping.items():
if len(i[1])>1:
    mapping[i[0]] = [i[1], i[1][0]]
else:
    mapping[i[0]] = [i[1]]

print(mapping)
{0: ['a'], 1: ['bill', 'b'], 2: ['william', 'w'], 3: ['stein', 's']}

I'm now stucked. I would like to use itertools library to iterate over the dict values to create all possible combinations.

Thanks in advance :)

2
  • Would it be infeasible to simply generate all combinations with the standard itertools tools and then filter out the elements that are considered redundant by your criteria? Commented Jun 23, 2020 at 13:10
  • then... how to say a criteria is redundant accoridng to python? Thanks anyway :) but @schwobaseggl solved it Commented Jun 24, 2020 at 7:54

2 Answers 2

1

You can use some itertools:

from itertools import product, permutations

lst = [list({s, s[:1]}) for s in ['a', 'bill', 'smith']]
# [['a'], ['bill', 'b'], ['s', 'smith']]

for perms in map(permutations, product(*lst)):
    for p in perms:
        print(p)

('a', 'bill', 's')
('a', 's', 'bill')
('bill', 'a', 's')
('bill', 's', 'a')
('s', 'a', 'bill')
('s', 'bill', 'a')
('a', 'bill', 'smith')
('a', 'smith', 'bill')
('bill', 'a', 'smith')
('bill', 'smith', 'a')
('smith', 'a', 'bill')
('smith', 'bill', 'a')
('a', 'b', 's')
('a', 's', 'b')
('b', 'a', 's')
('b', 's', 'a')
('s', 'a', 'b')
('s', 'b', 'a')
('a', 'b', 'smith')
('a', 'smith', 'b')
('b', 'a', 'smith')
('b', 'smith', 'a')
('smith', 'a', 'b')
('smith', 'b', 'a')

The first step creates the list of equivalent lists:

[['a'], ['bill', 'b'], ['s', 'smith']]

then, product produces the cartesian product of the lists in said lists:

('a', 'bill', 's')
('a', 'bill', 'smith')
('a', 'b', 's')
...

and for each of those, permutations gives you, well, all permutations:

('a', 'bill', 's')
('a', 's', 'bill')
('bill', 'a', 's')
...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! You saved me 😅
0

You could do something like this with combinations from itertools:

Here I am assuming you want the first letter of each word in the list only if it has a length greater than 1. If not you can change the if condition.

from itertools import combinations

lst = ['a', 'bill', 'smith']
lst_n=[]
for words in lst:
    lst_n.append(words)
    if len(words)>1:
        lst_n.append(words[0])

for t in range(1,len(lst_n)+1):
    for comb in combinations(lst_n,r=t):
        print(list(comb))

OUTPUT:

['a']
['bill']
['b']
['smith']
['s']
['a', 'bill']
['a', 'b']
['a', 'smith']
['a', 's']
['bill', 'b']
['bill', 'smith']
['bill', 's']
['b', 'smith']
['b', 's']
['smith', 's']
['a', 'bill', 'b']
['a', 'bill', 'smith']
['a', 'bill', 's']
['a', 'b', 'smith']
['a', 'b', 's']
['a', 'smith', 's']
['bill', 'b', 'smith']
['bill', 'b', 's']
['bill', 'smith', 's']
['b', 'smith', 's']
['a', 'bill', 'b', 'smith']
['a', 'bill', 'b', 's']
['a', 'bill', 'smith', 's']
['a', 'b', 'smith', 's']
['bill', 'b', 'smith', 's']
['a', 'bill', 'b', 'smith', 's']

Here if you want combinations to be of length 3 only remove the for loop with range and set r=3.

1 Comment

Thanks for the time spent helping me but I found other answer more useful :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.