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',
'8131.903',
'168485.073',
'0.0',
'0.0',
'0.022',
'372.162',
'1123.041',
'1448.424']
Now consider a namedtuple 'Point':
Point = namedtuple('Point', 'usr sys idl wai hiq siq used buff cach free
read writ recv send majpf minpf alloc vmfree')
How do we convert the variable t to a Point? The most obvious (to me anyways..) approach - of just providing the list as a constructor argument - does not work:
In [57]: Point(t)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-57-635019d8b551> in <module>()
----> 1 Point(t)
TypeError: __new__() takes exactly 19 arguments (2 given)
使用Point(*t)
扩大的内容t
作为参数传递给Point
构造。
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.