[Tutor] Arbitrary-argument set function

Alan Gauld alan.gauld at btinternet.com
Wed Oct 2 02:17:24 CEST 2013


On 01/10/13 23:43, Spyros Charonis wrote:

> I am trying to extract from a set of about 20 sequences, the characters
> which are unique to each sequence. For simplicity, imagine I have only 3
> "sequences" (words in this example) such as:
>
>
> s1='spam'; s2='scam', s3='slam'
>
>
> I would like the character that is unique to each sequence, i.e. I need
> my function to return the list [ 'p', 'c', ',l' ].

Do you need them in that order or are you only looking for a list of 
characters that appear in a single sequence? Also what happens if a 
sequence has more than one unique character?

If its only unique letters you want I'd do it this way (which
may not be optimal!)

loop over each string and from it create a dict of the individual 
letters with the current string as the value.

At the end any dict entries with one string in the value list is
unique to that string so in your example the dict would look like:

{ s: ['spam','scam','slam'],
   p: ['spam'],
   a: ['spam','scam','slam'],
   m: ['spam','scam','slam'],
   c: ['scam'],
   l: ['slam']
}

If your real world solution scales to many 'words' then you could hold a 
counter as value instead and simply increment it instead of adding the 
string. But that prevents you mapping words to unique letters.

Just a thought.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list