Python - 类型错误:“图形”对象不可下标

粗暴地

我正在尝试实现 Graph 和 bfs(广度优先搜索)算法。我有这个 graph_object 类:

class Vertex:
    def __init__(self,id):
        self.id=id #just a character representing each node/vertex name e.g. 'a', 'b'...
        self.neighbors=[]
        '''list of adjacent vertices. we call them neighbors. 
        d.neighbors=[b,c,g]. 
        note that here we are talking about the Vertex objects b,c,g. Not their corresponding vertex IDS ('b', 'c' and 'g')
        '''
        self.parent=None
        '''parent of a vertex depends on order of visit of the vertices in a graph.  

        calling the function bfs() on above graph will assign a.parent=None, b.parent=a , c.parent=d, d.parent=b, g.parent=d '''
    def __str__(self):
        return "{}:{}".format(self.id,self.get_neighbors()) #
    def get_neighbors(self):
        return [v.id for v in self.neighbors]
    def get_id(self):
        return self.id
    def get_parent(self):
        return self.parent
    def add_neigbor(self,v):
        self.neighbors.append(v)

class Graph:
    def __init__(self):
        self.vertices={}
        self.num_vert=0
    def get_vertex(self,v_id):
        if v_id not in self.get_vertices():
            return None
        else:
            return [v for v in self.vertices if v.id==v_id].pop(0) #to understand what happens here, refer to dict_exercise.py
    def add_vertex(self,v_id):
        if v_id not in self.get_vertices():
            new=Vertex(v_id)
            self.vertices[new]=new.neighbors
    def add_edge(self,v1_id,v2_id):
        if v1_id not in self.get_vertices():
            self.add_vertex(v1_id)
        if v2_id not in self.get_vertices():
            self.add_vertex(v2_id)
        #vertices with IDs v1_id and v2_id are now guaranteed to exist in graph, we get the corresponding vertex objects v1 and v2
        v1=self.get_vertex(v1_id)
        v2=self.get_vertex(v2_id)
        if v2_id not in v1.get_neighbors():
            v1.add_neigbor(v2)
        if v1_id not in v2.get_neighbors():
            v2.add_neigbor(v1)
    def get_vertices(self):
        return [v.id for v in self.vertices.keys()]

我还使用了一个包含 bfs 算法的测试类:

from graph_object import *


def bfs(g, s):
    explored, queue = [], [s]
    while queue:
        current_vertex = queue.pop(0)
        if current_vertex not in explored:
            explored.append(current_vertex)
            neighbors = g[current_vertex]
            queue += neighbors
    return explored


graph = Graph()

graph.add_edge('a', 'b')
graph.add_edge('b', 'c')
graph.add_edge('c', 'd')
graph.add_edge('d', 'b')
graph.add_edge('d', 'g')

print("List of vertex IDs in graph:")
vertices = graph.get_vertices() #returns graph vertices as a list of vertex IDs
print(vertices)

vertices_dict = graph.vertices
print("Vertex information in format <vertex id>:<list of neighbors>")
for vertex in vertices_dict.keys():
    print(vertex)


print("BFS(a) explored the elements in graph in the order:")
print(*bfs(graph, 'a'), sep=', ')

当我运行代码时,我收到此错误:

Traceback (most recent call last):
List of vertex IDs in graph:
File "C:/Users/rawsly/PycharmProjects/MiniProject/test.py", line 40, in <module>
['a', 'b', 'c', 'd', 'g']
print(*bfs(graph, 'a'), sep=', ')
Vertex information in format <vertex id>:<list of neighbors>
File "C:/Users/rawsly/PycharmProjects/MiniProject/test.py", line 10, in bfs
a:['b']
neighbors = g[current_vertex]
b:['a', 'c', 'd']
TypeError: 'Graph' object is not subscriptable

我知道最好在 bfs 中使用集合结构进行探索,但后来我意识到输出没有排序,因为集合结构没有排序。因此我想改用列表另一方面,使用dequeue也更有效,但现在我没有考虑效率。我稍后会改进代码,但首先,我想解决这个错误。

为了娱乐和利润

您访问图表的方式是错误的。它不是字典,所以它不知道如何按照您现在编码的方式获取项目。您还会遇到队列问题,因为这不是您将项目添加到列表的方式。您可以使用图形类中的顶点获取器,并将其添加到可以使用附加的队列列表中。

def bfs(g, s):
    explored, queue = [], [s]
    while queue:
        current_vertex = queue.pop(0)
        print(current_vertex)
        if current_vertex not in explored:
            explored.append(current_vertex)
            neighbors = g.get_vertex(current_vertex).get_neighbors()
            queue.extend(neighbors)
    return explored

