Problem in understanding Python list comprehensions

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

What does the last line mean in the following code?

import pickle, urllib                                                                                                 


        
6条回答
  •  孤独总比滥情好
    2021-02-03 13:50

    Firstly, you need to put http:// in front of the URL, ie:

    handle = urllib.urlopen("http://www.pythonchallenge.com/pc/def/banner.p")
    

    An expression [e for e in a_list] is a list comprehension which generates a list of values.

    With Python strings, the * operator is used to repeat a string. Try typing in the commands one by one into an interpreter then look at data:

    >>> data[0]
    [(' ', 95)]
    

    This shows us each line of data is a tuple containing two elements.

    Thus the expression e[1] * e[0] is effectively the string in e[0] repeated e[1] times.

    Hence the program draws a banner.

提交回复
热议问题