简体   繁体   English

如何从python列表中的字符中获取length=n的所有字符串

[英]How to get all strings of length=n from the characters in a list in python

I would like to know a simple and pythonic way to get all strings of length n that are composed of the characters contained in a list called L .我想知道一种简单且 Pythonic 的方法来获取所有长度为n 的字符串,这些字符串由名为L的列表中包含的字符组成。

For example:例如:

L = ['0','1']
n = 3

How do I get:如何得到:

['000','001','010','011','100','101','110','111']

A possible solution is by using itertools.product .一个可能的解决方案是使用itertools.product It works, but it's not very elegant, so I'm looking for a more pythonic solution.它有效,但不是很优雅,所以我正在寻找一个更 Pythonic 的解决方案。 Here's how it would be:这是它的样子:

L = ['0','1']
n = 3
a = [L for i in range(0,n)]
x = list(itertools.product(*a))
x = ["".join(i) for i in x]

The resulting list looks like this:结果列表如下所示:

>>> x
['000', '001', '010', '011', '100', '101', '110', '111']

Is there something built-in like all_possible_strings(L, n) , which takes the elements in L and obtains all possible combinations of length n with those elements?是否有像all_possible_strings(L, n)这样的内置all_possible_strings(L, n) ,它采用L 中的元素并获得长度为n 的所有可能的组合与这些元素?

The following code with Python 2.6 and above ONLY以下代码仅适用于 Python 2.6 及更高版本

First, import itertools:首先,导入itertools:

import itertools

Permutation (order matters):排列(顺序很重要):

print list(itertools.permutations([1,2,3,4], 2))
[(1, 2), (1, 3), (1, 4),
(2, 1), (2, 3), (2, 4),
(3, 1), (3, 2), (3, 4),
(4, 1), (4, 2), (4, 3)]

Documentation:文档:

https://docs.python.org/2/library/itertools.html#itertools.permutations https://docs.python.org/2/library/itertools.html#itertools.permutations

Happy Coding快乐编码

Using the repeat option of product seems to give you what you want more directly:使用的重复选项产品似乎给你更直接地想要的东西:

L = ['0','1']
n = 3
x = list(itertools.product(L, repeat=n))
x = ["".join(i) for i in x]
print(x)

Output:输出:

['000', '001', '010', '011', '100', '101', '110', '111']

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

相关问题 如何获取长度为 n 的所有二进制数的列表? - How to get a list of all binary numbers of length n? 如何获取字符列表中的所有子字符串(Python) - How to get all substrings in a list of characters (python) 如何从列表列表中的所有列表元素中删除所有 \\n 字符 - How can I remove all the \n characters from all the list elements in list of list 如何从python中的字符串列表中去除多个不需要的字符? - How to strip multiple unwanted characters from a list of strings in python? 生成具有字符a,b或c的所有字符串,其长度等于n,最多为1 b和2 c - Generate all strings having characters a,b or c with length equal to n with atmost 1 b and 2 c 从python中指定的字符串中获取n个字符 - Get n Characters from String specified in python 如何在正则表达式中匹配仅特定长度字符的字符串? (蟒蛇) - How to match strings of only a specific length of characters in regex? (Python) 如何从python 3列表中删除所有非数字字符? - How to remove all non numeric characters from a python 3 list? 如何从 python 中的字符串中删除所有字符,直到第 n 个字符 X 出现 - How to delete all the characters until the n'th occurence of character X from a string in python 如何在python中获取给定输入文件的所有列表的所有标记的长度? - How to get length of all the tokens of all the list for a given input file in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM