Python:如何转换嵌套列表中的数字列表?

时间:2017-04-08 09:10:31

标签: python python-2.7

我有一个二进制数列表,如下所示:

['11100', '11010', '11001', '10110', '10101', '10011', '01110', '01101', '01011', '00111']

是否有方法创建包含子列表的嵌套列表,其中元素是副数的单个数字?

new_list = [[1, 1, 1, 0, 0], [1, 1, 0, 1, 0], [1, 1, 0, 0, 1], [1, 0, 1, 1, 0], [1, 0, 1, 0, 1], [1, 0, 0, 1, 1], [0, 1, 1, 1, 0], [0, 1, 1, 0, 1], [0, 1, 0, 1, 1], [0, 0, 1, 1, 1]]

提前致谢!

2 个答案:

答案 0 :(得分:4)

使用像这样的列表理解

CUDA_VISIBLE_DEVICES= python neural_style.py <content file> --styles <style file> --output <output file>

答案 1 :(得分:1)

另一种可能的解决方案是:

oldlist = ['11100', '11010', '11001', '10110', '10101', '10011', '01110', '01101', '01011', '00111']
newlist = [list(map(int,list(x))) for x in oldlist]
print(newlist)

并提供此输出:

[[1, 1, 1, 0, 0], [1, 1, 0, 1, 0], [1, 1, 0, 0, 1], [1, 0, 1, 1, 0], [1, 0, 1, 0, 1], [1, 0, 0, 1, 1], [0, 1, 1, 1, 0], [0, 1, 1, 0, 1], [0, 1, 0, 1, 1], [0, 0, 1, 1, 1]]