470,826 Members | 2,020 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,826 developers. It's quick & easy.

Can a List Comprehension do ____ ?

Pythonistas,

I seem at a loss for a List Comprehension syntax that will do what I want.

I have a list of string position spans:
breaks [(133, 137), (181, 185), (227, 231), (232, 236), (278, 282), (283, 287), (352, 356), (412, 416), (485, 489), (490, 494)]

the first pair representing: someString[133:137]

What I want is a list of the positions between these spans, which would look like this:
spans [[(137, 181)], [(185, 227)], [(231, 232)], [(236, 278)], [(282, 283)], [(287, 352)], [(356, 412)], [(416, 485)], [(489, 490)]]
Of course I can get such a list, but I haven't been able to figure out how to do this with a List Comprehension. I am wondering if there is a syntax which makes pairs of the maximum of one list element and the minimum of thenext list element.

Can anyone show me the List Comprehensions light?

Was trying things like this:
listSpans=[(max(x,y),min(a,b)) for (x,y) in breaks for (a,b) in breaks.pop()]



With regards

Eric Pederson
http://www.songzilla.blogspot.com
:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
e-mail me at:
do@something.com
except, increment the "d" and "o" by one letter
and spell something with a "z"
:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
Jul 18 '05 #1
6 1368
Eric @ Zomething wrote:
breaks [(133, 137), (181, 185), (227, 231), (232, 236), (278, 282), (283, 287),
[(352, 356), (412, 416), (485, 489), (490, 494)]

the first pair representing: someString[133:137]

What I want is a list of the positions between these spans, which would
look like this:
spans [[(137, 181)], [(185, 227)], [(231, 232)], [(236, 278)], [(282, 283)],
[[[(287, 352)], [(356, 412)], [(416, 485)], [(489, 490)]]

breaks = [(133, 137), (181, 185), (227, 231), (232, 236), (278, 282), (283, 287), (352, 356), (412, 416), (485, 489), (490, 494)] it = iter(breaks)
[(start, end) for ((_, start), (end, _)) in zip(it, it)] [(137, 181), (231, 232), (282, 283), (356, 412), (489, 490)]


Peter

Jul 18 '05 #2
I want to use a list of glob patterns to create a single flat list of files

eg

P = ['*.txt','*.py']

I expected I would somehow be able to use list comprehensions to do
this, but in the end I could only do this hackish thing

import operator, glob
F = reduce(operator.add,[glob.glob(p) for p in P],[])
is there a more pythonic approach?
--
Robin Becker
Jul 18 '05 #3
Robin Becker <ro***@reportlab.com> wrote in
news:40**************@jessikat.fsnet.co.uk:
I want to use a list of glob patterns to create a single flat list of
files

eg

P = ['*.txt','*.py']

I expected I would somehow be able to use list comprehensions to do
this, but in the end I could only do this hackish thing

import operator, glob
F = reduce(operator.add,[glob.glob(p) for p in P],[])
is there a more pythonic approach?


If you want a list comprehension then how about:

filenames = [ name for pat in PATTERNS for name in glob.glob(pat) ]
Jul 18 '05 #4
Duncan Booth wrote:
If you want a list comprehension then how about:

filenames = [ name for pat in PATTERNS for name in glob.glob(pat) ]

PATTERNS = ["*t", "*xt"]
filenames = [name for pat in PATTERNS for name in glob.glob(pat)]
len(filenames), len(sets.Set(filenames))

(66, 39)

Names that match more than one pattern will appear more than once in the
list.

Peter

Jul 18 '05 #5
Robin Becker wrote:
I want to use a list of glob patterns to create a single flat list of
files

eg

P = ['*.txt','*.py']

I expected I would somehow be able to use list comprehensions to do
this, but in the end I could only do this hackish thing

import operator, glob
F = reduce(operator.add,[glob.glob(p) for p in P],[])
is there a more pythonic approach?


Here's how I would do it in 2.4:

patterns = ["*.txt", "*.py"]

def matchAny(name, patterns):
return True in (fnmatch.fnmatch(name, p) for p in patterns)

matches = [fn for fn in os.listdir(".") if matchAny(fn, patterns)]

Note that the result is slightly different: files starting with a dot are
not filtered out.

Peter
Jul 18 '05 #6
Peter Otten <__*******@web.de> wrote in news:cd*************@news.t-
online.com:
Duncan Booth wrote:
If you want a list comprehension then how about:

filenames = [ name for pat in PATTERNS for name in glob.glob(pat) ]

PATTERNS = ["*t", "*xt"]
filenames = [name for pat in PATTERNS for name in glob.glob(pat)]
len(filenames), len(sets.Set(filenames))

(66, 39)

Names that match more than one pattern will appear more than once in the
list.

As indeed they do in the original posting. I assumed that either Robin
wanted that behaviour or knew it wasn't relevant (the example given had
non-overlapping patterns).

To remove duplicates just pass through a dictionary or a set:

filenames = dict.fromkeys(filenames).keys()
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Fuzzyman | last post: by
35 posts views Thread by Moosebumps | last post: by
reply views Thread by S P Arif Sahari Wibowo | last post: by
6 posts views Thread by jena | last post: by
18 posts views Thread by a | last post: by
4 posts views Thread by Gregory Guthrie | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.