I have this GraphML file that I've read into Networkx.
So I access all the nodes by:
g.nodes()
It gives me a list of strings. Say one of them is "123".
I then try to access a node as:
g["123"]
and it gives me a dictionary.
I then try to access the nodes using the nodes function as follows:
for n in g.nodes( data = True ):
print n
It then gives me a 2-tuple with the string node name as first element and a dictionary as a second element.
The thing is, it is a different dictionary from the first one. And it's confusing the heck out of me, so any help here is appreciated.
Are they supposed to be different? If so, why? If not, then what am I doing wrong? :)
I can post the actual data if it would help.
Have you considered reading the various pages of documentation?
nlist : list
A list of nodes. If data=True a list of two-tuples containing (node, node data dictionary).
and...
adj_dict : dictionary
The adjacency dictionary for nodes connected to n.
"Node data dictionary" and "adjacendy dictionary" are not the same thing.
Related
I am currently working with road networks and followed this post that explained exactly what I was looking for.
I, however, am facing a problem. I tried running the script on a small part of my domain and came across the error shown bellow.
NetworkXError: Edge tuple (35,) must be a 2-tuple or 3-tuple.
After checking every variable I was working with, I found out that the problems is from the l = [set(x) for x in G.edges()]
I basically have a list of dictionaries that contain two set of coordinates (which represent the start and the end points of each roads of my domain) but some of the roads only have one set of coordinates in the dictionary.
Here's a simplified version of what I get when I print(l):
[{(2.455183, 48.7774425), (2.4551873, 48.7776523)},
{(2.4574735, 48.7736999), (2.4577528, 48.7738954)},
{(2.4574735, 48.7736999)},
{(2.4577528, 48.7738954), (2.4578287, 48.7723847)},
{(2.4585674, 48.7823935), (2.4586793, 48.7825114)}]
What I would like to know is if there is a way to select the dictionaries that only have one set of coordinates and either duplicate it inside the dictionary (so instead of having {(2.4574735, 48.7736999)}, I would have {(2.4574735, 48.7736999), (2.4574735, 48.7736999)}) or delete them if the previous proposition is not doable.
I found several ways of selecting dictionaries in a list based on their key names but I don't have any keys in mine so nothing I tried worked so far.
Any help would be greatly appreciated.
These are not dictionaries. These are sets. And your first requirement is not possible, as sets can not contain duplicate entries. So you can just check the lengths of the sets, and skip the ones that are less than length 2. So something like this:
l = [set(x) for x in G.edges() if len(set(x))>1]
I looking for a elegant way to find all nodes with a defined attribute. E.g. let's say I create a new network with two nodes
G.add_node('A', attr1='alpha')
G.add_node('B', attr1='beta')
Now, I would like to have a function that returns all nodes where the attribute "attr1" that matches "beta" something like
THX
Lazloo
Try
L = [node for node in G.nodes() if G.node[node]['attr1']=='beta']
to create a list (look at list comprehensions). You can also create other data types that contain all of these nodes.
I wanted to learn about Data Structures so I decided to create them using Python. I first created a Singly Linked List (which consists of two classes: the actual List and the Node). A List consists of Nodes (or can be empty). Each node had a "next" value. When I instantiated a list, it would look like this:
l = LinkedList([1,2])
and this was the sudocode for the init:
def __init__(self, item=None):
head = None
if a single item was given
head = Node(item)
head.next = None
else if multiple items were given
for i in item
if head: # i is not the first item in the list
new_head = Node(i)
new_head.next = head
head = new_head
else: # i is the first item in the list
head = Node(i)
head.next = None
Maybe there is a flaw in the logic above, but hopefully you get how it works more or less. The key thing I noted here was that I did not use any list ([]) or array ({}) because I didn't need to.
Now, I am trying to create a MultiSet but I am stuck in the init part.
It was simple for a Linked Lists because when I read articles on Linked Lists, all of the articles immediately mentioned a List class and a Node class (a List consists of a Node. a List has a head and a tail. a Node has a 'next' value). But when I read articles on MultiSets, they just mention that multisets are sets (or bags) of data where multiple instances of the same data are allowed.
This is my init for my multiset so far:
def __init__(self, items=None):
self.multiset = []
if items: # if items is not None
try:
for i in items: # iterate through multiple items (assuming items is a list of items)
self.multiset.append(i)
except: # items is only one item
self.multiset.append(items)
I don't think I am on the right track though because I'm using a Python list ([]) rather than implementing my own (like how I did with a Linked List using Nodes, list.head, list.tail and node.next).
Can someone point me in the right direction as to how I would create and instantiate my own MultiSet using Python (without using existing Python lists / arrays)? Or am I already on the right track and am I supposed to be using Python lists / arrays when I am creating my own MultiSet data structure?
It looks like you're conflating two things:
data structures - using Python (or any other language, basically), you can implement linked lists, balanced trees, hash tables, etc.
mapping semantics - any container, but an associative container in particular, has a protocol: what does it do when you insert a key that's already in it? does it have an operation to access all items with a given key? etc.
So, given your linked list, you can certainly implement a multiset (albeit with not that great performance), because it's mainly a question of your decision. One possible way would be:
Upon an insert, append a new node with the key
For a find, iterate through the nodes; return the first key you find, or None if there aren't any
For a find_all, iterate through the nodes, and return a list (or your own linked-list, for that matter), of all the keys matching it.
Similarly, a linked-list, by itself, doesn't dictate if you have to use it as a set or a dictionary (or anything else). These are orthogonal decisions.
What is the easiest/best way to get the key-value stores from my dictionary into the node properties in neo4jrestclient?
I have a dictionary
nodeDict = {"color":"Red", "width":16, "height":32}
How do I insert the dictionary into the property list during node creation?
from the neo4jrestclient doc page, the way to create a node with properties is
n = gdb.nodes.create(color="Red", widht=16, height=32)
I can iterate through the dictionary, generating a JSON request for each key-value store, but that seems wrong.
Creating the node and assigning properties should be done with a single JSON request, right?
I could convert the dictionary to a string, strip off the curly braces, and change the colons into equal signs, but that also seems wrong.
Thanks!
I don't know neo4j API; but given what you describe, argument unpacking should do the job:
n = gdb.nodes.create(**nodeDict)
Also, I am not sure Cypher is supported yet in Neo4jRestClient, but with that you could do like http://docs.neo4j.org/chunked/snapshot/query-create.html#create-create-single-node-from-map ,
create n = {props}
and pass in the props as a parameter.
HTH
In networkx
gr.nodes(data=True)
returns the list of nodes along with the attributes present on each node. Is it possible to get all the neighbors along with attributes of a given node in the graph.
Neighbors function doesn't accept any optional boolean parameter.
Use the adjacency_iter if you want to iterate over them or adjacency_list if you want them as a list.
Edit
I should add that neither of those will give you the attributes of node. But you can get them easily with g.node[n].