Python,自动阵列形成数据检索错误

Python, Automated Array Formation Data Retrieval Error

我试图创建一个二维数组,然后从数组中提取数据并在数组中的特定点插入数据。下面是我为创建二维数组编写的一些代码:

from array import *
import math, random

TDArrayBuilder = []
TDArray = []
for yrunner in range(3):
    for xrunner in range(3):
        TDArrayBuilder.append(random.randint(0,1))
    TDArray.insert(yrunner, [TDArrayBuilder])
    TDArrayBuilder = []

print(TDArray[0][2])

这次吐出的Error如下:

Traceback (most recent call last):
File "C:/TestFile.py", line 13, in
print(TDArray[0][2])
IndexError: list index out of range

在此之前我还写了一些关于查找和打印二维数组中的最小值和最大值的代码,它很容易能够在指定位置打印值。我很确定这只是因为我使用了 numpy,但我仍然想在没有 numpy 的情况下这样做。

示例代码:

import numpy as np  #required Import
import math
#preset matrix data
location = []       #Used for locations in searching
arr = np.array([[11, 12, 13],[14, 15, 16],[17, 15, 11],[12, 14, 15]]) #Data matrix
result = np.where(arr == (np.amax(arr)))    #Find the position(s) of the lowest data or the highest data, change the np.amax to npamin for max or min respectively
listofCoordinates = list(zip(result[0], result[1])) #Removes unnecessary stuff from the list
for cord in listofCoordinates:  #takes the coordinate data out, individually
    for char in cord:           #loop used to separate the characters in the coordinate data
        location.append(char)   #Stores these characters in a locator array

length = (len(location))                #Takes the length of location and stores it
length = int(math.floor((length / 2)))  #Floors out the length / 2, and changes it to an int instead of a float
for printer in range(length):           #For loop to iterate over the location list
    ycoord = location[(printer*2)]      #Finds the row, or y coord, of the variable
    xcoord = location[((printer*2)+1)]  #Finds the column, or x coord of the variable
    print(arr[ycoord][xcoord])          #Prints the data, specific to the location of the variables

总结:

我希望能够从二维数组中检索数据,但我不知道该怎么做(关于第一个代码)。我使用 numpy 创建了一个文件并且它可以工作,但是,我不想在当前的操作中使用它。任何事情都会有所帮助

from random import randint

TDArray = list()
for yrunner in range(3):
    TDArrayBuilder = list()
    for xrunner in range(3):
        TDArrayBuilder.append(randint(0, 1))
    TDArray.insert(yrunner, TDArrayBuilder)

print(TDArray)
print(TDArray[0][2])

TDArray = [[randint(0, 1) for _ in range(3)] for _ in range(3)]
print(TDArray)
print(TDArray[0][2])