【问题标题】:Making Matrices without a library / Inserting nested lists without anything being in a list在没有库的情况下制作矩阵/在列表中没有任何内容的情况下插入嵌套列表
【发布时间】:2018-05-24 07:07:30
【问题描述】:

我正在尝试在没有库的情况下在 Python 中输入然后打印一个矩阵。

代码:

a = []
n = int(input('Length of the row'))
m = int(input('Length of the column'))
for j in range(0, m):
    a[j].append([])
for i in range(0, n):
    for j in range(0, m):
        a[i][j] = int(input())

for i in range(0, n):
    print()
    for j in range(0, m):
        print(a[i][j], end=" ")

工作:

当我列出我的清单时,让我们说:

a = [[1,1,1],[1,1,1],[1,1,1]]

mn都设为3,就可以正常工作了。 p>

错误:

但只有一个空列表,如代码示例中所示,我总是收到列表索引超出范围的错误。

a[j].append([]) IndexError: list index out of range

问题:

我不知道如何在列表中输入嵌套列表,在这些嵌套列表中输入整数,使用循环,或任何与此相关的东西。

【问题讨论】:

  • 你从哪里得到错误,错误的确切文本是什么?
  • a[j].append([]) IndexError: list index out of range

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


【解决方案1】:

注定会失败:

a = []
# ...
for j in range(0, m):
    a[j].append([])  # a is empty, so a[j] must fail!

试试吧:

a = []
n = int(input('Length of the row'))
m = int(input('Length of the column'))
for i in range(m):  # m is len of outer list: number of rows == len of column
    a.append([])
    for j in range(n):  # n is len of inner list (those are the rows)
        a[i].append(int(input()))  
        # append as well, as indexes to fill do not exist yet

# the loop could be written as a comprehension:
a = [[int(input()) for _ in range(n)] for _ in range(m)]

for i in range(m):
    print()
    for j in range(n):
        print(a[i][j], end=" ")

【讨论】:

    【解决方案2】:

    删除第一个循环,然后使输入循环看起来像这样:

    a = [None] * n
    for i in range(n):
        a[i] = []
        for j in range(m):
            a[i].append(int(input()))
    

    【讨论】:

      【解决方案3】:

      append 将元素添加到列表中。 a 起初是空的,所以 a[i] 是一个错误。查看我的示例,它可以满足您的需求。

      a = []
      n = int(input('Enter row count: '))
      m = int(input('Enter column count: '))
      for i in range(0, n):
          a.append([])
      for i in range(0, n):
          for j in range(0, m):
              a[i].append(int(input()))
      
      
      for i in range(0, n):
          print()
          for j in range(0, m):
              print(a[i][j], end=" ")
      print()
      

      示例运行:

      Enter row count: 2
      Enter column count: 3
      1
      2
      3
      4
      5
      6
      
      1 2 3 
      4 5 6 
      

      【讨论】:

        【解决方案4】:

        当您创建 a 时,它是空的,因此您不能不索引其中的任何内容。

        >>> a = []
        >>> len(a)
        0
        >>> a[0]
        Traceback (most recent call last):
          File "<pyshell#126>", line 1, in <module>
            a[0]
        IndexError: list index out of range
        >>> 
        

        只需附加空列表:

        >>> a.append([])
        >>> a
        [[]]
        >>> len(a)
        1
        >>> a[0]
        []
        >>>
        

        在添加到内部循环时,您会遇到类似的问题。可以这样做

        >>> i = 0
        >>> a[i].append(int(input()))
        4
        >>> a
        [[4]]
        >>>
        

        【讨论】:

          【解决方案5】:

          你有两个错误:

          1. 您对行和列的定义不一致
          2. 您正在处理尚未分配任何内存的数组/列表内容

          此外,您不需要第三个 for 循环。

          n=2
          m=3
          a=[]
          for i in range(0, n):
              a.append([])
              for j in range(0, m):
                  a[i].append( int(input()) )
          print(a) 
          

          对于输入 1-6,它为您提供

          1
          2
          3
          4
          5
          6
          [[1, 2, 3], [4, 5, 6]]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-07-10
            • 1970-01-01
            • 2019-04-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-04-15
            • 1970-01-01
            相关资源
            最近更新 更多