[Tutor] how to invert tuples

marcus lütolf marcus.luetolf at bluewin.ch
Sun Nov 22 15:58:35 EST 2015


dear pythonistas

i have written the code below for identifying tuples occurring twice or more times in the list of tuples called "flights".
1. Question: How can i modify this code so it does not matter which string is first and which is second  or (h,i) == (i,h) respectively ?
2. Question: On my python file I have written the rather long list of tuples in 3 lines. If I break them down in multiple shorter lines 
the for loop will not work through the last tuple.
Thanks in advance for help, Marcus.

>>> lst = dict()
>>> a = 'M1'
>>> b = 'M2'
>>> c = 'M3'
>>> d = 'M4'
>>> e = 'M5'
>>> f = 'M6'
>>> g = 'M7'
>>> h = 'M8'
>>> i = 'M9'
>>> j = 'M10'
>>> k = 'M11'
>>> l = 'M12'
>>> flights =  (h,i), (h,j),(k,f), (k,a), (f,a), (e,f), (e,g), (d,g), (l,c),(l,b), (c,b),(j,c), (j,k), (c,k), (h,f), (h,d), (f,d), (a,l), (a,g),(e,i), (e,b,), (i,b), (i,l), (i,k), (l,k), (f,j),(f,b),
       (j,b),(h,a),(h,g),(a,g),
      (d,e), (d,c), (e,c), (i,c), (i,d), (c,d), (f,e,),(f,i),(e,i), (a,j,), (a,b), (j,b), (h,l), (h,k), (l,k), (l,f),
      (l,g),(f,g),(b,k), (b,d), (k,d), (i,a), (i,j), (a,j), (h,c), (h,e), (c,e), (h,j)
>>> for pair in flights:
           if pair not in lst:
               lst[pair] = 1
        else:
            lst [pair] += 1
>>> for key,val in lst.items():
            if val > 1:
>>> print key, val



---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus



More information about the Tutor mailing list