簡體   English   中英

使用字典從 csv 文件創建圖形

[英]Create a graph from a csv file using a dictionary

我正在嘗試做一個學術項目(我不能使用 python 中的任何庫)從 csv 文件中的數據構建圖形數據結構。

首先,我正在閱讀 csv 文件並將信息放入字典中,使用:

def github_csv():
    with open('Github1.csv', 'r') as csv_file:
        data = csv.DictReader(csv_file)
        next(data)
        for row in data:
            print(row)

output 是:

{'follower': '9236', 'followed': '1570'}
{'follower': '13256', 'followed': '9236'}
{'follower': '9236', 'followed': '13256'}

我的第一個疑問是有什么辦法可以這樣說:

'9236': ['1570', '13256']
'13256': ['9236']

那么如何將鍵值分配給一個頂點並為另一個頂點分配相應的值,然后創建邊呢? 我的圖表 class 是:

class Graph:
        
    def __init__(self, directed=False):
        self._directed = directed
        self._number = 0           
        self._vertices = {}         

    def insert_vertex(self, x):
        v = Vertex(x)
        self._vertices[v] = {}     
        self._number = len(self._vertices)
        return v

    def insert_edge(self, u, v, x=None):
        e = Edge(u, v, x)
        self._vertices[u][v] = e  
        self._vertices[v][u] = e  

編輯:

class Vertex:

    __slots__ = "_element"

    def __init__(self, x):
        self._element = x

    def vertice(self):
        return self._element

    def __eq__(self, other):
        if isinstance(other, Vertex):
            return self._element == other.vertice()
        return False

    def __repr__(self):
        return '{0}'.format(self._element)

    def __hash__(self):
        return hash(id(self))  


class Edge:

    __slots__ = '_origin', '_destination', '_weight'

    def __init__(self, u, v, p=None):
        self._origin = u
        self._destination = v
        self._weight = p

    def __hash__(self):
        return hash((self._origin, self._destination))

    def __repr__(self):
        if self._weight is None:
            return '({0}, {1})'.format(self._origin, self._destination)
        return '({0}, {1}, {2})'.format(self._origin, self._destination, self._weight)

    def endpoints(self):
        return self._origin, self._destination

    def opposite(self, v):
        return self._origin if v is self._destination else self._origin

    def cost(self):
        return self._weight

    def show_edge(self):
        print('(', self._origin, ', ', self._destination, ') com peso', self._weight)

關於第一個問題:

lista = [{'follower': '9236', 'followed': '1570'},
{'follower': '13256', 'followed': '9236'},
{'follower': '9236', 'followed': '13256'}]

rel_dict = {}
for d in lista:
    if d["follower"] in rel_dict.keys():
        rel_dict[d["follower"]].append(d["followed"])
    else:
        rel_dict[d["follower"]] = [d["followed"]]

rel_dict:

{'9236': ['1570', '13256'], '13256': ['9236']}

EDIT2(帶有頂點和邊定義):

要將這些數據添加到圖表中:

graph = Graph()
for k,v in rel_dict.items():
    k_vertex = graph.insert_vertex(k)
    for v_item in v:
        v_item_vertex = graph.insert_vertex(v_item)
        graph.insert_edge(k_vertex, v_item_vertex)

假設你得到row作為你提到的字典,

edges_dict = {}
def github_csv():
with open('Github1.csv', 'r') as csv_file:
    data = csv.DictReader(csv_file)
    next(data)
    for row in data:
        edges_dict[ row['follower'] ].append(row['followed'])

這應該根據需要為您提供一個edges_dict列表字典。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM