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

Steve Holden steve at holdenweb.com
Thu Dec 16 17:38:55 EST 2004


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.
>  
Well since that's the case it would seem you'd be best processing each 
item from the large list against the small list, though in truth it may 
not make any difference.

It looks like the best way to proceed might be to reduce each line to a 
canonical form -- strip the parens and other irrelevant characters out, 
and sort the words in order. After that it'd be relatively simple to 
determine whether two lines match - they'd be the same!

The only slight wrinkle would be keeping the original lines for 
reference, but that's not difficult.

Does this give you enough of an idea, or do you need code samples?

regards
  Steve
> 
> 
> 
> Steve Holden <steve at holdenweb.com> wrote:
> 
> 
>>Mark Devine wrote:
>>
>>
>>>Sorry for not putting a subject in the last e-mail. The function lower suited my case exactly. Here however is my main problem:
>>>Given that my new list is :
>>>[class-map match-all cmap1', 'match ip any', 'class-map match-any cmap2', 'match any', 'policy-map policy1', 'class cmap1', 'policy-map policy2', 'service-policy policy1', 'class cmap2']
>>>
>>>Each element in my new list could appear in any order together within another larger list (list1) and I want to count how many matches occur. For example the larger list could have an element 'class-map cmap2 (match any)' and I want to match that but if only 'class-map match-any' or 'class-map cmap2' appears I don't want it to match. 
>>>
>>>Can anybody help?
>>>Is my problem clearly stated?
>>>
>>
>>Well, let's see: you'd like to know which strings occur in both lists, 
>>right?
>>
>>You might like to look at the "Efficient grep using Python?" thread for 
>>  suggestions. My favorite would be:
>>
>>.>>> lst1 = ["ab", "ac", "ba", "bb", "bc"]
>>.>>> lst2 = ["ac", "ab", "bd", "cb", "bb"]
>>.>>> dct1 = dict.fromkeys(lst1)
>>.>>> [x for x in lst2 if x not in dct1]
>>['bd', 'cb']
>>.>>> [x for x in lst2 if x in dct1]
>>['ac', 'ab', 'bb']
>>
>>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
>>-- 
>>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
> 
> 


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



More information about the Python-list mailing list