An OrderedSet acts very much like a list. There are two important differences:
Use it like you would use a list. All the standard operators are defined.
Create a set with a few initial items:
>>> s = OrderedSet(['apple', 'banana', 'pear'])
Look up the index of ‘banana’:
>>> s.index('banana')
1
>>> s.indexFor('banana') # (synonym)
1
Look up an unknown index:
>>> s.index('automobile')
Traceback (most recent call last):
...
KeyError: 'automobile'
Add a new item:
>>> s.add('orange')
3
>>> s.index('orange')
3
Add an item that’s already there:
>>> s.add('apple')
0
Extend with some more items:
>>> s.extend(['grapefruit', 'kiwi'])
>>> s.index('grapefruit')
4
See that it otherwise behaves like a list:
>>> s[0]
'apple'
>>> s[0] = 'Apple'
>>> s[0]
'Apple'
>>> len(s)
6
>>> for item in s:
... print item,
Apple banana pear orange grapefruit kiwi
None element is used as a placeholder for non-present elements, but it is never semantically an element of the set:
>>> del s[0]
>>> s[0] is None
True
>>> s.index('banana')
1
>>> None in s
False
(We would have used Ellipsis, but it’s not picklable, and it’s not worth the trouble to work around that.)
Initialize a new OrderedSet.
Add an item to the set (unless it’s already there), returning its index.
None is never an element of an OrderedSet.
Add an item to the set (unless it’s already there), returning its index.
None is never an element of an OrderedSet.
An object that behaves like an OrderedSet, but simply contains the range of numbers up to len. Thus, every number is its own index.
IdentitySets are used to label Tensors on axes where labels would be meaningless or unnecessary.