masber
masber

Reputation: 3067

python 3 comprehension dictionary

This is my code:

def brujinGraph(k, strList):

    vertex = [[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0]]

    brujinGraph = {strList[i]: strList[j][:-1] for i in range(len(vertex)) for j in range(k) and vertex[i][j] == 1}

    return brujinGraph

strList = ['AAGA', 'AAGA', 'AGAT', 'ATTC', 'CTAA', 'CTCT', 'GATT', 'TAAG', 'TCTA', 'TCTC', 'TTCT']
brujinGraph(4, strList)

and it is throwing me an UnboundLocalError: local variable 'j' referenced before assignment

any idea what does it means and why am I getting this error?

Upvotes: 0

Views: 32

Answers (2)

dawg
dawg

Reputation: 103844

Couple of issues:

  1. You need an if not an and at the end

  2. I think it is better expressed this way:

    brujinGraph = {strList[i]: strList[j][:-1] for i, x in enumerate(vertex) for j, e in enumerate(x) if e == 1}

Upvotes: 1

Tony Suffolk 66
Tony Suffolk 66

Reputation: 9704

Without knowing exactly what vertex and strList are :

Do you actually mean :

{strList[i]: strList[j][:-1] for i in range(len(vertex)) for j in range(len(vertex[i])) if vertex[i][j] == 1}

i.e. change that and into an if

Upvotes: 1

Related Questions