In networkx, how to update edges weight in a vectorized way?
<p>I have to define a network where the weight of each edge must be equal to the number of connections between each pair of nodes. The following code generates such network:</p>

<pre><code>In [1]: import networkx as nx

In [2]: g = nx.Graph()

In [3]: connections = [[1,2],[2,3],[1,2],[2,1],[1,4],[2,3]]

In [4]: for e1,e2 in connections :
    if g.has_edge(e1,e2) :
        g[e1][e2]['weight'] += 1
    else :
        g.add_edge(e1,e2,weight=1)
    ...:         

In [5]: g.edges(data=True)
Out[5]: [(1, 2, {'weight': 3}), (1, 4, {'weight': 1}), (2, 3, {'weight': 2})]
</code></pre>

<p>In the real situation, the connection list will contain thousands of pairs. Thousands of such lists will be generated, and each of them must be immediately included in the network and deleted since there is no memory to store all lists together.</p>

<p>Since Python is an interpreted language, I can not use the command "for", because it would take forever to run. Maybe "vectorize" is not the proper work, what I mean is something similar to what we do with numpy arrays, where there are commands that operate on all elements at once, instead of using the command "for" to operate in each element.</p>