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

Michael Spencer mahs at telcopartners.com
Thu Dec 16 19:45:35 EST 2004


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




More information about the Python-list mailing list