[Tutor] list comprehension question
Tom Musgrove
tommusgrove__ at hotmail.com
Tue Jun 1 18:43:45 EDT 2004
>Was that a typo?
Hi Blake, it was indeed a typo, new to the whole python thing...
Here is the solution I came up with
>>>ListAUniqueEdges = [(1,5), (2,6), (3,7), (8,9)]
>>>ListBUniqueEdges = [(2,7), (3,5), (1,6), (10,11)]
>>>NewDict = {}
>>>for k,v in ListAUniqueEdges:
>>> listTemp = []
>>> for m,n in ListBUniqueEdges:
>>> if((k==m)or(k==n)or(v==m)or(v==n)): #seems there should be a better
>>>method here
>>> if (listTemp == []) :
>>> listTemp = [(m,n)]
>>> else:
>>> listTemp.extend([(m,n)])
>>> NewDict[(k,v)] = listTemp
>>>
>>>NewDict
{(3,7): [(2,7), (3,5)], (2,6): [(2,7), (1,6)], (8,9): [], (1,5):[(3,5),
(1,6)]}
>>>
So, it seems to work ok
I'd be interested if there is a 'better' or more elegant way. Especially
the
if((k==m)or(k==n)or(v==m)or(v==n))
(I tried (k or v) == (m or n) but didn't give the result I'd hoped for...)
>I can do it with a list comprehension, but I'ld like to see
>what you came up with first...
>
>Later,
>Blake.
>
_________________________________________________________________
MSN Toolbar provides one-click access to Hotmail from any Web page FREE
download! http://toolbar.msn.click-url.com/go/onm00200413ave/direct/01/
More information about the Tutor
mailing list