Quantcast
Channel: How can I partition (split up, divide) a list based on a condition? - Stack Overflow
Viewing all articles
Browse latest Browse all 41

Answer by RichieHindle for How can I partition (split up, divide) a list based on a condition?

$
0
0

First go (pre-OP-edit): Use sets:

mylist = [1,2,3,4,5,6,7]goodvals = [1,3,7,8,9]myset = set(mylist)goodset = set(goodvals)print list(myset.intersection(goodset))  # [1, 3, 7]print list(myset.difference(goodset))    # [2, 4, 5, 6]

That's good for both readability (IMHO) and performance.

Second go (post-OP-edit):

Create your list of good extensions as a set:

IMAGE_TYPES = set(['.jpg','.jpeg','.gif','.bmp','.png'])

and that will increase performance. Otherwise, what you have looks fine to me.


Viewing all articles
Browse latest Browse all 41

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>