cf.flat

cf.flat(x)[source]

Return an iterator over an arbitrarily nested sequence.

Parameters:
x : scalar or arbitrarily nested sequence

The arbitrarily nested sequence to be flattened. Note that a If x is a string or a scalar then this is equivalent to passing a single element sequence containing x.

Returns:
out : generator

An iterator over flattened sequence.

Examples:
>>> print cf.flat([1, [2, [3, 4]]])
<generator object flat at 0x3649cd0>
>>> print list(cf.flat([1, (2, [3, 4])]))
[1, 2, 3, 4]
>>> import numpy
>>> print list(cf.flat((1, [2, numpy.array([[3, 4], [5, 6]])]))
[1, 2, 3, 4, 5, 6]
>>> for a in cf.flat([1, [2, [3, 4]]]):
...     print a,
1 2 3 4
>>> for a in cf.flat(['a', ['bc', ['def', 'ghij']]]):
...     print a, ' ',
a bc def ghij
>>> for a in cf.flat(2004):
...     print a
2004
>>> for a in cf.flat('abcdefghij'):
...     print a
abcdefghij
>>> f
<CF Field: eastward_wind(air_pressure(5), latitude(110), longitude(106)) m s-1>
>>> for a in cf.flat(f):
...     print repr(a)
<CF Field: eastward_wind(air_pressure(5), latitude(110), longitude(106)) m s-1>
>>> for a in cf.flat([f, [f, [f, f]]]):
...     print repr(a)
<CF Field: eastward_wind(air_pressure(5), latitude(110), longitude(106)) m s-1>
<CF Field: eastward_wind(air_pressure(5), latitude(110), longitude(106)) m s-1>
<CF Field: eastward_wind(air_pressure(5), latitude(110), longitude(106)) m s-1>
<CF Field: eastward_wind(air_pressure(5), latitude(110), longitude(106)) m s-1>
>>> fl = cf.FieldList(cf.flat([f, [f, [f, f]]])
>>> fl
[<CF Field: eastward_wind(air_pressure(5), latitude(110), longitude(106)) m s-1>,
 <CF Field: eastward_wind(air_pressure(5), latitude(110), longitude(106)) m s-1>,
 <CF Field: eastward_wind(air_pressure(5), latitude(110), longitude(106)) m s-1>,
 <CF Field: eastward_wind(air_pressure(5), latitude(110), longitude(106)) m s-1>]

Previous topic

cf.dirname

Next topic

cf.pathjoin

This Page