单词合并

发布于 2025-01-24 09:36:14 字数 687 浏览 0 评论 0原文

我是新的用户学习Python。我有查询是否可以完成。如果用户输入一个单词假设“ dance”和字符是“ $”和“@”,则他将获得所有单词和字符组合的可能性['d $@a $@a $@n@$ c@$ e',, d@$ a@$ n@$ c@$ e,'da $ n $ c $ e','d $@an $ c@$ e','da@nc $ e']等。组合。有可能实现吗?

import itertools as it

string = 'ABCD'  # input('enter string: ')
fill_char = '$'  # input('enter fill_char: ')

fillers = it.product(['', fill_char], repeat=len(string))

for filler in fillers:
    tmp = it.zip_longest(string, filler)
    print(''.join([''.join(char_and_fill_char) for char_and_fill_char in tmp]))


Output:
ABCD
ABCD$
ABC$D
ABC$D$
AB$CD
AB$CD$
AB$C$D
AB$C$D$
A$BCD
A$BCD$
A$BC$D
A$BC$D$
A$B$CD
A$B$CD$
A$B$C$D
A$B$C$D$

我想要3个字符的组合。如果有人能提供帮助,我会非常奇怪。

I am new user learning python. I have query if it can be done or not. If user input a word suppose "Dance" and characters are "$" and "@", he would get a all possibilities of word and character combination for example ['D$@a$@n@$c@$e',D@$a@$n@$c@$e, 'Da$n$c$e', 'D$@an$c@$e', 'Da@nc$e'], etc. It should give a combinations. Is it possible to achieve?

import itertools as it

string = 'ABCD'  # input('enter string: ')
fill_char = '

I wanted combination of 3 characters. If anybody could help I would be very greatful.

# input('enter fill_char: ') fillers = it.product(['', fill_char], repeat=len(string)) for filler in fillers: tmp = it.zip_longest(string, filler) print(''.join([''.join(char_and_fill_char) for char_and_fill_char in tmp])) Output: ABCD ABCD$ ABC$D ABC$D$ AB$CD AB$CD$ AB$C$D AB$C$D$ A$BCD A$BCD$ A$BC$D A$BC$D$ A$B$CD A$B$CD$ A$B$C$D A$B$C$D$

I wanted combination of 3 characters. If anybody could help I would be very greatful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2025-01-31 09:36:14

这是一个可能的解决方案:
对于更清晰的演示,我只选择了两个字母。基本上,将您的字符串的每个字母拿走,然后将fill_chars的所有可能组合加入其中。然后构建所有列表的产品(每个列表都包含一个字母的所有组合)。
添加了几个评论和打印语句以进行解释。

from itertools import permutations, product
string = 'AB'  # input('enter string: ')
fill_char = '$@'  # input('enter fill_char: ')

s_lst = list(string)
fill_lst = list(fill_char)
powerSet = []

for char in s_lst:
    perm_each_char=[]
    
    for k in range(len(fill_char)+1):
        tmp = [''.join(p) for p in permutations(fill_lst, k)]
        # tmp : permutation result in list, each joined together to string (e.g k=2: ['@
, '$@'])
        tmp2 = [''.join((char,elem)) for elem in tmp]
        # tmp2 : list of current char joined with each item in list of tmp (e.g. k=2: ['A@
, 'A$@'])
        perm_each_char.extend(tmp2)
        print(f"{char=} - {k=} - {perm_each_char=}")
        
    powerSet.append(perm_each_char) # list of lists, each list contains all permutations of one char of the string
    print(f"combined: {powerSet=} \n")
    
all_combinations = list(''.join(x) for x in product(*powerSet))
# now you build the itertools.product with all lists to get every combination
print(f"{all_combinations=}")

Output:

char='A' - k=0 - perm_each_char=['A']
char='A' - k=1 - perm_each_char=['A', 'A
, 'A@']
char='A' - k=2 - perm_each_char=['A', 'A
, 'A@', 'A$@', 'A@
]
combined: powerSet=[['A', 'A
, 'A@', 'A$@', 'A@
]] 

char='B' - k=0 - perm_each_char=['B']
char='B' - k=1 - perm_each_char=['B', 'B
, 'B@']
char='B' - k=2 - perm_each_char=['B', 'B
, 'B@', 'B$@', 'B@
]
combined: powerSet=[['A', 'A
, 'A@', 'A$@', 'A@
], ['B', 'B
, 'B@', 'B$@', 'B@
]] 

all_combinations=['AB', 'AB
, 'AB@', 'AB$@', 'AB@
, 'A$B', 'A$B
, 'A$B@', 'A$B$@', 'A$B@
, 'A@B', 'A@B
, 'A@B@', 'A@B$@', 'A@B@
, 'A$@B', 'A$@B
, 'A$@B@', 'A$@B$@', 'A$@B@
, 'A@$B', 'A@$B
, 'A@$B@', 'A@$B$@', 'A@$B@
]

Here is one possible solution:
For more clear presentation I only chose a string of two letters. Basically take every single letter of your string and join all possible combinations of fill_chars to it. Then build the product of all lists (each containing all combinations for one letter).
Added several comments and print statements for explanation.

from itertools import permutations, product
string = 'AB'  # input('enter string: ')
fill_char = '$@'  # input('enter fill_char: ')

s_lst = list(string)
fill_lst = list(fill_char)
powerSet = []

for char in s_lst:
    perm_each_char=[]
    
    for k in range(len(fill_char)+1):
        tmp = [''.join(p) for p in permutations(fill_lst, k)]
        # tmp : permutation result in list, each joined together to string (e.g k=2: ['@
, '$@'])
        tmp2 = [''.join((char,elem)) for elem in tmp]
        # tmp2 : list of current char joined with each item in list of tmp (e.g. k=2: ['A@
, 'A$@'])
        perm_each_char.extend(tmp2)
        print(f"{char=} - {k=} - {perm_each_char=}")
        
    powerSet.append(perm_each_char) # list of lists, each list contains all permutations of one char of the string
    print(f"combined: {powerSet=} \n")
    
all_combinations = list(''.join(x) for x in product(*powerSet))
# now you build the itertools.product with all lists to get every combination
print(f"{all_combinations=}")

Output:

char='A' - k=0 - perm_each_char=['A']
char='A' - k=1 - perm_each_char=['A', 'A
, 'A@']
char='A' - k=2 - perm_each_char=['A', 'A
, 'A@', 'A$@', 'A@
]
combined: powerSet=[['A', 'A
, 'A@', 'A$@', 'A@
]] 

char='B' - k=0 - perm_each_char=['B']
char='B' - k=1 - perm_each_char=['B', 'B
, 'B@']
char='B' - k=2 - perm_each_char=['B', 'B
, 'B@', 'B$@', 'B@
]
combined: powerSet=[['A', 'A
, 'A@', 'A$@', 'A@
], ['B', 'B
, 'B@', 'B$@', 'B@
]] 

all_combinations=['AB', 'AB
, 'AB@', 'AB$@', 'AB@
, 'A$B', 'A$B
, 'A$B@', 'A$B$@', 'A$B@
, 'A@B', 'A@B
, 'A@B@', 'A@B$@', 'A@B@
, 'A$@B', 'A$@B
, 'A$@B@', 'A$@B$@', 'A$@B@
, 'A@$B', 'A@$B
, 'A@$B@', 'A@$B$@', 'A@$B@
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文