索引器错误:标量变量的索引无效,我用其他简单函数尝试了相同的公式,但不起作用

2024-09-30 14:26:25 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行python 3部分代码时:

def tambah_edges(self,edges) :
    for z in (0,1) :
        for j in self.all_edges :
            if j[0][0] == j[1][0] and j[0][0]<j[1][1] :
                sisi = np.arange(j[0][1],j[1][1]+1)
                    for k in sisi :
                    v =  edges[z]==[j[0][0],k]
                        if v.all() :
                            #memisahkan edges yang berpotongan dengan edges baru
                            temp_1 = [j[0],edges[z]]
                            temp_2 = [edges[z],j[1]]
                            #menghapus edges dan nodes lama yang sudah terpotong
                            self.all_edges = np.delete(self.all_edges, np.where(self.all_edges == j))
                            #memasukkan edges baru
                            self.all_edges = np.concatenate(temp_1,axis=0)
                            self.all_edges = np.concatenate(temp_2,axis=0)
                            #memasukkan nodes baru
                            if not edges[z] in self.all_nodes :
                                self.all_nodes = np.append(self.all_nodes,edges[z],axis=0)
                                self.intersection = np.append(self.intersection,edges[z],axis=0)

突然,我收到错误消息,说:

Traceback (most recent call last):
  File "C:\Users\Isama\Desktop\eksperimen\cpp.py", line 499, in <module>
    main()
  File "C:\Users\Isama\Desktop\eksperimen\cpp.py", line 482, in main
    map_exploration()
  File "C:\Users\Isama\Desktop\eksperimen\cpp.py", line 468, in map_exploration
    robot.tambah_edges(ez)
  File "C:\Users\Isama\Desktop\eksperimen\cpp.py", line 169, in tambah_edges
    if j[0][0] == j[1][0] and j[0][0]<j[1][1] :
IndexError: invalid index to scalar variable.

我不明白为什么会出现这样的问题,我已经用另一个更简单的代码尝试了这些代码,它正在工作

self.all_边是一个三维数组,其中包含以下内容:

[[[ 5  1]
  [ 5  6]]

 [[ 5  6]
  [12  6]]

 [[12  6]
  [12  1]]

 [[12  1]
  [14  1]]

 [[14  1]
  [14 14]]

 [[14 14]
  [ 1 14]]

 [[ 1 14]
  [ 1  1]]

 [[ 1  1]
  [ 5  1]]]

另一次,我使用与此代码相同的格式执行更简单的任务:

def kuda(self) :
        for j in self.all_edges :
            if j[0][0] == j[1][0] and j[0][1] < j[1][1]:
                print(j)

它打印了这个:

[[5 1]
 [5 6]]
[[14  1]
 [14 14]]

从第一个示例的错误消息中,我可以肯定“j”数组索引的问题,但我不明白为什么它只处理一组问题而不处理另一组问题

我是python新手,非常感谢大家的帮助

谢谢


Tags: 代码inselfforifnpallusers