Creating a namedtuple from a list

前端 未结 2 1415
北荒
北荒 2021-01-15 01:46

Consider a list variable t

In [55]: t
Out[55]:
[\'1.423\',
 \'0.046\',
 \'98.521\',
 \'0.010\',
 \'0.000\',
 \'0.000\',
 \'5814251520.0\',         


        
2条回答
  •  日久生厌
    2021-01-15 02:26

    More efficient solution: Use the special _make alternate constructor to directly construct the namedtuple from an arbitrary iterable without creating additional intermediate tuples (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).

提交回复
热议问题