List Comprehensions Enhancement

Magnus L. Hetland mlh at idt.ntnu.no
Sat Sep 11 13:49:34 EDT 1999


Greg Ewing <greg at cosc.canterbury.ac.nz> writes:

> I have implemented some of the list comprehension syntax
> ideas that were discussed a while ago.
> 
> Some examples:
> 
> nums = [1, 2, 3, 4, 5]
> strs = ["Apple", "Banana", "Coconut"]
> 
> print [3 * x for x in nums]
> print [x for x in nums if x > 2]
> print [(i, s) for i in nums for s in strs]
> print [(i, s) for i in nums for s in [f for f in strs if "n" in f]]
> 
> For more details and a patch, see:
> 

Cool!

Let's see...

def quicksort(list):         # No duplicates allowed
    if len(list) == 1:
        return list
    else:
        return quicksort([x for x in list if x < list[0]])
             + list[0]
             + quicksort([x for x in list if x > list[0]])

Oh, well...

--

  Magnus              Making no sound / Yet smouldering with passion
  Lie          The firefly is still sadder / Than the moaning insect
  Hetland                                       : Minamoto Shigeyuki




More information about the Python-list mailing list