简体   繁体   English

如何从字符串列表中创建特定长度的所有可能订单

[英]how to create all possible orders in a specific length from a list of strings

I have a list of strings that need to fit in 6 characters.我有一个需要容纳 6 个字符的字符串列表。 The strings can be split but the characters in the string can't be randomized.字符串可以拆分,但字符串中的字符不能随机化。 The strings have different lengths (4 and 3 characters)字符串具有不同的长度(4 个和 3 个字符)

I tried a few things with itertools and know how to get all possibilities but not how to get only the possibilities with a specific length requirement.我用 itertools 尝试了一些东西,并且知道如何获得所有可能性,但不知道如何仅获得具有特定长度要求的可能性。

It's ok to omit the first zero from the list entries.可以从列表条目中省略第一个零。

An example of a list:列表示例:

wordlist = ["0254", "0294", "0284", "0289", "027", "024", "026", "088"]

It would be ok to get combinations like 025427 , 254027 , 270254 , 027254 (0 and 4 of the list) and the obvious 027088 , 088027 (4 and 7 of the list) and even 272488 (4, 5 and 7 of the list)可以得到025427254027270254027254 (列表的 0 和 4 )和明显的027088088027 (列表的 4 和 7 )甚至272488 (列表的 4 、 5 和 7 )这样的组合

I think the solution lies in itertools in combination with something else.我认为解决方案在于 itertools 与其他东西的结合。

Just use a standard double loop and check if you need/can remove zeros to get the desired length只需使用标准双循环并检查您是否需要/可以删除零以获得所需的长度

combinations = []
for i in wordlist:
    for j in wordlist:
        if len(i) == 4 and len(j) == 4:  # remove both zeros
            combinations.append(i[1:] + j[1:]) 
        elif len(i) == 3 and len(j) == 3:
            combinations.append(i + j)  # dont remove any zero
        else:
            combinations.append(i[1:] + j)  # remove first element zero
            combinations.append(i + j[1:])  # remove second element zero

This will give you all possible combinations (including matching elements with themselves)这将为您提供所有可能的组合(包括与自身匹配的元素)

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

相关问题 如何从Python的字符串列表中创建所有可能的长度为100个字符的句子 - How to create all possible sentence of length 100 characters from a list of strings in Python 从字符串创建特定列表 - create a specific list from strings 使用可变长度字符串的可变长度列表,如何创建所有组合 - With variable length list of variable length strings, how do I create all combinations 如何用所有可能的组合替换字典中列表中的字符串 - How to replace strings in list from a dictionary with all possible combinations 创建一个计算特定长度列表中的单词/字符串的函数-Python 3 - Create a function that counts words/strings in a list of a specific length - Python 3 从令牌列表中生成所有可能的字符串 - Generate all possible strings from a list of token 列出 python 中一组项目的所有可能订单 - List of all possible orders of a set of items in python 从 python 中的字符串列表创建给定长度的随机列表 - Create random list of given length from a list of strings in python 从给定字符开始的给定长度的字符列表中创建所有可能的单词 - Create all the possible words from a list of character for a given length that starts with a given character 如何从另一个列表创建所有可能值的列表 - How to create the list of all possible values from another list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM