简体   繁体   English

从给定的单词列表中生成具有“N”长度的所有可能组合(寻找不重复)

[英]Generate all possible combination with “N” length from given words list (Looking for no repeat)

I would like to generate words sentence with given length.我想生成给定长度的单词句子。 Here i would like to get output without any repeat of characters or words.在这里,我想得到 output 没有任何重复的字符或单词。

With current code i am receiving outputs使用当前代码,我正在接收输出

example example example example example
example example example example example2
example example example example example3
example example example example example4
example example example example example5
example example example example example6
example example example example example7
example example example example example8
example example example example example9
example example example example2 example
example example example example2 example2

But i would like to receive output with random words as without any word repeated in sentence但我想收到带有随机单词的 output,因为句子中没有重复的单词

example example1 example2 example3 example4
example1 example2 example3 example4 example5
example5 example1 example2 example3 example4
example6 example4 example2 example5 example1

Here is the code这是代码

import numpy as np 
# Python 3 program to print all 
# possible strings of length k 
    
# The method that prints all 
# possible strings of length k. 
# It is mainly a wrapper over 
# recursive function printAllKLengthRec() 
def printAllKLength(set, k): 

    n = len(set) 
    printAllKLengthRec(set, "", n, k) 

# The main recursive method 
# to print all possible 
# strings of length k 
def printAllKLengthRec(set, prefix, n, k): 
    
    # Base case: k is 0, 
    # print prefix 
    if (k == 0) : 
        print(prefix) 
        return

    # One by one add all characters 
    # from set and recursively 
    # call for k equals to k-1 
    for i in range(n): 

        # Next character of input added 
        newPrefix = prefix + set[i] 
        
        # k is decreased, because 
        # we have added a new character 
        printAllKLengthRec(set, newPrefix, n, k - 1) 

# Driver Code 
if __name__ == "__main__": 
    
    
    print("\nSecond Test") 
    set2 = ['example ', 'example2 ', 'example3 ', 'example4 ', 'example5 ', 'example6 ', 'example7 ', 'example8 ', 'example9 '] 
    k = 5
    printAllKLength(set2, k) 

# This code is contributed 
# by ChitraNayal 

You just have to keep track of the characters you haven't used in your set, so you don't repeat them.你只需要记录你在你的系列中没有使用过的角色,所以你不要重复它们。 Here I have a list comprehension called remaining_char:在这里,我有一个名为 remaining_char 的列表理解:

def printAllKLength(set, k):
    n = len(set)
    printAllKLengthRec(set, "", n, k)

def printAllKLengthRec(set, prefix, n, k):
    if (k == 0):
        print(prefix)
        return
    remaining_char = [char for char in set if char not in prefix]
    for i in range(len(remaining_char)):
        newPrefix = prefix + remaining_char[i]
        printAllKLengthRec(set, newPrefix, n, k - 1)

In response to your comment, to add space between the words, you can modify your wrapper function as follows:作为对您的评论的回应,要在单词之间添加空格,您可以修改您的包装器 function 如下:

def printAllKLength(set, k):
    set = {word + "   " for word in set}
    n = len(set)
    printAllKLengthRec(set, "", n, k)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定一个数字,生成长度为n的X和O的所有可能组合的列表 - Given a number, generate a list of all possible combination of X and O with length n 从 python 中的给定单词列表创建所有可能组合的列表 - creating a list of all possible combination from a given list of words in python 从列表中获取n长度的所有组合 - Get all combination with n -length from a list 列出给定长度限制 k 的列表 1 到 n 的所有组合 - Listing all combination of list 1 to n given length restriction k 从列表字母中打印所有可能的长度为 10 的单词的组合,其中重复“A”两次 - Print all possible combination of words of length 10 from a list letters with repeating 'A' exactly twice 从给定字符开始的给定长度的字符列表中创建所有可能的单词 - Create all the possible words from a list of character for a given length that starts with a given character 给定列表的所有可能的列表组合 - All possible combination of lists for a given list 如何生成给定长度的所有可能组合 - How to generate all possible combinations of given length 生成长度为n的列表,其中包含m个可能的元素 - Generate a list of length n with m possible elements 关于列出给定 n 和 k 的所有可能组合的问题(Python) - Question about listing all possible combination given n and k (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM