code_junkie

Problem in understanding Python list comprehensions

Nov 14th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. import pickle, urllib
  2.  
  3. handle = urllib.urlopen("http://www.pythonchallenge.com/pc/def/banner.p")
  4. data = pickle.load(handle)
  5. handle.close()
  6.  
  7. for elt in data:
  8. print "".join([e[1] * e[0] for e in elt])
  9.  
  10. print "".join([e[1] * e[0] for e in elt])
  11.  
  12. x = []
  13. for e in elt:
  14. x.append(e[1] * e[0])
  15. print "".join(x)
  16.  
  17. >>> data[0]
  18. [(' ', 95)]
  19.  
  20. >>> ':'.join(['ab', 'cd'])
  21. >>> 'ab:cd'
  22.  
  23. for elt in data:
  24. for e in elt:
  25. print "e[0] == %s, e[1] == %d, which gives us: '%s'" % (e[0], e[1], ''.join(e[1] * e[0]))
  26.  
  27. print "".join([e[1] * e[0] for e in elt])
  28.  
  29. print "".join([e[1] * e[0] for e in elt if len(e) == 2])
Add Comment
Please, Sign In to add comment