Perl Christmas Quiz

Paul Makepeace paulm at paulm.com
Tue Dec 16 14:54:10 GMT 2008


On Tue, Dec 16, 2008 at 2:12 PM, Avleen Vig <avleen at gmail.com> wrote:
> On Tue, Dec 16, 2008 at 11:35 AM, Paul Makepeace <paulm at paulm.com> wrote:
>>> 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.)
>
> I'm probably misunderstanding 'truly flat' but like this?
> [''.join(i) * min(l1.count(i), l2.count(i)) for i in
> set(l1).intersection(set(l2))]]

This concatenates the repeated elements into e.g. a single string. Try
adding some extra fields in your test data and you'll end up with
something like,

['q', 'pp', 'n']

rather than

['q', 'p', 'p', 'n']

Slightly oddly, IMO, python doesn't have a 'flatten' function.

P


More information about the london.pm mailing list