Python: List comprehension list of lists

前端 未结 2 1890
无人及你
无人及你 2020-12-13 12:51

I have a list of lists, and would like to use list comprehension to apply a function to each element in the list of lists, but when I do this, I end up with one long list ra

相关标签:
2条回答
  • 2020-12-13 13:46

    Use this:

    [[number+1 for number in group] for group in x]
    

    Or use this if you know map:

    [map(lambda x:x+1 ,group) for group in x]
    
    0 讨论(0)
  • 2020-12-13 13:46
    lista = [[i+3*(j-1) for i in range(1,4)] for j in range(1,4)]
    
    print(lista)
    # outputs [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    
    0 讨论(0)
提交回复
热议问题