Graphics Reference
In-Depth Information
Tip
Python provides convenient ways to access lists using indexes,
including shortcuts for getting the last item on a list or sublist. You can
use the same techniques to access subsets of strings, too.
Each
row
is processed with a
for
loop. For each row, the function
addNode
is called. The function
addNode
takes one argument: the node ID (that is,
the person's name).
addNode
is called twice, first for the source node (the
first two items in the row), followed by the target node (the next item in the
row of data).
Collecting Nodes in a Hashmap
At the beginning of the Python script, the
addNode()
function is defined
(
def
), which adds and updates nodes in a global
hashmap
declared in
nodemap={}
. A hashmap is a type of data structure that allows for efficient
access of items using a
unique key
. Conveniently, nodes in graphs must
have a unique ID, so this ID can be used as the key for a hashmap. In
addition to the key, hashmaps can store additional values, lists, or objects.
Here the hashmap is used to store node objects:
nodemap = {}
# define function to find or add a node; and adjust
its count
def addNode(name):
if name in nodemap:
node = nodemap[name]
node["count"] += 1
else:
node = {"nodeid": name, "count": 1}
nodemap[name] = node
return
An empty hashmap is initially defined (
nodemap = {}
). The
addNode
function is defined with one arguments passe in:
name
, which is the unique
node ID (that is, the author's name, or the name of the influencer).