arbitrary_element(iterable)
This is most useful for "peeking" at an arbitrary element of a set, but can be used for any list, dictionary, etc., as well.
This function does not return a random element. If iterable
is ordered, sequential calls will return the same value:
>>> l = [1, 2, 3] >>> nx.utils.arbitrary_element(l) 1 >>> nx.utils.arbitrary_element(l) 1
Any object that implements __iter__
, e.g. set, dict, list, tuple, etc.
If iterable
is an iterator (because the current implementation of this function would consume an element from the iterator).
Returns an arbitrary element of iterable
without removing it.
Arbitrary elements from common Iterable objects:
>>> nx.utils.arbitrary_element([1, 2, 3]) # list 1
>>> nx.utils.arbitrary_element((1, 2, 3)) # tuple 1
>>> nx.utils.arbitrary_element({1, 2, 3}) # set 1
>>> d = {k: v for k, v in zip([1, 2, 3], [3, 2, 1])}
... nx.utils.arbitrary_element(d) # dict_keys 1
>>> nx.utils.arbitrary_element(d.values()) # dict values 3
:None:None:`str`
is also an Iterable:
>>> nx.utils.arbitrary_element("hello") 'h'
ValueError
is raised if iterable
is an iterator:
>>> iterator = iter([1, 2, 3]) # Iterator, *not* IterableSee :
... nx.utils.arbitrary_element(iterator) Traceback (most recent call last): ... ValueError: cannot return an arbitrary item from an iterator
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.utils.misc.arbitrary_element
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them