将字符串分解为整数列表的列表

时间:2015-08-09 17:39:03

标签: python string list dictionary integer

我有这段代码:

content = [line.rstrip('\r\n') for line in open('Ch02/digits/testDigits/0_0.txt')]
content = [[x] for x in content]
content[0][0]

我得到了这个输出:

'00000000000001100000000000000000'

说我想将此字符串转换为整数列表,如: [[0],[0],....,[0]],什么是pythonic解决方案?

2 个答案:

答案 0 :(得分:3)

s = '001100'

list_of_lists = [[int(ch)] for ch in s]

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

答案 1 :(得分:0)

content = [line.rstrip('\r\n') for line in open('Ch02/digits/testDigits/0_0.txt')]
lst = []
for each in content:
    lst.append([[x] for x in each])
lst = np.array(lst)
lst.reshape(32,32)