The csv file contains a list of the adjacency of the graph in the form where the number of the vertex is in the first place, and all that goes after that in the line are the vertices that are associated with it
adjacency list

How to read data from a file into a dictionary, so that the key would be the number of the vertex, and the values ​​would be the vertices that are associated with it?

Closed due to the fact that off-topic participants Xander , Enikeyschik , freim , LFC , 0xdb Feb 23 at 16:56 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- freim, LFC, 0xdb
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • I forgot to say that each vertex is stored in a separate cell - THAT GUY
  • Sample CSV show? - Andrey
  • And what exactly is the question? Read line by line, cut lines on tabs, bring everything to int and lay out the dictionary. Though in a cycle, though through dict comprehension. What exactly caused the difficulty? - Xander

1 answer 1

Suppose the values ​​in CSV are separated by commas:

import csv with open('sample.csv') as f: graph = {line[0]:line[1:] for line in csv.reader(f)} print(graph) # {'1': ['2', '3', '4', '5', '6'], '2': ['4', '5', '3', '6', '2', '1', '6', '2'], '3': ['5', '6', '2', '5', '6', '2']}