【问题标题】:Convert integer to space separated list in python [closed]在python中将整数转换为空格分隔列表[关闭]
【发布时间】:2020-03-18 10:50:38
【问题描述】:

我的输入为:

第一行包含两个空格分隔的整数 N 和 M。然后在下一行是矩阵 A 的 NxM 输入: 输入:

4 5
11110
11010
11000
00000

我希望输出为列表列表(整数)

输出:

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

【问题讨论】:

    标签: python python-3.x list arraylist input


    【解决方案1】:

    在竞争编码中,输入如下所示:

    试试:

    n,m = map(int, input().split())
    
    mat = []
    for i in range(n):
        row = list(map(int,list(input())))
        mat.append(row)
    
    print(mat)
    

    垫子:

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

    您可以通过here进行测试

    【讨论】:

      【解决方案2】:

      将输入读取为字符串。那么你可以使用这个sn-p来获取输出

      op_list = []
      def to_list(number):
          return [int(dig) for dig in number]
      
      op_list.append(to_list("11110"))
      op_list.append(to_list("11010"))
      op_list.append(to_list("11000"))
      op_list.append(to_list("00000"))
      
      print(op_list)
      

      【讨论】:

        【解决方案3】:
        import numpy as np
        
        with open('draft.txt', 'r') as src:
            lines = src.read().splitlines()
            row_count, col_count = lines[0].split(' ')
        
            m = np.zeros((int(row_count), int(col_count)))
        
            for i in range(1, len(lines)):
                m[i-1, :] = list(lines[i])
        
        print(m)
        

        【讨论】:

          【解决方案4】:

          假设input.txt 文件与您的.py 文件位于同一目录中,并且包含以下内容:

          4 5
          11110
          11010
          11000
          00000
          

          您可以使用以下方法将矩阵作为列表列表:

          with open('./input.txt', 'r') as file:
              lines = file.readlines()
          
              header = lines[0]
              num_rows, num_cols = (int(x) for x in header.split(' '))
              print(num_rows, num_cols)
          
              matrix = []
              for i in range(1, len(lines)):
                  line = lines[i]
                  line = line.rstrip() # get rid of '\n'
                  as_nums = [int(num) for num in line]
                  assert len(as_nums) == num_cols
                  matrix.append(as_nums)
          
              assert len(matrix) == num_rows
          

          【讨论】:

            【解决方案5】:

            以下可能会有所帮助:

            inp = input()
            rows, columns = map(int, inp.split())
            
            lines = []
            for i in range(rows):
              line = input()[:columns]
              lines.append([int(l) for l in line])
            
            print(lines)
            

            输入:

            4 5
            11110
            11010
            11000
            00000
            

            输出:

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

            【讨论】:

              猜你喜欢
              • 2020-04-14
              • 1970-01-01
              • 2018-07-21
              • 2021-03-13
              • 2022-08-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多