1

I have a text file of the looks like this:

0 1
0 2
0 3
2 3
3 4
4 1
.. ..

I'd like to make it a dictionary looking like this

graph = { "0" : ["1", "2", "3"],
      "1" : ["4", "0"],
      "2" : ["0", "1", "3"],
      "3" : ["0", "2", "4"],
      "4" : ["1", "3"]
    }

the file text list is a list of edges for a graph. I need to make a graph out of it without using any package. My final aim is to calculate the diameter and clustering coefficient. However, before starting I need to create the graph.

My attempt so far was:

d = {}
    with open("/Volumes/City_University/data_mining/Ecoli.txt") as f:
        for line in f:
           (key, val) = line.split()
           d[int(key)] = val
    for x in d:
    print (x)

Outcome:

471
472
474
475
476
477
478
479
480
481
483
484
485
486
487

Thanks

4 Answers 4

1

As one other possible option, you can also use defaultdict here:

from collections import defaultdict
d = defaultdict(list)
with open("/Volumes/City_University/data_mining/Ecoli.txt") as f:
    for line in f:
        key, val = line.split()
        d[key].append(val)
for k, v in d.items():
    print(k, v)

This saves you from having to check whether a key is already in d or not, and it also saves you a couple of lines.

Sign up to request clarification or add additional context in comments.

2 Comments

this works better, I found also the 0 key in the result
Great. I'm glad you found it.
1
d = {}
with open("/Volumes/City_University/data_mining/Ecoli.txt") as f:
    for line in f:
       (key, val) = line.split()
       if key in d:
           d[key].append(val)
       else:
           d[key] = [val]
for x, v in d.items():
print x, v

Explanation:

Just make the values of d lists, and append to the lists.

3 Comments

awesome, thank you very much it seems it works. The only issue is that I can't find the key 0... How can I extract the print outcome to a txt file so that I can look better and see if the 0 key is there as well? Thanks again.
Please see this question.
@Andrea Sportelli You don't need to print to a file to check if a key is there. Simply add one more line to your code: print d.get('0').
0

try this:

d = {}
with open("/Volumes/City_University/data_mining/Ecoli.txt") as f:
    for line in f:
       (key, val) = line.split()
       if key in d:
           d[key].append(val)
       else:
           d[key] = [val]
for x in d:
    print x,d[x]

if key is found in dictionary it will append the value else create a new pair

Comments

0
import numpy as np
file_name='text_file.txt'
key_column=0

dat=np.genfromtxt(file_name,dtype=str)
d={i:[] for i in np.unique(dat[:,key_column])}

for row in dat:
    for key in d.keys():
        if row[key_column]==key :d[key].append(row[1])

print d

1 Comment

this might me good for when number of keys is too large, do two passes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.