Alex Nikitin
Alex Nikitin

Reputation: 524

Transforming Python list to networkx graph

Currently I have a list:

[['Мама мыть', 10, 'рама'],
 ['Мама мыть', 10, 'рама', 5, 'долго'],
 ['Мама мыть', 10, 'рама', 3, 'вчера'],
 ['Мама мыть', 10, 'рама', 3, 'вчера', 1, 'поздно']]

I need somehow to convert it to Networkx edges, where pairs of words should become nodes of the graph, and integers between become weights :

G = nx.Graph()
G.add_edge('Мама мыть', 'рама', weight=10)
G.add_edge('рама', 'долго', weight=5)
G.add_edge('рама', 'вчера', weight=3)
G.add_edge('вчера', 'поздно', weight=1)

Currently I'm stuck and have no ideas. Any help would be appreciated!

Upvotes: 6

Views: 3552

Answers (1)

Julien Marrec
Julien Marrec

Reputation: 11895

Since you have repeated information, I suggest starting by creating a dictionary to ensure uniqueness. I add a check to make sure you don't have conflicting distances for repeated elements.

In [1]:
distances = {}
for row in l:
    for i in range(0, len(row)-1, 2):
        key_tuple = (row[i], row[i+2])
        d = row[i+1]
        if key_tuple in distances.keys():
            if distances[key_tuple] != d:
                print("Warning: Found a conflicting distance for {}: {} and "
                      "{}. Using last".format(key_tuple, distances[key_tuple], d))
        distances[key_tuple] = d

In [2]: distances
Out[2]:
 {('Мама мыть', 'рама'): 10,
 ('рама', 'долго'): 5,
 ('рама', 'вчера'): 3,
 ('вчера', 'поздно'): 1}

Then you can create your edges using that dictionary.

In [3]:
import networkx as nx
G = nx.Graph()
for k, v in distances.items():
    G.add_edge(k[0], k[1], weight=v)

Upvotes: 3

Related Questions