I'm having a bit of trouble with this so any help would be gratefully
recieved...
After splitting up a url I have a string of the form
'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag will only be
preceeded by an operator if it is a '-', if it is preceded by nothing,
'+' is to be assumed.
Using re.split, I can generate a list that looks thus:
['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
I wish to derive two lists - each containing either tags to be
included, or tags to be excluded. My idea was to take an element,
examine what element precedes it and accordingly, insert it into the
relevant list. However, I have not been successful.
Is there a better way that I have not considered? If this method is
suitable, how might I implement it?
Thanks all,
Rob Cowie 14 1201
Rob Cowie wrote: I'm having a bit of trouble with this so any help would be gratefully recieved...
After splitting up a url I have a string of the form 'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag will only be preceeded by an operator if it is a '-', if it is preceded by nothing, '+' is to be assumed.
Using re.split, I can generate a list that looks thus: ['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
I wish to derive two lists - each containing either tags to be included, or tags to be excluded. My idea was to take an element, examine what element precedes it and accordingly, insert it into the relevant list. However, I have not been successful.
Is there a better way that I have not considered? If this method is suitable, how might I implement it?
Thanks all,
Rob Cowie
a = [ '+', 'tag1', '+', 'tag2', '-', 'tag3', '+', 'tag4' ]
import itertools
b = list(itertools.islice(a,0,8,2))
c = list(itertools.islice(a,1,8,2))
result1 = [x[1] for x in itertools.izip(b,c) if x[0] == '+']
result2 = [x[1] for x in itertools.izip(b,c) if x[0] == '-']
print
print result1
print result2
Gerard
Rob Cowie wrote: I wish to derive two lists - each containing either tags to be included, or tags to be excluded. My idea was to take an element, examine what element precedes it and accordingly, insert it into the relevant list. However, I have not been successful.
Is there a better way that I have not considered?
Maybe. You could write a couple regexes, one to find the included
tags, and one for the excluded, then run re.findall on them both.
But there's nothing fundamentally wrong with your method.
If this method is suitable, how might I implement it?
tags = ['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
include, exclude = [], []
op = '+'
for cur in tags:
if cur in '+-':
op = cur
else:
if op == '+':
include.append(cur)
else:
exclude.append(cur)
--Ben
Gerard Flanagan wrote: Rob Cowie wrote: I'm having a bit of trouble with this so any help would be gratefully recieved...
After splitting up a url I have a string of the form 'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag will only be preceeded by an operator if it is a '-', if it is preceded by nothing, '+' is to be assumed.
Using re.split, I can generate a list that looks thus: ['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
I wish to derive two lists - each containing either tags to be included, or tags to be excluded. My idea was to take an element, examine what element precedes it and accordingly, insert it into the relevant list. However, I have not been successful.
Is there a better way that I have not considered? If this method is suitable, how might I implement it?
Thanks all,
Rob Cowie
a = [ '+', 'tag1', '+', 'tag2', '-', 'tag3', '+', 'tag4' ]
import itertools
b = list(itertools.islice(a,0,8,2)) c = list(itertools.islice(a,1,8,2))
result1 = [x[1] for x in itertools.izip(b,c) if x[0] == '+'] result2 = [x[1] for x in itertools.izip(b,c) if x[0] == '-']
print print result1 print result2
Gerard
'8' is the length of 'a' (len(a))
Rob Cowie wrote: I'm having a bit of trouble with this so any help would be gratefully recieved...
After splitting up a url I have a string of the form 'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag will only be preceeded by an operator if it is a '-', if it is preceded by nothing, '+' is to be assumed.
Using re.split, I can generate a list that looks thus: ['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
I wish to derive two lists - each containing either tags to be included, or tags to be excluded. My idea was to take an element, examine what element precedes it and accordingly, insert it into the relevant list. However, I have not been successful.
Is there a better way that I have not considered? If this method is suitable, how might I implement it?
Thanks all,
Rob Cowie
Unclever way:
alist = ['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
include, disinclude = [], []
aniter = iter(alist)
if len(alist) % 2:
include.append(aniter.next())
for asign in aniter:
if asign == '+':
include.append(aniter.next())
else:
disinclude.append(aniter.next())
A cleverer way will probably use list comprehension and logic shortcutting.
James
Gerard Flanagan wrote: Rob Cowie wrote:
I'm having a bit of trouble with this so any help would be gratefully recieved...
After splitting up a url I have a string of the form 'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag will only be preceeded by an operator if it is a '-', if it is preceded by nothing, '+' is to be assumed.
Using re.split, I can generate a list that looks thus: ['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
I wish to derive two lists - each containing either tags to be included, or tags to be excluded. My idea was to take an element, examine what element precedes it and accordingly, insert it into the relevant list. However, I have not been successful.
Is there a better way that I have not considered? If this method is suitable, how might I implement it?
Thanks all,
Rob Cowie
a = [ '+', 'tag1', '+', 'tag2', '-', 'tag3', '+', 'tag4' ]
import itertools
b = list(itertools.islice(a,0,8,2)) c = list(itertools.islice(a,1,8,2))
result1 = [x[1] for x in itertools.izip(b,c) if x[0] == '+'] result2 = [x[1] for x in itertools.izip(b,c) if x[0] == '-']
print print result1 print result2
Gerard
Unfortunately this does not address the complete specification: a = [ 'tag1', '+', 'tag2', '-', 'tag3', '+', 'tag4' ]
import itertools
b = list(itertools.islice(a,0,len(a),2)) c = list(itertools.islice(a,1,len(a),2))
result1 = [x[1] for x in itertools.izip(b,c) if x[0] == '+'] result2 = [x[1] for x in itertools.izip(b,c) if x[0] == '-']
print
print result1
[] print result2
[]
Need to check for the absence of that first op.
James
Rob Cowie a écrit : I'm having a bit of trouble with this so any help would be gratefully recieved...
After splitting up a url I have a string of the form 'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag will only be preceeded by an operator if it is a '-', if it is preceded by nothing, '+' is to be assumed.
Using re.split, I can generate a list that looks thus: ['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
I wish to derive two lists - each containing either tags to be included, or tags to be excluded. My idea was to take an element, examine what element precedes it and accordingly, insert it into the relevant list. However, I have not been successful.
Is there a better way that I have not considered?
If you're responsible for the original URL, you may consider rewriting
it this way:
scheme://domain.tld/resource?tag1=1&tag2=1&tag3=1&tag4=0
Else - and after you've finished cursing the guy that came out with such
an innovative way to use url parameters - I think the first thing to do
would be to fix the implicit-first-operator-mess, so you have something
consistent:
if the_list[0] != "-":
the_list.insert(0, "+")
Then a possible solution could be:
todo = {'+' : [], '-' : []}
for op, tag in zip(the_list[::2], the_list[1::2]):
todo[op].append(tag)
But there's surely something better...
Bruno Desthuilliers wrote: Rob Cowie a écrit :
I'm having a bit of trouble with this so any help would be gratefully recieved...
After splitting up a url I have a string of the form 'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag will only be preceeded by an operator if it is a '-', if it is preceded by nothing, '+' is to be assumed.
Using re.split, I can generate a list that looks thus: ['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
I wish to derive two lists - each containing either tags to be included, or tags to be excluded. My idea was to take an element, examine what element precedes it and accordingly, insert it into the relevant list. However, I have not been successful.
Is there a better way that I have not considered?
If you're responsible for the original URL, you may consider rewriting it this way: scheme://domain.tld/resource?tag1=1&tag2=1&tag3=1&tag4=0
Else - and after you've finished cursing the guy that came out with such an innovative way to use url parameters - I think the first thing to do would be to fix the implicit-first-operator-mess, so you have something consistent:
if the_list[0] != "-": the_list.insert(0, "+")
Then a possible solution could be:
todo = {'+' : [], '-' : []} for op, tag in zip(the_list[::2], the_list[1::2]): todo[op].append(tag)
But there's surely something better...
Fabulous. Here is a fix:
the_list = ['+'] * (len(the_list) % 2) + the_list
todo = {'+' : [], '-' : []}
for op, tag in zip(the_list[::2], the_list[1::2]):
todo[op].append(tag)
Gerard Flanagan <gr********@yahoo.co.uk> wrote:
... a = [ '+', 'tag1', '+', 'tag2', '-', 'tag3', '+', 'tag4' ]
import itertools
b = list(itertools.islice(a,0,8,2)) c = list(itertools.islice(a,1,8,2))
Much as I love itertools, this specific task would be best expressed ad
b = a[::2]
c = a[1::2]
Do note that you really don't need the 'list(...)' here, for the
following use:
result1 = [x[1] for x in itertools.izip(b,c) if x[0] == '+'] result2 = [x[1] for x in itertools.izip(b,c) if x[0] == '-']
....would be just as good if b and c were islice objects rather than
lists, except for the issue of _repeating_ (izipping twice). I'd rather
do some variant of a single-loop such as:
results = {'+':[], '-':[]}
for operator, tag in itertools.izip(a[::2], a[1::2]):
results[operator].append(tag)
and use results['+'] and results['-'] thereafter.
These approaches do not consider the inconvenient fact that the leading
'+' does in fact not appear in list a -- it needs to be assumed, the OP
stated; only a '-' would instead appear explicitly. Little for it but
specialcasing depending on whether a[0]=='-', I think -- e.g. in the
above 3-line snippet of mine, insert right after the first line:
if a[0]!='-': results['+'].append(a.pop(0))
Alex
James Stroud wrote: Gerard Flanagan wrote: Rob Cowie wrote:
I'm having a bit of trouble with this so any help would be gratefully recieved...
After splitting up a url I have a string of the form 'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag will only be preceeded by an operator if it is a '-', if it is preceded by nothing, '+' is to be assumed.
Using re.split, I can generate a list that looks thus: ['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
I wish to derive two lists - each containing either tags to be included, or tags to be excluded. My idea was to take an element,
[...] a = [ '+', 'tag1', '+', 'tag2', '-', 'tag3', '+', 'tag4' ]
import itertools
b = list(itertools.islice(a,0,8,2)) c = list(itertools.islice(a,1,8,2))
result1 = [x[1] for x in itertools.izip(b,c) if x[0] == '+'] result2 = [x[1] for x in itertools.izip(b,c) if x[0] == '-']
print print result1 print result2
Gerard
Unfortunately this does not address the complete specification: >>> a = [ 'tag1', '+', 'tag2', '-', 'tag3', '+', 'tag4' ] >>> >>> import itertools >>> >>> b = list(itertools.islice(a,0,len(a),2)) >>> c = list(itertools.islice(a,1,len(a),2)) >>> >>> result1 = [x[1] for x in itertools.izip(b,c) if x[0] == '+'] >>> result2 = [x[1] for x in itertools.izip(b,c) if x[0] == '-'] >>> >>> print >>> print result1 [] >>> print result2
[]
Need to check for the absence of that first op.
James
Yes, should have stuck to the spec.
Gerard
Alex Martelli wrote: Gerard Flanagan <gr********@yahoo.co.uk> wrote: ... a = [ '+', 'tag1', '+', 'tag2', '-', 'tag3', '+', 'tag4' ]
import itertools
b = list(itertools.islice(a,0,8,2)) c = list(itertools.islice(a,1,8,2)) Much as I love itertools, this specific task would be best expressed ad
b = a[::2] c = a[1::2]
Yes, I thought that when I saw bruno's solution - I can't say that I've
never seen that syntax before, but I never really understood that this
is what it did.
Do note that you really don't need the 'list(...)' here, for the following use:
result1 = [x[1] for x in itertools.izip(b,c) if x[0] == '+'] result2 = [x[1] for x in itertools.izip(b,c) if x[0] == '-'] ...would be just as good if b and c were islice objects rather than lists, except for the issue of _repeating_ (izipping twice).
I couldn't get it to work without the 'list(...)' , it seems you must
have to 'rewind' the islice, eg. this works:
b = itertools.islice(a,0,8,2)
c = itertools.islice(a,1,8,2)
result1 = [x[1] for x in itertools.izip(b,c) if x[0] == '+']
b = itertools.islice(a,0,8,2)
c = itertools.islice(a,1,8,2)
result2 = [x[1] for x in itertools.izip(b,c) if x[0] == '-']
but not without that 're-assignment' of b and c.
I'd rather do some variant of a single-loop such as:
results = {'+':[], '-':[]} for operator, tag in itertools.izip(a[::2], a[1::2]): results[operator].append(tag)
and use results['+'] and results['-'] thereafter.
These approaches do not consider the inconvenient fact that the leading '+' does in fact not appear in list a -- it needs to be assumed, the OP stated; only a '-' would instead appear explicitly. Little for it but specialcasing depending on whether a[0]=='-', I think -- e.g. in the above 3-line snippet of mine, insert right after the first line:
if a[0]!='-': results['+'].append(a.pop(0))
Alex
Cheers
Gerard
"Rob Cowie" <co*******@gmail.com> wrote in message
news:11**********************@t39g2000cwt.googlegr oups.com... I'm having a bit of trouble with this so any help would be gratefully recieved...
After splitting up a url I have a string of the form 'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag will only be preceeded by an operator if it is a '-', if it is preceded by nothing, '+' is to be assumed.
Using re.split, I can generate a list that looks thus: ['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
I wish to derive two lists - each containing either tags to be included, or tags to be excluded. My idea was to take an element, examine what element precedes it and accordingly, insert it into the relevant list. However, I have not been successful.
Is there a better way that I have not considered? If this method is suitable, how might I implement it?
Thanks all,
Rob Cowie
Here's how this would look with pyparsing (download pyparsing at http://pyparsing.sourceforge.net ):
data = 'tag1+tag2+tag3-tag4'
from pyparsing import *
tag = Word(alphas,alphanums)
incl = Literal("+").suppress()
excl = Literal("-").suppress()
inclQual = Optional(incl) + tag
exclQual = excl + tag
qualDef = OneOrMore(
inclQual.setResultsName("includes",listAllMatches= True ) |
exclQual.setResultsName("excludes",listAllMatches= True ) )
quals = qualDef.parseString(data)
print quals.includes
print quals.excludes
Prints out:
[['tag1'], ['tag2'], ['tag3']]
[['tag4']]
-- Paul
Gerard Flanagan a écrit : Alex Martelli wrote:
Gerard Flanagan <gr********@yahoo.co.uk> wrote: ...
a = [ '+', 'tag1', '+', 'tag2', '-', 'tag3', '+', 'tag4' ]
import itertools
b = list(itertools.islice(a,0,8,2)) c = list(itertools.islice(a,1,8,2))
Much as I love itertools, this specific task would be best expressed ad
b = a[::2] c = a[1::2]
Yes, I thought that when I saw bruno's solution - I can't say that I've never seen that syntax before, but I never really understood that this is what it did.
It's in fact pretty simple. The full slice syntax is [start:end:step],
with default values of start=0, end=len(seq), step=1. So a[::2] will
retrieve a[0], a[2], a[4] etc, and a[1::2] -> a[1], a[3], a[5] etc.
(snip)
Rob Cowie wrote: I'm having a bit of trouble with this so any help would be gratefully recieved...
After splitting up a url I have a string of the form 'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag will only be preceeded by an operator if it is a '-', if it is preceded by nothing, '+' is to be assumed.
Using re.split, I can generate a list that looks thus: ['tag1', '+', 'tag2', '+', 'tag3', '-', 'tag4']
I wish to derive two lists - each containing either tags to be included, or tags to be excluded. My idea was to take an element, examine what element precedes it and accordingly, insert it into the relevant list. However, I have not been successful.
Is there a better way that I have not considered? If this method is suitable, how might I implement it?
Thanks all,
Rob Cowie
Since you're already using a regexp, why not modify it to group the operators
with their tags? : import re source = "tag1+tag2+tag3-tag4"
.... tagfinder = re.compile("([+-]?)(\w+)")
.... include = [] exclude = []
.... for op, tag in tagfinder.findall(source):
.... if op == "-":
.... exclude.append(tag)
.... else:
.... include.append(tag)
.... include
['tag1', 'tag2', 'tag3'] exclude
['tag4']
(Example assumes that a tag can be matched by \w+ and that there
is no space between the operators and their tags)
Michael
Thanks everyone. I assumed there was something I had not considered...
list slicing is that thing.
The pyParsing example looks interesting - but for this case, a little
too heavy. It doesn't really warrant including a third party module.
Rob C This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Generic Usenet Account |
last post by:
To settle the dispute regarding what happens when an "erase" method is
invoked on an STL container (i.e. whether the element is merely
removed from the container or whether it also gets deleted in...
|
by: C++fan |
last post by:
Suppose that I define the following class:
class example_class{
public:
example_class();
void funtion_1();
void function_2();
protected:
|
by: Bernd.Moos |
last post by:
Given the following XML document:
<text>
<p>
<w>Ronaldo</w>
<w>scoredw>
<w>the</w>
<w>1</w>
<c>:</c>
<w>1</w>
|
by: Herrcho |
last post by:
in K&R Chapter 6.3
it mentions two methods to calculate NKEYS.
and points out the first one which is to terminate the list of
initializers with a null pointer, then loop along keytab until the...
|
by: plmanikandan |
last post by:
Hi,
I am new to link list programming.I need to traverse from the end of
link list.Is there any way to find the end of link list without
traversing from start(i.e traversing from first to find the...
|
by: Andy Dingley |
last post by:
Imagine the XHTML "definition list" structure
<dl>
<dt>A</dt>
<dd>a</dd>
<dt>B</dt>
<dd>b1</dd>
<dd>b2</dd>
|
by: patrizio.trinchini |
last post by:
Hi,
how can remove sibling elements based on the value of an attribute ?
For instance, gven the XML document:
<root>
<parentElment>
<testElement name="A">
<removableElement/>
|
by: stephen.cunliffe |
last post by:
Hi,
I'm looking for opinion/facts/arguments on the correct nesting of UL,
OL, & LI elements.
For example, this is what I want (unordered list):
* Item 1
* Item 2
* Item 3
|
by: Linda |
last post by:
Hi,
Sorry if this is the wrong group I'm posting to. We are using a CMS
which saves all files as .xml. We have an xslt file where we parse
all html tags. At the moment, the CMS sometimes puts...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |