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"
::::::::::::::: ::::::::::::::: ::::::::::::::: ::::::::::::::: :::::::: 6 1493
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
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
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) ]
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
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
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( ) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 has to be a *really* easy way of doing it - after a
|
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':
|
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('> ')
tn.write('THE COMMAND IS HERE\n')
|
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!
|
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=]
| |
by: a |
last post by:
can someone tell me how to use them
thanks
|
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
|
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 =
|
by: beginner |
last post by:
Hi All,
If I have a list comprehension:
ab=
c = "ABC"
print c
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |