Python's set datatype
Here's a little tip for the friday. Python's set datatype was added in Python2.4 and it's really great and can be used in different situations.
First, when you want to filter out a list to extract only unique items: print set([1, 1, 3, 4, 5, 3, 2])
Second, when you want to find out how two lists relate, for example, find out all the elements they have in common: print set([1, 1, 3, 4, 5, 3, 2]).difference([1, 2])
print set([1, 1, 3, 4, 5, 3, 2]).intersection([1, 2])
That's it :) 6 comments so far
Post a comment
Commenting on this post has expired.
|
Blog labels |