【问题标题】:Break a string into list of list of integers将字符串分解为整数列表
【发布时间】:2015-11-01 15:03:50
【问题描述】:

我有这个代码:

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的解决方案是什么?

【问题讨论】:

    标签: python string list dictionary integer


    【解决方案1】:
    s = '001100'
    
    list_of_lists = [[int(ch)] for ch in s]
    
    >> [[0], [0], [1], [1], [0], [0]]
    

    【讨论】:

      【解决方案2】:
      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多