1

I'm trying to put together a programming project at my university, but I'm stuck. The "math" part I've got covered, but I need help with lists and loops in Python.

I'm working with graphs, let me show one example, to make it clearer: 3 node graph

As you can see, I have 3 nodes in it and 2 edges, nothing fancy. I need to compute the closest route between each pair of nodes, and I have that covered already. Now I need to put it either in n lists, n elements each, or in a n x n, so either:

a = [0, 1, 2]
b = [1, 0, 1]
c = [2, 1, 0]

or a table (or matrix?) like this: table 3x3, where I only really need the part with the white background.

When I read data to work on, I put each node into a list, as a new element, so with these 3 steps I gradually get:

lw = []
lw = ['a']
lw = ['a','b']
lw = ['a','b','c']

Is there any way, to create empty lists out of elemnts of lw? I would really like to name them like dis_a, dis_b, dis_c etc. I tried to do it with a dictionary, but then I couldn't manipulate with those lists, like I usually do. And truth be told, I'd much prefer the solution using lists, as I already have a latter part of the program written for this.

EDIT: Ok, so one of you asked for input/output to make my question clearer. Mmy input is:

lw = ['a','b','c']

my desired output is:

a = []
b = []
c = []

or something like that (lists that can be easly identified with the nodes, that I have listed in lw)

EDIT2:

Ok, so now I have a number of lists created like this (still sticking to the example):

dis['a']
dis['b']
dis['c']

I have a working command

path[X][Y]

which on input takes the names of the nodes (as in lw list ex. 'a'), and on input returns a list of

nodes on the shortest path from X to Y. What I need to be doing now, is to take the length of it with

len(path[X][Y])

And substract 1 from it (it's counting the "starting" point as well, so it's correcting it). Then I have

to put this number in a corresponding place in a list. I would like to do it in a loop, so it would

automatically append numbers to existing lists, so I would automatically get from

dis['a'] = []
dis['b'] = []
dis['c'] = []

to

dis['a'] = [0, 1, 2]
dis['b'] = [1, 0, 1]
dis['c'] = [2, 1, 0]

Don't worry, about it calculating the path twice (ex. from a to b and then from b to a), It doesn't have to be perfect ;) I tried to create such method, but I have no idea, how to store the results in said lists (or if I'm even correct). Here is my proposition:

def lo():   
    for i in range(0, len(lw)):
        for j in range (0, len(lw)):
        dis[i].append(path[lw[i]][lw[j]])
rmunn
  • 34,942
  • 10
  • 74
  • 105
kjubus
  • 443
  • 3
  • 8
  • 21
  • 1
    Can you please add an example of the desired output to make the question clearer? – iled Dec 12 '15 at 13:50
  • "Is there any way, to create empty lists out of elemnts of lw?" Could you also clarify that statement ? You can create empty lists out of anything (or out of nothing for that matter) so this doesn't really make sense to me. – ereOn Dec 12 '15 at 13:54
  • @kjubus Thanks for that. ComputerFellow's answer already gives you a way to achieve your desired output. – iled Dec 12 '15 at 14:11

2 Answers2

2

What couldn't you manipulate with a dictionary?

dis_a, dis_b, dis_c... could as well be dis['a'], dis['b'] and dis['c']...

You could do:

dis = {}
for elem in lw:
    dis[elem] = []
    # ...

Want to loop over all dis-es now?

for elem, val in dis.iteritems():
    print elem    # your a, b, or c
    print val     # your [] corresponding to a, b, or c

If you want to add elements, and find min and max (as requested in the comments):

Let's say initially dis['a'] gets [0, 1, 2]

and ...

dis['b'] = [1, 0, 1]
dis['c'] = [2, 1, 0]

and now you want to add a new item to all of the dis-es...

item = 5
for elem in dis:
    dis[elem].append(item)

and now you want to find the max ...

for elem in dis:
    print max(dis[elem])
ComputerFellow
  • 11,710
  • 12
  • 50
  • 61
  • I'm a newbie in python (and not much of a programmer either, rather a beginner, I'd say), but when I Do it like you show in the first part, I can print all the created ***dis*** lists, and get a result: print(dis) {'a': [], 'c': [], 'b': []} but how do I get to add values to them, find max/min etc? – kjubus Dec 12 '15 at 13:59
  • @kjubus - I suggest reading through [the Python tutorial](https://docs.python.org/3/tutorial). (If you're using Python 2, then use [the Python 2 tutorial](https://docs.python.org/2/tutorial) instead). But briefly, to access the lists in your dictionary, you can do `dis['a']`, `dis['b']`, etc. To append the number 1 to list `a`, you can do `dis['a'].append(1)`. I hope that and the tutorial links are enough to get you started. – rmunn Dec 12 '15 at 14:05
  • Note: to find out more about dictionaries, [the dictionary section of the tutorial](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) will help you the most. (As with my other comment, that's a link to the Python 3 tutorial. [If you're using Python 2, use this link instead](https://docs.python.org/2/tutorial/datastructures.html#dictionaries).) – rmunn Dec 12 '15 at 14:08
  • @computerfellow thank you! I think that it does finally explain and solves my problem to me :) – kjubus Dec 12 '15 at 14:20
  • `d = {k:[] for k in lw}` – Padraic Cunningham Dec 12 '15 at 14:21
  • @ComputerFellow accepted. Now I have a diffrent problem. Please, see an EDIT2 in my original post. – kjubus Dec 12 '15 at 15:19
-1

If I'm correct you want to dynamically create variables. Here is short answer, for more information checkout this post How can you dynamically create variables via a while loop? Actually there you can find different approaches like using a dict.

>>> lw = ['a', 'b', 'c']
>>> for l in lw:
        exec("{prefix}{var}=[]".format(prefix="dis_", var=l)) 

>>> dis_a
        []

>>> dis_b.append('some')
>>> dis_b
        ['some']
Community
  • 1
  • 1
sanddog
  • 91
  • 1
  • 4
  • This is a bad idea. Using `exec` has all kinds of security problems, and should only **ever** be done by someone who really knows what they're doing. The OP has said that he's a Python beginner, so he should *not* be using `exec`. – rmunn Dec 12 '15 at 16:56
  • 1
    @rmunn yep, you are definitely right. – sanddog Dec 12 '15 at 17:29