[Tutor] list comprehension question

Blake Winton bwinton at latte.ca
Tue Jun 1 16:05:45 EDT 2004


> I have two lists of keys that I have from creating edge 
> dictionaries for the altered and original mesh, then
> using list comprehension to find the unique edges.
> giving, for instance, the following.
> 
> ListAUniqueEdges = {(1,5), (2,6), (3,7), (8,9)}
> ListBUniqueEdges = {(2,7), (3,5), (1,6), (10,11)}

Just one question, you have those listed as dictionaries, but
I've found it easier to work with if I converted them to lists.
i.e.:
ListAUniqueEdges = [(1,5), (2,6), (3,7), (8,9)]
ListBUniqueEdges = [(2,7), (3,5), (1,6), (10,11)]

Was that a typo?

> I need to create two dictionaries from the above, showing
> how the edges from lista relate to those in listb and vice
> versa, ie the resulting Dict would be
> 
> DictA = {(1,5): {(1,6), (3,5)}, (2,6): {(2,7), (1,6)},
> (3,7): {(3,5), (2,7)}, (8,9):{}}
> 
> is there some elegant way to do this, I think it should
> be doable via list comprehension, but for some reason I
> can't think of the solution right now, and the power of
> google isn't helping...

I can do it with a list comprehension, but I'ld like to see
what you came up with first...

Later,
Blake.




More information about the Tutor mailing list