Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. # LISTS
  2. properties: ordered, iterable, mutable, can contain multiple data types
  3.  
  4. ## create an empty list (two ways)
  5. ```python
  6. empty_list = []
  7. empty_list = list()
  8. ```
  9.  
  10. ## create a list
  11. ```python
  12. simpsons = ['homer', 'marge', 'bart']
  13. ```
  14.  
  15. ## examine a list
  16. ```python
  17. simpsons[0] # print element 0 ('homer')
  18. len(simpsons) # returns the length (3)
  19. ```
  20.  
  21. ## modify a list (does not return the list)
  22. ```python
  23. simpsons.append('lisa') # append element to end
  24. simpsons.extend(['itchy', 'scratchy']) # append multiple elements to end
  25. simpsons.insert(0, 'maggie') # insert element at index 0 (shifts everything right)
  26. simpsons.remove('bart') # searches for first instance and removes it
  27. simpsons.pop(0) # removes element 0 and returns it
  28. del simpsons[0] # removes element 0 (does not return it)
  29. simpsons[0] = 'krusty' # replace element 0
  30. ```
  31.  
  32. ## concatenate lists (slower than 'extend' method)
  33. ```python
  34. neighbors = simpsons + ['ned','rod','todd']
  35. ```
  36.  
  37. ## find elements in a list
  38. ```python
  39. simpsons.count('lisa') # counts the number of instances
  40. simpsons.index('itchy') # returns index of first instance
  41. ```
  42.  
  43. ## list slicing [start:end:stride]
  44. ```python
  45. weekdays = ['mon','tues','wed','thurs','fri']
  46. weekdays[0] # element 0
  47. weekdays[0:3] # elements 0, 1, 2
  48. weekdays[:3] # elements 0, 1, 2
  49. weekdays[3:] # elements 3, 4
  50. weekdays[-1] # last element (element 4)
  51. weekdays[::2] # every 2nd element (0, 2, 4)
  52. weekdays[::-1] # backwards (4, 3, 2, 1, 0)
  53. ```
  54.  
  55. ## alternative method for returning the list backwards
  56. ```python
  57. list(reversed(weekdays))
  58. ```
  59.  
  60. ## sort a list in place (modifies but does not return the list)
  61. ```python
  62. simpsons.sort()
  63. simpsons.sort(reverse=True) # sort in reverse
  64. simpsons.sort(key=len) # sort by a key
  65. ```
  66.  
  67. ## return a sorted list (but does not modify the original list)
  68. ```python
  69. sorted(simpsons)
  70. sorted(simpsons, reverse=True)
  71. sorted(simpsons, key=len)
  72. ```
  73.  
  74. ## insert into an already sorted list, and keep it sorted
  75. ```python
  76. num = [10, 20, 40, 50]
  77. from bisect import insort
  78. insort(num, 30)
  79. ```
  80.  
  81. ## create a second reference to the same list
  82. ```python
  83. same_num = num
  84. same_num[0] = 0 # modifies both 'num' and 'same_num'
  85. ```
  86.  
  87. ## copy a list (two ways)
  88. ```python
  89. new_num = num[:]
  90. new_num = list(num)
  91. ```
  92.  
  93. ## examine objects
  94. ```python
  95. id(num) == id(same_num) # returns True
  96. id(num) == id(new_num) # returns False
  97. num is same_num # returns True
  98. num is new_num # returns False
  99. num == same_num # returns True
  100. num == new_num # returns True (their contents are equivalent)
  101. ```
  102.  
  103.  
  104. ## Selecting Items In A List With Filters
  105. ```python
  106. regimentSize = (5345, 6436, 3453, 2352, 5212, 6232, 2124, 3425, 1200, 1000, 1211);
  107.  
  108. smallRegiments = list(filter((lambda x: x < 2500), regimentSize));
  109. ```
  110.  
  111. # Flatten Lists Of Lists
  112. # Create a list containing three lists of names
  113. ```python
  114. list_of_lists = [['Amy','Betty','Cathryn','Dana'],
  115. ['Elizabeth','Fay','Gora'],
  116. ['Heidi','Jane','Kayley']]
  117. ```
  118. # For each element in list_of_lists, take each element in the list
  119. ```python
  120. flattened_list = [i for row in list_of_lists for i in row]
  121. ```
  122.  
  123.  
  124.  
  125. ## equivalent list comprehension
  126. ```python
  127. items = [item for row in matrix
  128. for item in row] # [1, 2, 3, 4]
  129.  
  130. ```
  131.  
  132. ## Create a list of first names
  133. ```python
  134. first_names = ['Steve', 'Jane', 'Sara', 'Mary','Jack','Bob', 'Bily', 'Boni', 'Chris','Sori', 'Will', 'Won','Li']
  135. ```
  136.  
  137. ## Create a function called "chunks" with two arguments, l and n:
  138. ```python
  139. def chunks(l, n):
  140. # For item i in a range that is a length of l,
  141. for i in range(0, len(l), n):
  142. # Create an index range for l of n items:
  143. yield l[i:i+n]
  144. ```
  145.  
  146. ## Create a list that from the results of the function chunks:
  147. ```python
  148. list(chunks(first_names, 5))
  149. [['Steve', 'Jane', 'Sara', 'Mary', 'Jack'],
  150. ['Bob', 'Bily', 'Boni', 'Chris', 'Sori'],
  151. ['Will', 'Won', 'Li']]
  152. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement