473,583 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.co m
except, increment the "d" and "o" by one letter
and spell something with a "z"
::::::::::::::: ::::::::::::::: ::::::::::::::: ::::::::::::::: ::::::::
Jul 18 '05 #1
6 1490
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***@reportla b.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(fi lenames))

(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.fnmatc h(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(fi lenames))

(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(f ilenames).keys( )
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
40613
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* just created a new pointer to the original value.... For example I wanted to initialize a list of empty lists.... a=, , , , ] I thought there...
35
2952
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 could do: result = for element in list: if element == 'blah':
7
2262
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 command. Here's a generic version of the code in question: ##### # Prior code opens telnet connection "tn" and logs in. tn.read_until('> ')...
0
1523
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 some modules that will help me build one? Thank you!
6
1965
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 def __call__(self): return self.s.upper() l=]
18
460
by: a | last post by:
can someone tell me how to use them thanks
4
1803
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 lc expression as a for loop to drive the recursion. Thanks for any insight! Gregory
10
15010
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
3050
by: beginner | last post by:
Hi All, If I have a list comprehension: ab= c = "ABC" print c
0
7896
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8186
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7936
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6583
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5704
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3821
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1436
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1160
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.