Subject: | |
From: | |
Reply To: | |
Date: | Sun, 19 Nov 2006 00:34:02 +0000 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
Paul Berkowitz wrote:
> That gets me the 'difference' items. I'd also like to get the line
> numbers
> of those items in the l2 text.
See <http://docs.python.org/lib/module-sets.html>.
from sets import Set
list1 = [1,2,3,4,5,6]
list2 = [2,4,7]
set1 = Set(list1)
set2 = Set(list2)
unique_to_list1= list(set1 - set2)
print unique_to_list1
common_to_both = list(set1 & set2)
print common_to_both
unique_to_list1_indexes = [list1.index(val) + 1 for val in
unique_to_list1]
print unique_to_list1_indexes
common_to_both_indexes_for_list2= [list2.index(val) + 1 for val in
common_to_both]
print common_to_both_indexes_for_list2
Bear in mind that if a source list contains multiple identical
values, only the first one will be considered. Also, remember that
case is considered when comparing text, so if you need to do case-
insensitive comparisons you'll need to normalise all the text
beforehand.
If passing information via the shell, remember to deal with text
encoding issues appropriately. If passing input data directly via 'do
shell script' rather than by file don't forget that there's a limit
on how much you can pass that way. If using TIDs to split return-
delimited Unicode text into lists and vice-versa, bear in mind that
this has at least one known bug where many non-ASCII characters will
cause list-to-unicode-text coercions to fail silently.
HTH
has
--
http://freespace.virgin.net/hamish.sanderson/
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org
--
http://freespace.virgin.net/hamish.sanderson/
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org
|
|
|