Creating A Namedtuple From A List
Consider a list variable t In [55]: t Out[55]: ['1.423', '0.046', '98.521', '0.010', '0.000', '0.000', '5814251520.0', '769945600.0', '18775908352.0', '2.45024350208e+11
Solution 1:
Use Point(*t)
to expand the contents of t
as arguments to the Point
constructor.
Solution 2:
More efficient solution: Use the special _make
alternate constructor to directly construct the namedtuple
from an arbitrary iterable without creating additional intermediate tuple
s (as star-unpacking to the main constructor requires). Runs faster, less memory churn:
Point._make(t)
Despite the name, _make
is part of the public API; it's named with a leading underscore to avoid conflicts with field names (which aren't allowed to begin with an underscore).
Post a Comment for "Creating A Namedtuple From A List"