编辑:我看到您想扩展队列而不是追加。您还想使用 get_neighbors 函数以及获取顶点。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python TicTacToe 错误:类型错误:“函数”对象不可下标

来自分类Dev

python函数中的类型错误(int对象不可下标)

来自分类Dev

类型错误:“int”对象不可下标 - Python3

来自分类Dev

类型对象不可下标-python

来自分类Dev

Python错误:int对象不可下标

来自分类Dev

Python:使用API从JSON中提取数据,然后发送到Excel。获取类型错误:NoneType对象不可下标

来自分类Dev

Python'TypeError':'Generator'对象不可下标

来自分类Dev

Python:TypeError:“ int”对象不可下标

来自分类Dev

TypeError:'float'对象不可下标--Python

来自分类Dev

Python TypeError:“设置”对象不可下标

来自分类Dev

TypeError:“ NoneType”对象不可下标(PYTHON)

来自分类Dev

Python TypeError:“函数”对象不可下标

来自分类Dev

NLTK python错误:“ TypeError:'dict_keys'对象不可下标”

来自分类Dev

类型错误:“int”对象不可下标?

来自分类Dev

类型错误:“机架”对象不可下标

来自分类Dev

类型错误:“方法”对象不可下标

来自分类Dev

类型错误:“发布”对象不可下标

来自分类Dev

响应”对象不可下标Python http发布请求

来自分类Dev

Python-偶然发现“'DictReader'对象不可下标”

来自分类Dev

Python重新申请/搜索TypeError:“ NoneType”对象不可下标

来自分类Dev

TypeError:“ int”对象在Python3中不可下标

来自分类Dev

Python 3.8中的连续'TypeError:'function'对象不可下标'

来自分类Dev

Django / Python:/'NoneType'对象处的TypeError不可下标

来自分类Dev

Python-TypeError:“ NoneType”对象不可下标

来自分类Dev

TypeError:“函数”对象不可下标[Python3]

来自分类Dev

Python网页抓取:TypeError:'int'对象不可下标

来自分类Dev

Python 3.6 - TypeError:'zip' 对象不可下标

来自分类Dev

类型错误:“类型”对象不可下标+迭代为“A”而不是A

来自分类Dev

django提出的“功能”对象不可下标,但是本地python不可以

Related 相关文章

  1. 1

    Python TicTacToe 错误:类型错误:“函数”对象不可下标

  2. 2

    python函数中的类型错误(int对象不可下标)

  3. 3

    类型错误:“int”对象不可下标 - Python3

  4. 4

    类型对象不可下标-python

  5. 5

    Python错误:int对象不可下标

  6. 6

    Python:使用API从JSON中提取数据,然后发送到Excel。获取类型错误:NoneType对象不可下标

  7. 7

    Python'TypeError':'Generator'对象不可下标

  8. 8

    Python:TypeError:“ int”对象不可下标

  9. 9

    TypeError:'float'对象不可下标--Python

  10. 10

    Python TypeError:“设置”对象不可下标

  11. 11

    TypeError:“ NoneType”对象不可下标(PYTHON)

  12. 12

    Python TypeError:“函数”对象不可下标

  13. 13

    NLTK python错误:“ TypeError:'dict_keys'对象不可下标”

  14. 14

    类型错误:“int”对象不可下标?

  15. 15

    类型错误:“机架”对象不可下标

  16. 16

    类型错误:“方法”对象不可下标

  17. 17

    类型错误:“发布”对象不可下标

  18. 18

    响应”对象不可下标Python http发布请求

  19. 19

    Python-偶然发现“'DictReader'对象不可下标”

  20. 20

    Python重新申请/搜索TypeError:“ NoneType”对象不可下标

  21. 21

    TypeError:“ int”对象在Python3中不可下标

  22. 22

    Python 3.8中的连续'TypeError:'function'对象不可下标'

  23. 23

    Django / Python:/'NoneType'对象处的TypeError不可下标

  24. 24

    Python-TypeError:“ NoneType”对象不可下标

  25. 25

    TypeError:“函数”对象不可下标[Python3]

  26. 26

    Python网页抓取:TypeError:'int'对象不可下标

  27. 27

    Python 3.6 - TypeError:'zip' 对象不可下标

  28. 28

    类型错误:“类型”对象不可下标+迭代为“A”而不是A

  29. 29

    django提出的“功能”对象不可下标,但是本地python不可以

热门标签

归档