Home > other >  Python dictionary stored in the undirected graph nodes
Python dictionary stored in the undirected graph nodes

Time:09-16

I want to traverse a text file stored in each side of the two nodes, then the value of each node as a key, and connected node as the value of it there is a list
Text file the following
1, 2,
1, 4
1, 6
1, 7
1, 8
1, 9
2, 3
3, 4
4, 5
5 or 6
7, 12
8, 14
10, 11
10, 12
10, 13
10, 14
14 or 17
15 and 16th
16 or 17
17, 26
18th and 19th
18,
18, 25
19, 20
20,
21, 22
22, 23
23, 24
24, 25
24, 29
25 p
27, 28
27, 29
27, 30
27, 32
27 behavior
29, 30
30, 31
30, 32
32 33
The code below
Nodes={' 1 ': []' 3 ', '2' : (' 3 ')} # gave dictionary initialized
='/home/hadoop/nodefile nodeInfo. TXT'
With the open (nodefile) as fb:
For the line in fb:
Listlink=line. Rstrip (). The split (', ')
For the keys and values in nodes. The items () :
If listlink [0]==keys:
Nodes (keys)=list (values). Append (listlink [1]) # meet key phase at the same time, the only store node into the list of values
The else:
Nodes. Setdefault (listlink [0], [listlink [1]]) # if not found the same key in a dictionary, as a new key value to store in,
Print (nodes)


When running the questions such as:
The File "/home/hadoop/pycharmproject/readnode py", line 12, the in & lt; module>
Nodes (keys)=list (values). Append (listlink [1])
TypeError: 'NoneType' object is not iterable
How to solve this

CodePudding user response:

List. The append () returns nonetype so would be an error after add the first assignment

CodePudding user response:

List. The append () returns nonetype so would be an error after add the first assignment
  • Related