How to flatten a tuple in python

前端 未结 6 1528
挽巷
挽巷 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:03

    You can get the result in this way

    >> example =  [(50, (2.7387451803816479e-13, 219))]
    >>> [tuple(x[:1]) + (x[1]) for x in example] 
    [(50, 2.738745180381648e-13, 219)]
    
    0 讨论(0)
  • 2020-11-30 14:06

    Your could use the following function and apply it in a loop to every element in the list.

    import type
    
    def flatten(T):
        if type(T) != types.TupleType: return (T,)
        elif len(T) == 0: return ()
        else: return flatten(T[0]) + flatten(T[1:])
    

    How it works:

    • First it will be checked if type is tuple, if not, it "tuples" the argument
    • Second line returns an empty tuple, if tuple is empty
    • Third line gets out the first element and calls the function recursively

    The nice thing in this solution is:

    • It is not necessary to know the structure of the given tuple
    • The tuple can be nested arbitrarily deep
    • Works in Python 2.7

    The code is slightly adapted from following source:
    https://mail.python.org/pipermail/tutor/2001-April/005025.html

    Hope it helps someone :)

    0 讨论(0)
  • 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...

    0 讨论(0)
  • 2020-11-30 14:12
    [(a, b, c) for a, (b, c) in l]
    

    Tuple packing and unpacking solves the problem.

    0 讨论(0)
  • 2020-11-30 14:18

    An improvement from @sagacity answer, this will rerun a generator that flattens the tuple using a recursive and yield.

    def flatten_tuple(inp):
        for inp2 in inp:
            if not isinstance(inp2,tuple):
                yield inp2
            elif len(inp2) == 0:
                continue
            else:
                yield from flatten_tuple(inp2)
    

    To make it into list or tuple, use list() or tuple().

    list(flatten_tuple(nested_tuple))
    tuple(flatten_tuple(nested_tuple))
    
    0 讨论(0)
  • 2020-11-30 14:22

    New in Python 3.5 with the additional tuple unpacking introduced in PEP 448, you can use starred expressions in tuple literals such that you can use

    >>> l = [(50, (2.7387451803816479e-13, 219)), (40, (3.4587451803816479e-13, 220))]
    >>> [(a, *rest) for a, rest in l]
    [(50, 2.738745180381648e-13, 219), (40, 3.458745180381648e-13, 220)]
    

    This could be useful if you had a nested tuple used for record-keeping with many elements that you wanted to flatten.

    0 讨论(0)
提交回复
热议问题