comparing lists
Lulu of the Lotus-Eaters
mertz at gnosis.cx
Wed May 8 20:18:26 EDT 2002
|[Paul Magwene]
|> If you know you're dealing with lists of strings why don't you use the
|> "join" methods in the string module.
"Mark McEahern" <marklists at mceahern.com> wrote previously:
|Thanks. I hadn't thought of that. Sadly, it won't work for me because as I
|said my comparison is order-insensitive whereas your approach is
|order-sensitive.
You can always sort the lists before joining them. Or sort copies if
you need to leave the originals as-s. I.e.
ltmp = copy.copy(lst)
ltmp.sort()
...
There was a flaw, however, with Magwene's suggestion. Once you join the
items, you lose some information about what was where originally. For
example:
l1 = ['ab ','c',' d']
l2 = ['ab',' c ','d']
l3 = ['ab c d']
These will all compare as equal. What you probably want to do is add a
delimiter between the elements that you are pretty sure will not occur
in the original strings, e.g.:
>>> string.join(['a','b','c'],'$%^').upper()
'A$%^B$%^C'
or,
>>> '|-|'.join(['a','b','c']).upper()
'A|-|B|-|C'
--
---[ to our friends at TLAs (spread the word) ]--------------------------
Echelon North Korea Nazi cracking spy smuggle Columbia fissionable Stego
White Water strategic Clinton Delta Force militia TEMPEST Libya Mossad
---[ Postmodern Enterprises <mertz at gnosis.cx> ]--------------------------
More information about the Python-list
mailing list