Problem in understanding Python list comprehensions

前端 未结 6 992
情书的邮戳
情书的邮戳 2021-02-03 13:42

What does the last line mean in the following code?

import pickle, urllib                                                                                                 


        
6条回答
  •  梦毁少年i
    2021-02-03 13:52

    The question itself has already been fully answered but I'd like to add that a list comprehension also supports filtering. Your original line

    print "".join([e[1] * e[0] for e in elt])
    

    could, as an example, become

    print "".join([e[1] * e[0] for e in elt if len(e) == 2])
    

    to only operate on items in elt that have two elements.

提交回复
热议问题