1

I am trying to fill a dictionary while using loops, problem is the loop stops after fulfilling a condition without checking if there are more available conditions to fulfill,

sec=[
     [1, 2, 4.0, 100.0], 
     [2, 3, 1.5, 500.0], 
     [2, 4, 10.0, 700.0], 
     [2, 5, 5.75, 1500.0], 
     [3, 4, 11.4, 200.0], 
     [4, 5, 10.5, 750.0], 
     [4, 6, 6.75, 550.0]]

I made this list and there's also this dictionary

graph={1: [1], 2: [2], 3: [3], 4: [4], 5: [5], 6: [6]} what I am trying to accomplish is

graph = {1: ['2'],
         2: ['3', '4','5'],
         3: ['4'],
         4: ['5','6'],
         5: [],
         6: []}

how it should work is that it collects all sec[x][0] as the key of the dictionary and the sec[n][1] as the values in the dictionaries if sec[x][0] has value 1 and sec[x][1] has the value 2 then number 2 is added to the dictionary as value for key 1 the code I got is this

def magic(numList): #turns string into int
   s = ''.join(map(str, numList))
   return int(s)
for i in range(1,len(loc)+1): #len of loc is 6
    for n in range(0,len(loc)+1):
        if magic(graph[i])==sec[n][0]:
            graph[i].append(sec[n][1])

but it will only add the first value then index n will stop counting and then index i keeps going and so it will not check for more values in keys

2
  • 1
    Hard to tell why the code is not working without knowing what the aptly named magic function is doing. Also, are those supposed to be lists of strings? Commented Dec 31, 2016 at 15:27
  • sorry forgot to mention that, def magic(numList): #turns string into int s = ''.join(map(str, numList)) return int(s) basically turns the string into an int value Commented Dec 31, 2016 at 15:28

2 Answers 2

4

Your initial definition of graph isn't helpful for the intended result. Initialize the values with empty lists then append in a simple loop:

>>> sec=[
     [1, 2, 4.0, 100.0], 
     [2, 3, 1.5, 500.0], 
     [2, 4, 10.0, 700.0], 
     [2, 5, 5.75, 1500.0], 
     [3, 4, 11.4, 200.0], 
     [4, 5, 10.5, 750.0], 
     [4, 6, 6.75, 550.0]]
>>> graph = {i:[] for i in range(1,7)}
>>> for x,y,*z in sec: graph[x].append(str(y))

>>> graph
{1: ['2'], 2: ['3', '4', '5'], 3: ['4'], 4: ['5', '6'], 5: [], 6: []}
Sign up to request clarification or add additional context in comments.

4 Comments

It might be worth mentioning defaultdict(list)
@roganjosh Maybe, but then you won't have those [].
Note: *z only works in Python 3.x; for older versions, use for x,y,_,_
Thank you @tobias_k I was wondering what the *z meant as a Python 2 user
0

Use a defaultdict from the collections module. See below:

from collections import defaultdict

graph_dd = defaultdict(list)

for s in sec:
    from_, to_ = s[:2]
    graph_dd[from_].append(to_)
    # do-nothing reference to to_ creates empty list, so that all nodes are represented
    # in graph_dd; else 5 and 6 would not be created
    graph_dd[to_]

# convert to regular dict (not really necessary, but OP wanted a dict)
graph = dict(graph_dd.items())

for k,v in graph.items():
    print(k,':', v)

prints:

1 : [2]
2 : [3, 4, 5]
3 : [4]
4 : [5, 6]
5 : []
6 : []

Comments

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.