Home › Forums › Python Programming › "set" in python
- This topic has 0 replies, 1 voice, and was last updated 10 years, 4 months ago by Humble.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
HumbleKeymaster
set is an unordered collection of unique elements. Very useful to remove duplicate entries from a collection/sequence/iteratable object.
The elements of a Python set must be immutable. In particular, you can’t have list or dictionary elements in a set.
sets do not support indexing, slicing, or other sequence-like behavior. Values of type set are mutable..Below methods are available with “sets”..
| add(...) | Add an element to a set. | | This has no effect if the element is already present. | | clear(...) | Remove all elements from this set. | | copy(...) | Return a shallow copy of a set. | | difference(...) | Return the difference of two or more sets as a new set. | | (i.e. all elements that are in this set but not the others.) | | difference_update(...) | Remove all elements of another set from this set. | | discard(...) | Remove an element from a set if it is a member. | | If the element is not a member, do nothing. | | intersection(...) | Return the intersection of two or more sets as a new set. | | (i.e. elements that are common to all of the sets.) | | intersection_update(...) | Update a set with the intersection of itself and another. | isdisjoint(...) | Return True if two sets have a null intersection. | | issubset(...) | Report whether another set contains this set. | | issuperset(...) | Report whether this set contains another set. | | pop(...) | Remove and return an arbitrary set element. | Raises KeyError if the set is empty. | | remove(...) | Remove an element from a set; it must be a member. | | If the element is not a member, raise a KeyError. | | symmetric_difference(...) | Return the symmetric difference of two sets as a new set. | | (i.e. all elements that are in exactly one of the sets.) | | symmetric_difference_update(...) | Update a set with the symmetric difference of itself and another. | | union(...) | Return the union of sets as a new set. | | (i.e. all elements that are in either set.) | | update(...) | Update a set with the union of itself and others. Examples:
>>>> s = [1,2,1,2,12,333] >>> set(s) set([1, 2, 12, 333]) >>> s1= set(s) >>> s1 set([1, 2, 12, 333]) >>> type(s1) <type 'set'> >>> len(s1) 4 >>> s2=[8,54] >>> s1.union(s2) set([1, 2, 8, 12, 333, 54]) >>> s1 set([1, 2, 12, 333]) >>> for x in s1: ... print x ... 1 2 12 333 >>> 2 in s1 True >>> 3 in s1 False like union others like intersection(), difference(), copy() and symmetric_difference() ..etc are possible on sets..
s1|set(s2) set([1, 2, 54, 8, 12, 333]) >>> s1 set([1, 2, 12, 333]) >>> s2 [8, 54] >>> s1&set(s2) set([]) >>> >>> s1.add(100) >>> s1 set([1, 2, 12, 333, 100]) >>>
-
AuthorPosts
Viewing 1 post (of 1 total)