简体   繁体   English

Python:列表理解可从随机大小的字符串列表中创建最大n个大小的列表字符串

[英]Python: List comprehension to create a list strings of n-size maximum from list of strings of random size

The idea is to create a list of strings of a certain amount of characters preserving the order from the original list. 这个想法是创建一个包含一定数量字符的字符串列表,并保留原始列表中的顺序。 The challenge is to accomplish it using only list comprehensions. 挑战是仅使用列表理解来实现。

list_string = [ "aaa", "bb", "cc", "dd", "ee"]
str_len = 6
[some_list_comprehension]

The result should be something like ["aaabb", "ccddee"] . 结果应该类似于["aaabb", "ccddee"] The string aaabb in the result list is 5 characters long, while the string ccddee is 6, that is because strings in the original list cannot be chunked. 结果列表中的字符串aaabb为5个字符长,而字符串ccddee为6,这是因为原始列表中的字符串无法分块。 The order of the strings is relevant while creating the result, but irrelevant in the result, so that the end list could be ["ccddee", "aaabb"] but not ["eeddcc", "bbaaa"] . 字符串的顺序在创建结果时是相关的,但与结果无关,因此结束列表可以是["ccddee", "aaabb"]而不是["eeddcc", "bbaaa"] Each string appears in the result list just the same number of times as in the original, meaning that all possible combinations of the strings is not really the objective in this problem, mostly because each string in the result list is created following the order in the original. 每个字符串出现在结果列表中的次数与原始字符串中出现的次数相同,这意味着字符串的所有可能组合并不是此问题的真正目的,主要是因为结果列表中的每个字符串都是按照字符串中的顺序创建的原版的。 There are only 2 possible outputs : 只有两个可能的输出

["aaabb", "ccddee"]

or 要么

["ccddee", "aaabb"]

Ok, I think I got it, now. 好吧,我想我明白了。 Is this what you were looking for? 这是您要找的东西吗?

>>> list_string = [ "aaa", "bb", "cc", "dd", "ee"]
>>> str_len = 6
>>> [[''.join(list_string[:i]), ''.join(list_string[i:])] for i in xrange(len(list_string)) if all(1 <= len(s) <= str_len for s in [''.join(list_string[:i]), ''.join(list_string[i:])])]
[['aaabb', 'ccddee']]

It yields all possible combinations that might have worked with one partitioning of the string. 它会产生所有可能的组合,这些组合可能已用于字符串的一个分区。 Here are all possible results: 这是所有可能的结果:

>>> for str_len in range(len(''.join(list_string))):
        print str_len, [[''.join(list_string[:i]), ''.join(list_string[i:])] for i in xrange(len(list_string)) if all(1 <= len(s) <= str_len for s in [''.join(list_string[:i]), ''.join(list_string[i:])])]


0 []
1 []
2 []
3 []
4 []
5 []
6 [['aaabb', 'ccddee']]
7 [['aaabb', 'ccddee'], ['aaabbcc', 'ddee']]
8 [['aaa', 'bbccddee'], ['aaabb', 'ccddee'], ['aaabbcc', 'ddee']]
9 [['aaa', 'bbccddee'], ['aaabb', 'ccddee'], ['aaabbcc', 'ddee'], ['aaabbccdd', 'ee']]
10 [['aaa', 'bbccddee'], ['aaabb', 'ccddee'], ['aaabbcc', 'ddee'], ['aaabbccdd', 'ee']]

EDIT: Here is a version which uses filter but doesn't duplicate the result twice in the expression, and has nicer formatting: 编辑:这是一个使用filter的版本,但不会在表达式中重复两次结果,并且格式更好:

>>> filter(
        lambda res: all(1 <= len(s) <= str_len for s in res),
        [[''.join(list_string[:i]), ''.join(list_string[i:])]
         for i in xrange(len(list_string))])
[['aaabb', 'ccddee']]
from itertools import combinations
list_string = [ "aaa", "bb", "cc", "dd", "ee"]
minn=min(map(len,list_string))
maxx=max(map(len,list_string))
str_len=6
lis=[''.join(x) for i in range(1,maxx+1) for x in combinations(list_string,i) if len(''.join(x))<=str_len]        
print lis

output: 输出:

['aaa', 'bb', 'cc', 'dd', 'ee', 'aaabb', 'aaacc', 'aaadd', 'aaaee', 'bbcc', 'bbdd', 'bbee', 'ccdd', 'ccee', 'ddee', 'bbccdd', 'bbccee', 'bbddee', 'ccddee']

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM