How to convert list of tuples into a single list of unique values ? More complete picture
pekka niiranen
krissepu at vip.fi
Fri Jan 11 03:36:33 EST 2002
To give you a more complete picture of my goals:
I am trying to solve the problem of using reqular expression with nested
matches.
In example below I am finding words that are separated by ?- and ! -sings .
import sre
>>> Line = "?AA?BB!CC!?DD!ee?EE!ff?FF?GG!HH!hh?BB!dd?DD!ee"
>>> print sre.findall(pattern, Line)
[('?AA?BB!CC!', '?BB!'), ('?DD!', ''), ('?EE!', ''), ('?FF?GG!HH!', '?GG!'), ('?BB!', ''), ('?DD!', '')]
True, when using the sre above, one must set the amount of nesting
beforehand and use a single
character separators (in this case ? and !) for words. This is not a
problem at the moment, though
However, since I can use new finditer -method to loop thru matches,
>>> for m in sre.finditer(pattern, Line):
... print repr(m.group())
...
'?AA?BB!CC!'
'?DD!'
'?EE!'
'?FF?GG!HH!'
'?BB!'
'?DD!'
my intuition tells me I should be able to build the final list directly
without using intermediate lists
as buffers for removing empty matches. This is just a hunch.
I would be nice to override finditer -method to do cleaning of doubles,
but I have not
browsed thru python module -codes yet. Maybe this weekend.
The ultimate solution would be such, that:
- multiple character word -separators could be given ( notation [^?!])
will not work then ) and
- nesting -level is a parameter when building sre -pattern.
Thank you anyways,
-pekka-
Huaiyu Zhu wrote:
>On 10 Jan 2002 06:49:28 -0800, Paul Rubin <phr-n2002a at nightsong.com> wrote:
>
>>pekka niiranen <krissepu at vip.fi> writes:
>>
>>>I have a list of equal size tuples, but a tuple may contain empty values:
>>>
>>>t = [ ( a,b,c), (d, ,f) (b,a,j) ]
>>>
>>>How can I convert it into a single list of unique values:
>>>
>>>l = [a,b,c,d,f,j]
>>>
>>>No lambdas, please
>>>
>>I don't think (d, ,f) is syntactically valid. Other than that,
>>is something wrong with
>> import operator
>> l = operator.add(*t)
>>
>
>
>t = [ ( 'a','b','c'), ('d','f'), ('b','a','j') ]
>
>import operator
># Use reduce for more than two arguments
>l = list(reduce(operator.add, t))
>
># For uniqueness
>l.sort()
>def uniq(l, a):
> if [a] != l[-1:]: l.append(a)
> return l
>
>l = reduce(uniq, l, [])
>
>
>Huaiyu
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20020111/a63d112f/attachment.html>
More information about the Python-list
mailing list