[Tutor] how to invert tuples

Alan Gauld alan.gauld at btinternet.com
Sun Nov 22 18:55:05 EST 2015


On 22/11/15 20:58, marcus lütolf wrote:
> dear pythonistas
>
> i have written the code below for identifying tuples occurring
 > twice or more times in the list of tuples called "flights".

First thing to point out is that flights is not a list of tuples, its a 
tuple of tuples. Lists must be enclosed in []. If you do that it may 
simplify things for you. Also you create a dictionary called lst which 
implies its a list. You should try to make your variable names relate to 
their logical purpose rather than their data structure. So maybe counts 
or similar would have been a good name?

These may seem like details but names are an important aspect
of communicating a programs intent and accuracy of data type
determines what can and can't be done with the data so it
is important to get them right.

> 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 ?

There are several ways to do this. But in your code you are never 
comparing the tuples so I'm not sure where you plan on using it?
Can you explain exactly how you plan on using that function?

> 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.

We'd need to see that code to know why, but more usefully you can just 
put the list inside parens/brackets to ensure they all get handled, like so:

flights = [(),(),(),().....,()]

OR

flights = { (),
             (),
             ...
             ()]

Or anything in between


> 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

You can replace the if/else lines by using the get()
method of a dictionary. For example, using get():

          lst[pair] = lst.get(pair,0) + 1

>>>> for key,val in lst.items():
>              if val > 1:
>>>> print key, val

The indentation on that last line looks off but
I suspect that may just be email issues

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list