Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. string = #extremely large number
  2.  
  3. num = [int(c) for c in string if not c.isspace()]
  4.  
  5. num = []
  6. for c in string:
  7. if not c.isspace():
  8. num.append(int(c))
  9.  
  10. num = [ int( c)
  11.  
  12. "num" shall be a list of: the int created from each c
  13.  
  14. for c in string
  15.  
  16. where c takes on each value found in string
  17.  
  18. if not c .isspace() ]
  19.  
  20. such that it is not the case that c is a space (end of list description)
  21.  
  22. string = 'aVeryLargeNumber'
  23. num = [int(c) for c in string if not c.isspace()] #list comprehension
  24.  
  25. """Breakdown of a list comprehension into it's parts."""
  26. num = [] #creates an empty list
  27. for c in string: #This threw me for a loop when I first started learning
  28. #as everytime I ran into the 'for something in somethingelse':
  29. #the c was always something else. The c is just a place holder
  30. #for a smaller unit in the string (in this example).
  31. #For instance we could also write it as:
  32. #for number in '1234567890':, which is also equivalent to
  33. #for x in '1234567890': or
  34. #for whatever in '1234567890'
  35. #Typically you want to use something descriptive.
  36. #Also, string, does not have to be just a string. It can be anything
  37. #so long as you can iterate (go through it) one item at a time
  38. #such as a list, tuple, dictionary.
  39.  
  40. if not c.isspace(): #in this example it means if c is not a whitespace character
  41. #which is a space, line feed, carriage return, form feed,
  42. #horizontal tab, vertical tab.
  43.  
  44. num.append(int(c)) #This converts the string representation of a number to an actual
  45. #number(technically an integer), and appends it to a list.
  46.  
  47. '1234567890' # our string in this example
  48. num = []
  49. for c in '1234567890':
  50. if not c.isspace():
  51. num.append(int(c))
  52.  
  53. num = [] #our list, empty for now
  54. for '1' in '1234567890':
  55. if not '1'.isspace():
  56. num.append(int('1'))
  57.  
  58. num = [1] #our list, now with a one appended!
  59. for '2' in '1234567890':
  60. if not '2'.isspace():
  61. num.append(int('2'))
  62.  
  63. Traceback (most recent call last):
  64. File "<string>", line 1, in <fragment>
  65. builtins.ValueError: invalid literal for int() with base 10: '.'
  66.  
  67. >>> print [i for i in range(10)]
  68. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  69.  
  70. >>> print [i for i in range(20) if i%2 == 0]
  71. [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
  72.  
  73. >>> nums = [1,2,3,4]
  74. >>> fruit = ["Apples", "Peaches", "Pears", "Bananas"]
  75. >>> print [(i,f) for i in nums for f in fruit]
  76. [(1, 'Apples'), (1, 'Peaches'), (1, 'Pears'), (1, 'Bananas'),
  77. (2, 'Apples'), (2, 'Peaches'), (2, 'Pears'), (2, 'Bananas'),
  78. (3, 'Apples'), (3, 'Peaches'), (3, 'Pears'), (3, 'Bananas'),
  79. (4, 'Apples'), (4, 'Peaches'), (4, 'Pears'), (4, 'Bananas')]
  80. >>> print [(i,f) for i in nums for f in fruit if f[0] == "P"]
  81. [(1, 'Peaches'), (1, 'Pears'),
  82. (2, 'Peaches'), (2, 'Pears'),
  83. (3, 'Peaches'), (3, 'Pears'),
  84. (4, 'Peaches'), (4, 'Pears')]
  85. >>> print [(i,f) for i in nums for f in fruit if f[0] == "P" if i%2 == 1]
  86. [(1, 'Peaches'), (1, 'Pears'), (3, 'Peaches'), (3, 'Pears')]
  87. >>> print [i for i in zip(nums,fruit) if i[0]%2==0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement