Perl Christmas Quiz

Paul Makepeace paulm at
Tue Dec 16 11:35:51 GMT 2008

On Mon, Dec 15, 2008 at 1:29 PM, Nicholas Clark <nick at> wrote:
> On Mon, Dec 15, 2008 at 01:22:49PM +0000, Avleen Vig wrote:
>> On Dec 15, 2008, at 10:04, "James Laver" <james.laver at> wrote:
>> >On Sun, Dec 14, 2008 at 3:15 PM, Avleen Vig <avleen at> wrote:
>> >>In the spirit of sharing, I offer this solution, from your neighbours
>> >>in the Python community:
>> >>
>> >>a = ['m', 'n', 'o', 'o', 'p', 'p', 'q']
>> >>b = ['n', 'p', 'q', 'r', 'r', 's']
>> >>
>> >>def FindSetMatches(list1, list2):
>> >>for i in set(list1).intersection(set(list2)):
>> >>  print '%s min(%s, %s)' % (i, list1.count(i), list2.count(i))
>> Plus I just wanted to be a snob with my four-line solution.
> Yes, but can Python do it in one line?

[(i,) * min(l1.count(i), l2.count(i)) for i in set(l1).intersection(set(l2))]

That produces an array of tuples of the repeated elements,

[('q',), ('p', 'p'), ('n',)]

A truly flat list is left as exercise (I battle with a reduce+lambda
hack but didn't quite win.)


> (Sort of a serious question. Not knowing Python, but having this understanding
> that all constructions that C (etc) and Perl (etc) would delimit with {},
> Python does with whitespace indentation level, I'm figuring that some things
> in Python *have* to be done in multiple lines. For maintaining real world
> code, this is probably actually a feature, rather than a "feature")
> Nicholas Clark

More information about the mailing list