.. currentmodule:: cf .. default-role:: obj .. _fs_field_list: Introduction to the `cf.FieldList` object ========================================= A `cf.FieldList` object is an ordered sequence of `cf.Field` objects. It supports all of the :ref:`python list-like operations `. For example: >>> fl [, ] >>> fl[0] >>> fl[::-1] [, ] >>> fl[slice(1, -1, 2)] [] Note that an indexing by an integer returns an individual field, but other types of index always return a field list. >>> len(fl) 2 >>> f = fl.pop() >>> f >>> len(fl) 1 >>> fl.append(f.copy()) >>> len(fl) 2 >>> f in fl True >>> from operator import attrgetter >>> fl [, ] >>> fl.sort(key=attrgetter('standard_name')) [, ] >>> fl += fl[-1].copy() [, , ] Selecting fields ^^^^^^^^^^^^^^^^ One or more fields from a field list may be selected with the `~cf.FieldList.select` method. This returns a single field or a new field list, depending on how many fields are selected. For example. >>> fl [, ] >>> fl.select('air_temperature') ] >>> fl.select('[air_temperature|x_wind]') [, ] Manipulating the field elements ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ For in-place changes, a :ref:`for loop ` may be used to process each field element. For example to reverse the data array axis order of each field in-place: >>> fl [, ] >>> for f in fl: ... f.transpose(i=True) ... >>> fl [, ] For changes which result in new fields, a for loop or :ref:`list comprehension ` may be used: >>> new_fl = cf.FieldList(f.round() for f in fl)) >>> new_fl = cf.FieldList() >>> for f in fl: ... new_fl.append(f[0:3]) ... >>> new_fl [, ] In-place changes to the fields may also be done with a list comprehension (at the expense of creating a temporary list): >>> [f.transpose(i=True) for f in fl] For changes which result in new fields, a for loop or :ref:`list comprehension ` may be used: