473,394 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1480
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
35
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you...
7
by: Chris P. | last post by:
Hi. I've made a program that logs onto a telnet server, enters a command, and then creates a list of useful information out of the information that is dumped to the screen as a result of the...
0
by: S P Arif Sahari Wibowo | last post by:
Hi! I am looking for mailing list manager written in Perl that have automatic bounce management utilizing VERP or other customized delivery method. Do you know any (or a list of)? Or maybe...
6
by: jena | last post by:
hello, when i create list of lambdas: l=] then l() returns 'C', i think, it should be 'A' my workaround is to define helper class with __call__ method: class X: def __init__(self,s): self.s=s...
18
by: a | last post by:
can someone tell me how to use them thanks
4
by: Gregory Guthrie | last post by:
Sorry for a simple question- but I don't understand how to parse this use of a list comprehension. The "or" clauses are odd to me. It also seems like it is being overly clever (?) in using a...
10
by: Debajit Adhikary | last post by:
I have two lists: a = b = What I'd like to do is append all of the elements of b at the end of a, so that a looks like: a =
4
by: beginner | last post by:
Hi All, If I have a list comprehension: ab= c = "ABC" print c
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.