comparing lists
Paul Magwene
p.magwene at snet.net
Wed May 8 18:31:08 EDT 2002
On Wed, 08 May 2002 18:40:26 -0400, Mark McEahern wrote:
> Suppose I want to do a boolean comparison (equal or not equal) of two
> lists case-insensitively and I don't care about order. I can do this:
>
> 1. Create temporary copies that are lowercased and use the in operator:
>
> 2. Sort temporary copies, then then compare them.
>
> Are there other options? Is there a strong reason to prefer one
> approach over the other?
>
> Sample code below.
>
> Thanks,
>
> // mark
>
>
If you know you're dealing with lists of strings why don't you use the
"join" methods in the string module.
For example:
>>> import string
>>> l = ['a','b','c']
>>> m = ['C','b','a']
>>> n = ['A','B','c']
>>> lstr = string.join(l)
>>> mstr = string.join(m)
>>> nstr = string.join(n)
>>> lstr.lower()
'a b c'
>>> lstr.lower() == nstr.lower()
1
>>> lstr.lower() == mstr.lower()
0
>>>
--Paul
More information about the Python-list
mailing list