How to print the progress of a list comprehension in python?

后端 未结 5 1409
忘掉有多难
忘掉有多难 2021-01-02 23:17

In my method i have to return a list within a list. I would like to have a list comprehension, because of the performance since the list takes about 5 minutes to create.

5条回答
  •  无人及你
    2021-01-02 23:46

    doc_collection = [[1, 2],
                      [3, 4],
                      [5, 6]]
    
    result = [print(progress) or
              [str(token) for token in document]
              for progress, document in enumerate(doc_collection)]
    
    print(result)  # [['1', '2'], ['3', '4'], ['5', '6']]
    

    I don't consider this good or readable code, but the idea is fun.

    It works because print always returns None so print(progress) or x will always be x (by the definition of or).

提交回复
热议问题