How to remove the innermost level of nesting in a list of lists of varying lengths

前端 未结 7 1468
感情败类
感情败类 2021-01-13 14:51

I\'m trying to remove the innermost nesting in a list of lists of single element length lists. Do you know a relatively easy way (converting to NumPy arrays is fine) to get

相关标签:
7条回答
  • 2021-01-13 15:29

    As in your case, innermost object has just one element. You may access the value based on index instead of using some additional function. For example:

    >>> [[y[0] for y in x] for x in my_list]
    [[1, 2, 3, 4, 5], [6, 7, 8], [11, 12]]
    

    If there is possibility that your inner-most list may have more than one element, you may do:

    >>> [[z for y in x for z in y] for x in my_list]
    [[1, 2, 3, 4, 5], [6, 7, 8], [11, 12]]
    
    0 讨论(0)
提交回复
热议问题