I have a text file that reads:
a;b
a;c
a;d
b;h
c;e
e;f
e;g
e;j
f;b
g;d
h;b
h;e
i;d
i;e
but when I print it after making it into a dictionary
def read_graph(file_name):
graph = {}
for line in open(file_name):
if ";" in line:
key, val = map(str.strip, line.split(";"))
graph[key] = val
return dict(sorted(graph.items())))
It prints:
{'a': 'b', 'b': 'd', 'c': 'e', 'd': 'g', 'e': 'd', 'f': 'd'}
how do I make it where it prints the keys that repeat?
I assume for this you'd want to use a list of strings instead of a single string as the value, otherwise your dictionary will keep replacing the value for the same key.
Instead of:
{'a': 'b'}
You would probably want a structure such as:
{'a': ['b','c','d']}
Using your function:
def read_graph(file_name):
graph = {}
for line in open(file_name):
if ";" not in line: continue
key, val = line.strip().split(';')
if key not in graph: graph[key] = list()
if val not in graph[key]: graph[key].append(val)
return dict(sorted(graph.items()))
read_graph('file.txt')
{'a': ['b', 'c', 'd'], 'c': ['e'], 'b': ['h'], 'e': ['f', 'g', 'j'], 'g': ['d'], 'f': ['b'], 'i': ['d', 'e'], 'h': ['b', 'e']}
Dictionaries in python (and every other language I know) have unique values for each key, and will overwrite them when you put a new value in for an existing key.
Consider a different kind of data structure, like a set of tuples, eg
{('a','b'), ('a','c'), ...}
Or, as it looks like you are making a graph, a dictionary where the values are lists of vertices instead of individual vertices, eg
{'a':['b','c'],...}
To make the set of tuples, replace the line
graph[key] = val
with
graph.append((key, val))
To make a dictionary-to-lists, use
if key in graph:
graph[key].append(val)
else:
graph[key] = [val]
Hope this helps!
You cannot because that is a dictionary, and it is not allowed to have two same keys or it would ambiguous. You could group by key.
def read_graph(file_name):
graph = {}
for line in open(file_name):
if ";" in line:
key, val = map(str.strip, line.split(";"))
if key not in graph:
graph[key] = [val]
else:
graph[key].append(val)
return dict(sorted(graph.items())))
So now you have for every key, an array with its values.
Since you seem to be working with a graph structure, I would recommend you look at the NetworkX package for Python. They have pre-built graph data-structures for you to use and many algorithms that can operate on them.
import networkx as nx
graph = nx.Graph()
with open(file_name) as f: # This closes the file automatically when you're done
for line in f:
if ";" in line:
source, dest = map(str.strip, line.split(";"))
graph.add_edge(source, dest)
In case you still want to use vanilla Python only:
Python's dictionaries can only have one value per key. To store multiple values for a single key, you have to store your keys in a list of values.
my_dict = {
'a': ['b', 'c', 'd'],
'b': ['h'],
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.