.. currentmodule:: cf .. default-role:: obj .. _fs_field_list: The `cf.FieldList` object ========================= A `cf.FieldList` is an ordered sequence of `cf.Field` objects. List-like operators ^^^^^^^^^^^^^^^^^^^ 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 >>> fl [, ] >>> fl.reverse() [, ] >>> fl += fl[-1].copy() [, , ] Selecting fields ^^^^^^^^^^^^^^^^ One or more fields from a field list may be selected with the `~cf.FieldList.select` method that returns a new field list containing the selected fields: >>> fl [, ] >>> fl.select('air_temperature') ] >>> fl.select('[air_temperature|x_wind]') [, ] >>> fl.select('NOTHING') [] Manipulating the fields ^^^^^^^^^^^^^^^^^^^^^^^ 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 [, ] In-place changes to the fields may also be done with a list comprehension: >>> [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: >>> 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 [, ] Sorting the fields ^^^^^^^^^^^^^^^^^^ The field list may be sorted in-place with the `~cf.FieldList.sort` method, that works in a similar way to the `!sort` method of a built-in list. The only difference is that by default the fields are sorted by their identities (e.g their standard names). For example: >>> fl [, , , ] >>> fl.sort() >>> fl [, , , ] >>> fl.sort(reverse=True) >>> fl [, , , ]