-1

so i have been trying to get these two function to work, when i do them sepretly they work, but when i combine the two functions using the elif function, it only runs the 1st function and prints out position list, and an error says"the neighbour_list is not defined"

this is my code

 my_file=open("test_graph_1.txt","r")
 x=[]
 y=[]
 nodenumber=[]
 positionx=[]
 positiony=[]


for row in my_file:

    value=row[:-1]

    my_list=value.split(",")

    if len(my_list)==3:
        nodenumber.append(int(my_list[0]))

        positionx.append(int(my_list[1]))
        positiony.append(int(my_list[2]))

        nodenumber1 =[(nodenumber[a],positionx[a],positiony[a]) for a i range(len(nodenumber))]
        position_list=tuple(nodenumber1)




    elif len(my_list)==2:
        x.append(int(my_list[0]))
        y.append(int(my_list[1]))

        l1 = [(x[i] , y[i]) for i in range(len(x))]
        l2 = [(y[i] , x[i]) for i in range(len(x))]
        l1.extend(l2)
        neighbour_list=[[l[0] for l in l1 if l[1] == j] for j in range(len(x))]


 print("position_list",position_list)
 print("neigh",neighbour_list)

but when i print the code the position list comes put fine but the neighbour_list comes out like this :[[4, 1], [0, 4, 2], [1, 3], [2, 5, 4], [3, 0, 1], [3], []] the extra empty string, which isn't suppose to be there but before that all is fine

13python
  • 11
  • 3
  • so where is your functions ? – Mazdak Sep 28 '14 at 13:14
  • sorry i havent got a handle on the language yet, i mean my 2 different if loops to get a position_list and neighbour_list – 13python Sep 28 '14 at 13:16
  • 1
    `else my_list[2] == "":` should have raised a SyntaxError. Did you mean `elif ...`? (or just `else:`?) – unutbu Sep 28 '14 at 13:16
  • yea i tried it with elif as well but it didnt work so i tried else – 13python Sep 28 '14 at 13:19
  • thing thing is it run through the first if loop but dosent go through the elif loop – 13python Sep 28 '14 at 13:20
  • Let's pin down some terminology. The `if ... else ...` is a *statement*, not a function. The code inside the `if` block and the `else` block are [(in the docs) called `suites`](https://docs.python.org/3/reference/compound_stmts.html#compound-statements). We never speak of `if loops` or `elif loops`. – unutbu Sep 28 '14 at 13:22
  • ok thanks for clearing that up – 13python Sep 28 '14 at 13:23

1 Answers1

0

If, for each pass through the loop, my_list[2] != "" is True, then neighbour_list is never defined. Then

print("neigh",neighbour_list)

would raise a NameError: the neighbour_list is not defined.


Instead, define neighbour_list before entering the for-loop. You could also use

if len(my_list) == 3:
    ...
elif len(my_list) == 2:
    ...
else:
    ...

to handle the two types of lines you expect to receive.


N = 5
position_list = list()
neighbour_list = [list() for j in range(N)]

with open("test_graph_1.txt","r") as my_file:
    for row in my_file:
        try:
            my_list = map(int, row.split(','))
        except ValueError:
            # Choose how to handle malformed lines
            print('invalid line: {!r}'format(row))
            continue
        if len(my_list) == 3:
            nodenumber, positionX, positionY = my_list
            position_list.append(tuple([nodenumber,positionX,positionY]))
        elif len(my_list) == 2:
            nodenumber1, nodenumber2 = my_list
            neighbour_list[nodenumber1].append(nodenumber2)
            neighbour_list[nodenumber2].append(nodenumber1)            
        else:
            # Choose how to handle lines with more than 3 or less than 2 items
            continue

print(position_list)
print("neigh", neighbour_list)

You might also want to use a graph library like networkx or igraph.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • You have to assign a value to neighbour_list. You didn't say what your program's supposed to do, so we can't really help with that. – Aran-Fey Sep 28 '14 at 13:24
  • You could define a value for `neighbour_list` near the top, before the `for-loop`. You might set it to an empty list or `None`, depending on what you want the program to do when the `elif-suite` is never reached. – unutbu Sep 28 '14 at 13:26
  • In the txt file gives lists,about a graph with nodes. For a graph with N nodes and M links, the first N lines have the form nodeNumber, positionX, positionY The X and Y positions are integers. After this, the next M lines specify the links. They have the form nodeNumber1,nodeNumber2 – 13python Sep 28 '14 at 13:29
  • the first few rows have 3 columns which state the node number,xposition,y position, that why if using if my_list[2] != "": – 13python Sep 28 '14 at 13:32
  • the next few rows have 2 columns which state the connections of nodes, which node is connected to which node, the is used to figure out the neighboring nodes, so i used elif my_list[2] == "": how can i get both the if suites to run? or can i use something else – 13python Sep 28 '14 at 13:34