考虑一个列表变量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']


现在考虑一个命名元组“ Point”:

Point = namedtuple('Point', 'usr sys idl wai hiq siq  used  buff  cach  free
    read  writ recv  send majpf minpf alloc  vmfree')


我们如何将变量t转换为Point?最明显的方法(无论如何对我来说都是如此)-仅将列表提供为构造函数参数-不起作用:

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构造函数的参数。

关于python - 从列表创建一个namedtuple,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30610827/

10-12 16:58
查看更多