How to flatten a tuple in python

前端 未结 6 1553
挽巷
挽巷 2020-11-30 13:42

I have the following element of a list, and the list is 100 elements long.

[(50, (2.7387451803816479e-13, 219))]

How do I convert each elem

6条回答
  •  被撕碎了的回忆
    2020-11-30 14:11

    A Python 2.7 compatible way to do what Mitch suggests for Python 3.5.

    >>> example =  [(50, (2.7387451803816479e-13, 219)),
                (100, (3.7387451803816479e-13, 218))]
    >>> [(lambda *x: x)(k, *r) for k, r in example]
    [(50, 2.738745180381648e-13, 219), (100, 3.7387451803816477e-13, 218)]
    

    The advantage of this method is that you do not have to find a variable name for each value of the internal tuple to flatten like in the accepted answer. If there are two or three items, that's not really an issue, but imagine there are tenths values or more...

提交回复
热议问题