-2

The file contains:

1 2
2 6
2 3
2 4
3 1
3 4
4 5
5 4
6 5
6 7
7 6
7 8
8 5
8 7

From that a dictionary has to be created as follows:

D = {1: [2], 2: [6,3,4], 3: [1,4], 4: [5], 5: [4], 6: [5,7], 7: [6,8], 8: [5,7]}

Where the first element from the line is the key. What is the most efficient way?

geckon
  • 8,316
  • 4
  • 35
  • 59
  • 3
    This looks a bit like a "do my assignment for me" question to me... – LondonRob Aug 10 '15 at 09:51
  • The naive ways are too slow. I want to optimize my program. – Nikolay Gospodinov Aug 10 '15 at 09:53
  • 1
    What naive ways? What ways have you tried? – Cyphase Aug 10 '15 at 09:55
  • This looks like it might be an adjacency list for a directed graph. If so, I would recommend looking at NetworkX https://networkx.github.io/ – RMcG Aug 10 '15 at 10:25
  • 2
    FWIW, `D` isn't a great choice of variable name here. It's conventional in Python to use lower case names for simple variables. Names beginning with an uppercase letter are used for class names. And all upper case names can be used for constants. See [PEP8](https://www.python.org/dev/peps/pep-0008) for further details. – PM 2Ring Aug 10 '15 at 10:35

3 Answers3

5

This should do what you want:

from __future__ import print_function

from collections import defaultdict

dict_from_file = defaultdict(list)

with open('yourfile.txt', 'r') as infile:
    for line in infile:
        key, value = [int(x) for x in line.split()]
        dict_from_file[key].append(value)

# The dictionary dict_from_file is in the format that you want
print(dict_from_file)
Cyphase
  • 11,502
  • 2
  • 31
  • 32
  • Good catch on the `int`'s, I'll fix that. I only used `D` because that's what the OP used, but you're right, and usually I _would_ pick a better variable name. It's late :). That said, there's only so much we can do when we don't know what the dict _is_ :P. – Cyphase Aug 10 '15 at 10:32
  • No need, it's a good point :). – Cyphase Aug 10 '15 at 10:34
  • [Referring to comments from @PM2Ring, who pointed out that the OP wanted `int` keys/values, and that D isn't a good variable name. There was no need to remove the comments :).] – Cyphase Aug 10 '15 at 10:38
  • 1
    Some comments are (relatively) permanent, and some are like Post-It notes. :) – PM 2Ring Aug 10 '15 at 10:40
2

You could do this without the collections module using setdefault method method

code:

Inp_file = open('data.txt')
result = {}
for line in Inp_file.readlines():
     key, value = line.strip('\n').split(' ')
     result.setdefault(int(key),[]).append(int(value))

print(result)

output:

{1: [2], 2: [6,3,4], 3: [1,4], 4: [5], 5: [4], 6: [5,7], 7: [6,8], 8: [5,7]}
Community
  • 1
  • 1
The6thSense
  • 8,103
  • 8
  • 31
  • 65
  • One little correction is: `result.setdefault(int(key),[]).append(int(value))` Key and values should be integers. – Nikolay Gospodinov Aug 10 '15 at 10:15
  • Sorry you are right changed @НиколайГосподинов – The6thSense Aug 10 '15 at 10:16
  • @НиколайГосподинов if any othe answer answered your question accept it by clicking the tick mark or comment on their answer that your answer is wrong to improve their answer – The6thSense Aug 10 '15 at 10:27
1

You can do it as follows:-

import collections

file = open('data.txt')
result = collections.defaultdict(list)
for line in file.readlines():
   key, value = line.strip('\n').split(' ')
   result[key].append(value)

print(result)
hspandher
  • 15,934
  • 2
  • 32
  • 45