Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: create lowercase strings in lists - was: (No subject)

Reply
Thread Tools

Re: create lowercase strings in lists - was: (No subject)

 
 
Mark Devine
Guest
Posts: n/a
 
      12-17-2004
I got the script working. Thanks for all your help everyone. Trouble is its not showing the correct results. Here is the script and results:

#!/usr/bin/env python
import os
import sys
import time
import string
import pexpect
import commands
from sets import Set as set


# Test if the words of list2 elements appear in any order in list1 elements
# disregarding case and parens

# Reference list
list1 = ["a b C (D)", "D A B", "A B E"]
# Test list
list2 = ["A B C D", "A B D", "A E F", "A (E) B", "A B", "E A B" ]

def normalize(text, unwanted = "()", table = string.maketrans(string.ascii_uppercase,string.asc ii_lowercase)):
text.translate(table,unwanted)
return set(text.split())

reflist = [normalize(element) for element in list1]
print reflist

#This is the list of sets to test against


def testmember(element):
"""is element a member of the reflist, according to the above rules?"""
testelement = normalize(element)
#brute force comparison until match - depends on small reflist
for el in reflist:
if el.issuperset(testelement):
return True
return False

for element in list2:
print element, testmember(element)

Here is the results:

$ ./test.py
[Set(['a', 'C', 'b', '(D)']), Set(['A', 'B', 'D']), Set(['A', 'B', 'E'])]
A B C D False
A B D True
A E F False
A (E) B False
A B True
E A B True

The results should be:

> A B C D True
> A B D True
> A E F False
> A (E) B True
> A B False
> E A B True






Michael Spencer <> wrote:

>
> Steve Holden wrote:
> > Mark Devine wrote:
> >
> >> Actually what I want is element 'class-map match-all cmap1' from list
> >> 1 to match 'class-map cmap1 (match-all)' or 'class-map cmap1 mark
> >> match-all done' in list 2 but not to match 'class-map cmap1'.
> >> Each element in both lists have multiple words in them. If all the
> >> words of any element of the first list appear in any order within any
> >> element of the second list I want a match but if any of the words are
> >> missing then there is no match. There are far more elements in list 2
> >> than in list 1.
> >>

> >

> sounds like a case for sets...
>
> >>> # NB Python 2.4

> ...
> >>> # Test if the words of list2 elements appear in any order in list1 elements
> >>> # disregarding case and parens

> ...
> >>> # Reference list
> >>> list1 = ["a b C (D)",

> ... "D A B",
> ... "A B E"]
> >>> # Test list
> >>> list2 = ["A B C D", #True

> ... "A B D", #True
> ... "A E F", #False
> ... "A (E) B", #True
> ... "A B", #True
> ... "E A B" ]
> ...
> >>> def normalize(text, unwanted = "()"):

> ... conv = "".join(char.lower() for char in text if char not in unwanted)
> ... return set(conv.split())
> ...
> >>> reflist = [normalize(element) for element in list1]
> >>> print reflist

> ...
> [set(['a', 'c', 'b', 'd']), set(['a', 'b', 'd']), set(['a', 'b', 'e'])]
>
> This is the list of sets to test against
>
>
> >>> def testmember(element):

> ... """is element a member of the reflist, according to the above rules?"""
> ... testelement = normalize(element)
> ... #brute force comparison until match - depends on small reflist
> ... for el in reflist:
> ... if el.issuperset(testelement):
> ... return True
> ... return False
> ...
> >>> for element in list2:

> ... print element, testmember(element)
> ...
> A B C D True
> A B D True
> A E F False
> A (E) B True
> A B True
> E A B True
> >>>

>
> Michael
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>




__________________________________________________ _______________
Sign up for eircom broadband now and get a free two month trial.*
Phone 1850 73 00 73 or visit http://home.eircom.net/broadbandoffer


 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-17-2004
Mark Devine wrote:

> I got the script working. Thanks for all your help everyone. Trouble is
> its not showing the correct results. Here is the script and results:


In my book it is not working then.

> def normalize(text, unwanted = "()", table =
> string.maketrans(string.ascii_uppercase,string.asc ii_lowercase)):
> text.translate(table,unwanted)


Strings are immutable in Python. Make that

text = text.translate(table, unwanted)

This line of the script's output could have given you a clue:

> [Set(['a', 'C', 'b', '(D)']), Set(['A', 'B', 'D']), Set(['A', 'B', 'E'])]


(I didn't look any further, so there may be other problems)

Peter

PS: Do us a favour and (a) don't top-post (b) use space not tabs in your
source code (c) remove all text quoted from the parent that is not relevant
to your current problem.
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      12-17-2004
Mark Devine wrote:

> I got the script working. Thanks for all your help everyone. Trouble is its not showing the correct results. Here is the script and results:
>

Well, that's a pretty unusual interpretation of the word "working"

> [...]


I see from later postings you are getting closer to an answer, but
obviously you still have to strip out the characters that you don't want
to affect the match (such as "(" and ")").

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      12-17-2004
Mark Devine wrote:

> I got the script working. Thanks for all your help everyone. Trouble is its not showing the correct results. Here is the script and results:
>

Well, that's a pretty unusual interpretation of the word "working"

> [...]


I see from later postings you are getting closer to an answer, but
obviously you still have to strip out the characters that you don't want
to affect the match (such as "(" and ")").

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
converting lists to strings to lists robin Python 10 04-12-2006 04:58 PM
Re: create lowercase strings in lists - was: (No subject) Mark Devine Python 1 12-17-2004 09:49 AM
Re: create lowercase strings in lists - was: (No subject) Mark Devine Python 6 12-17-2004 08:09 AM
Re: create lowercase strings in lists - was: (No subject) Mark Devine Python 2 12-16-2004 08:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57