Учитывая набор букв, как создать все возможные комбинации слова с этими буквами, дублирующимися с указанным числом? - PullRequest
4 голосов
/ 11 апреля 2019

Допустим, у меня есть слово «яблоко», набор букв ['a', 'l', 'e'] и номер повторения 3. Из этого я хотел бы создать следующий набор: ['aaapple', 'aaappllle', 'aaappllleee', 'appllle', 'appllleee', 'appleee'].

Вот что я уже пробовал:

l = ['a', 'l', 'e']
word = "apple"

for i in range(0, len(l)):
    print wordWithDuplicatedLetters = "".join(3*c if c == l[i] else c for c in word)

Ноэто не соответствует всем комбинациям, и itertools, кажется, не предоставляет возможности, которую я ищу.

Ответы [ 4 ]

3 голосов
/ 11 апреля 2019

Я не думаю, что в вашем примере выходных данных есть все возможные комбинации, у моих ниже, я думаю, есть все.Хитрость заключается в том, чтобы просмотреть все комбинации любого размера, которые выполняет функция all_combinations ниже.

import itertools

repeat = ['a', 'l', 'e']
word = 'apple'

def all_combinations(itr):
    lst = list(itr)
    for r in range(1, len(lst) + 1):  # set start to 0 if want to include []
        for perm in itertools.combinations(lst, r=r):
            yield perm   # or use yield from in py3

def all_repeats():
    for rep in all_combinations(repeat):
        yield ''.join(char * 3 if char in rep else char for char in word)

print(list(all_repeats()))

вывод

['aaapple',
 'appllle',
 'appleee',
 'aaappllle',
 'aaappleee',
 'appllleee',
 'aaappllleee']
2 голосов
/ 11 апреля 2019

Вы можете разделить эту проблему на два этапа.Сначала выясним все возможные подмножества позиций, которые следует повторить.Это, по сути, набор питания , взятый отсюда с удаленным пустым корпусом.Построение его из индексов позволяет решению быть устойчивым к словам, которые содержат повторяющиеся буквы для повторения.

Во-вторых, для каждого случая в блоке питания создайте правильную строку и отобразите ее.

from itertools import chain, combinations

def powerset_non_empty(iterable):
    """
    powerset with empty set skipped
    powerset([1,2,3]) -->  (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
    """
    xs = list(iterable)
    # note we return a list, but could choose to return an iterator too
    return list(chain.from_iterable(combinations(xs,n) for n in range(1, len(xs)+1)))



l = ['a', 'l', 'e']
word = "apple"

indices = [i for i,c in enumerate(word) if c in l]
number_of_repetition = 3
powerset = powerset_non_empty(indices)

result = []
for index_tuple in powerset:
    s = ''
    for i, c in enumerate(word):
        if i in index_tuple:
            s += (number_of_repetition * c)
        else:
            s += c
    print(s)
    result.append(s)
#Output:
['aaapple',
 'appllle',
 'appleee',
 'aaappllle',
 'aaappleee',
 'appllleee',
 'aaappllleee']
2 голосов
/ 11 апреля 2019

Попробуйте использовать этот цикл:

s = ''
for i in word:
    if i in l:
        s += (3 * i)
    else:
        s += i

Что может быть пониманием списка:

s = ''.join([3 * i if i in l else i for i in word])

И теперь в обоих случаях:

print(s)

Is:

aaappllleee

Чтобы полностью ответить на ваш вопрос

Вы должны использовать:

import itertools

l = ['a', 'l', 'e']
word = 'apple'
l2 = []
for i in range(len(l)):
   for x in itertools.combinations(l, r=i+1):
       l2.append(x)
l3 = []
for lst in l2:
    l3.append(''.join(char * 3 if char in lst else char for char in word))

print(l3)

Выход:

['aaapple', 'appllle', 'appleee', 'aaappllle', 'aaappleee', 'appllleee', 'aaappllleee']
1 голос
/ 11 апреля 2019

Вы можете использовать простую функцию рекурсивного генератора:

l = ['a', 'l', 'e']
word = "apple"
def combo(d, r, c):
  for i in l:
    if any(j[0] == i and len(j) < r for j in c):
      w = [j if j[0] != i or len(j) == r else j+([i]*(r-1)) for j in c]
      yield ''.join(map(''.join, w))
      if any(j[0] in l and len(j) < r for j in w):
        yield from combo(d, r, w)


print(list(combo(l, 3, [[i] for i in word])))

Выход:

['aaapple', 'aaappllle', 'aaappllleee', 'aaappleee', 'aaappllleee', 'appllle', 'aaappllle', 'aaappllleee', 'appllleee', 'aaappllleee', 'appleee', 'aaappleee', 'aaappllleee', 'appllleee', 'aaappllleee']
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...