Guest User

Untitled

a guest
Nov 19th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.72 KB | None | 0 0
  1. for tup in somelist:
  2. if determine(tup):
  3. code_to_remove_tup
  4.  
  5. somelist = [x for x in somelist if not determine(x)]
  6.  
  7. somelist[:] = [x for x in somelist if not determine(x)]
  8.  
  9. from itertools import ifilterfalse
  10. somelist[:] = ifilterfalse(determine, somelist)
  11.  
  12. from itertools import filterfalse
  13. somelist[:] = filterfalse(determine, somelist)
  14.  
  15. somelist[:] = [tup for tup in somelist if determine(tup)]
  16.  
  17. for tup in somelist[:]:
  18. etc....
  19.  
  20. >>> somelist = range(10)
  21. >>> for x in somelist:
  22. ... somelist.remove(x)
  23. >>> somelist
  24. [1, 3, 5, 7, 9]
  25.  
  26. >>> somelist = range(10)
  27. >>> for x in somelist[:]:
  28. ... somelist.remove(x)
  29. >>> somelist
  30. []
  31.  
  32. for i in xrange(len(somelist) - 1, -1, -1):
  33. if some_condition(somelist, i):
  34. del somelist[i]
  35.  
  36. somelist = [tup for tup in somelist if determine(tup)]
  37.  
  38. newlist = []
  39. for tup in somelist:
  40. # lots of code here, possibly setting things up for calling determine
  41. if determine(tup):
  42. newlist.append(tup)
  43. somelist = newlist
  44.  
  45. for tup in somelist[:]:
  46. # lots of code here, possibly setting things up for calling determine
  47. if determine(tup):
  48. newlist.append(tup)
  49.  
  50. somelist[:] = filter(lambda tup: not determine(tup), somelist)
  51.  
  52. from itertools import ifilterfalse
  53. somelist[:] = list(ifilterfalse(determine, somelist))
  54.  
  55. >>> for w in words[:]: # Loop over a slice copy of the entire list.
  56. ... if len(w) > 6:
  57. ... words.insert(0, w)
  58. ...
  59. >>> words
  60. ['defenestrate', 'cat', 'window', 'defenestrate']
  61.  
  62. for x in a[:]:
  63. if x < 0: a.remove(x)
  64.  
  65. for item in originalList:
  66. if (item != badValue):
  67. newList.append(item)
  68.  
  69. originalList[:] = newList
  70.  
  71. array = [lots of stuff]
  72. arraySize = len(array)
  73. i = 0
  74. while i < arraySize:
  75. if someTest(array[i]):
  76. del array[i]
  77. arraySize -= 1
  78. else:
  79. i += 1
  80.  
  81. >>> L1 = [(1,2), (5,6), (-1,-2), (1,-2)]
  82. >>> for (a,b) in L1:
  83. ... if a < 0 or b < 0:
  84. ... L1.remove(a,b)
  85. ...
  86. Traceback (most recent call last):
  87. File "<stdin>", line 3, in <module>
  88. TypeError: remove() takes exactly one argument (2 given)
  89.  
  90. >>> L1[1]
  91. (5, 6)
  92. >>> type(L1[1])
  93. <type 'tuple'>
  94.  
  95. # The remove line now includes an extra "()" to make a tuple out of "a,b"
  96. L1.remove((a,b))
  97.  
  98. L1 is now: [(1, 2), (5, 6), (1, -2)]
  99.  
  100. L1 = [(1,2),(5,6),(-1,-2),(1,-2),(3,4),(5,7),(-4,4),(2,1),(-3,-3),(5,-1),(0,6)]
  101. ### Outputs:
  102. L1 is now: [(1, 2), (5, 6), (1, -2), (3, 4), (5, 7), (2, 1), (5, -1), (0, 6)]
  103.  
  104. L2 = L1
  105. for (a,b) in L1:
  106. if a < 0 or b < 0 :
  107. L2.remove((a,b))
  108. # Now, remove the original copy of L1 and replace with L2
  109. print L2 is L1
  110. del L1
  111. L1 = L2; del L2
  112. print ("L1 is now: ", L1)
  113.  
  114. 'L1 is now: ', [(1, 2), (5, 6), (1, -2), (3, 4), (5, 7), (2, 1), (5, -1), (0, 6)]
  115.  
  116. >>> L2=L1
  117. >>> L1 is L2
  118. True
  119.  
  120. import copy
  121. L1 = [(1,2), (5,6),(-1,-2), (1,-2),(3,4),(5,7),(-4,4),(2,1),(-3,-3),(5,-1),(0,6)]
  122. L2 = copy.copy(L1)
  123. for (a,b) in L1:
  124. if a < 0 or b < 0 :
  125. L2.remove((a,b))
  126. # Now, remove the original copy of L1 and replace with L2
  127. del L1
  128. L1 = L2; del L2
  129. >>> L1 is now: [(1, 2), (5, 6), (3, 4), (5, 7), (2, 1), (0, 6)]
  130.  
  131. L1 = [(1,2), (5,6),(-1,-2), (1,-2),(3,4),(5,7),(-4,4),(2,1),(-3,-3),(5,-1),(0,6)]
  132. for (a,b) in reversed(L1):
  133. if a < 0 or b < 0 :
  134. L1.remove((a,b))
  135. print ("L1 is now: ", L1)
  136. >>> L1 is now: [(1, 2), (5, 6), (3, 4), (5, 7), (2, 1), (0, 6)]
  137.  
  138. inlist = [{'field1':10, 'field2':20}, {'field1':30, 'field2':15}]
  139. for idx, i in enumerate(inlist):
  140. do some stuff with i['field1']
  141. if somecondition:
  142. xlist.append(idx)
  143. for i in reversed(xlist): del inlist[i]
  144.  
  145. list_len = len(some_list)
  146. for i in range(list_len):
  147. reverse_i = list_len - 1 - i
  148. cur = some_list[reverse_i]
  149.  
  150. # some logic with cur element
  151.  
  152. if some_condition:
  153. some_list.pop(reverse_i)
  154.  
  155. k = range(5)
  156. v = ['a','b','c','d','e']
  157. d = {key:val for key,val in zip(k, v)}
  158.  
  159. print d
  160. for i in range(5):
  161. print d[i]
  162. d.pop(i)
  163. print d
  164.  
  165. from fluidIter import FluidIterable
  166. fSomeList = FluidIterable(someList)
  167. for tup in fSomeList:
  168. if determine(tup):
  169. # remove 'tup' without "breaking" the iteration
  170. fSomeList.remove(tup)
  171. # tup has also been removed from 'someList'
  172. # as well as 'fSomeList'
  173.  
  174. from fluidIter import FluidIterable
  175. l = [0,1,2,3,4,5,6,7,8]
  176. fluidL = FluidIterable(l)
  177. for i in fluidL:
  178. print('initial state of list on this iteration: ' + str(fluidL))
  179. print('current iteration value: ' + str(i))
  180. print('popped value: ' + str(fluidL.pop(2)))
  181. print(' ')
  182.  
  183. print('Final List Value: ' + str(l))
  184.  
  185. initial state of list on this iteration: [0, 1, 2, 3, 4, 5, 6, 7, 8]
  186. current iteration value: 0
  187. popped value: 2
  188.  
  189. initial state of list on this iteration: [0, 1, 3, 4, 5, 6, 7, 8]
  190. current iteration value: 1
  191. popped value: 3
  192.  
  193. initial state of list on this iteration: [0, 1, 4, 5, 6, 7, 8]
  194. current iteration value: 4
  195. popped value: 4
  196.  
  197. initial state of list on this iteration: [0, 1, 5, 6, 7, 8]
  198. current iteration value: 5
  199. popped value: 5
  200.  
  201. initial state of list on this iteration: [0, 1, 6, 7, 8]
  202. current iteration value: 6
  203. popped value: 6
  204.  
  205. initial state of list on this iteration: [0, 1, 7, 8]
  206. current iteration value: 7
  207. popped value: 7
  208.  
  209. initial state of list on this iteration: [0, 1, 8]
  210. current iteration value: 8
  211. popped value: 8
  212.  
  213. Final List Value: [0, 1]
  214.  
  215. fluidL[2] = 'a' # is OK
  216. fluidL = [0, 1, 'a', 3, 4, 5, 6, 7, 8] # is not OK
  217.  
  218. fluidArr = FluidIterable([0,1,2,3])
  219. # get iterator first so can query the current index
  220. fluidArrIter = fluidArr.__iter__()
  221. for i, v in enumerate(fluidArrIter):
  222. print('enum: ', i)
  223. print('current val: ', v)
  224. print('current ind: ', fluidArrIter.currentIndex)
  225. print(fluidArr)
  226. fluidArr.insert(0,'a')
  227. print(' ')
  228.  
  229. print('Final List Value: ' + str(fluidArr))
  230.  
  231. enum: 0
  232. current val: 0
  233. current ind: 0
  234. [0, 1, 2, 3]
  235.  
  236. enum: 1
  237. current val: 1
  238. current ind: 2
  239. ['a', 0, 1, 2, 3]
  240.  
  241. enum: 2
  242. current val: 2
  243. current ind: 4
  244. ['a', 'a', 0, 1, 2, 3]
  245.  
  246. enum: 3
  247. current val: 3
  248. current ind: 6
  249. ['a', 'a', 'a', 0, 1, 2, 3]
  250.  
  251. Final List Value: ['a', 'a', 'a', 'a', 0, 1, 2, 3]
  252.  
  253. originalList = fluidArr.fixedIterable
  254.  
  255. newList = [i for i in oldList if testFunc(i)]
  256.  
  257. randInts = [70, 20, 61, 80, 54, 18, 7, 18, 55, 9]
  258. fRandInts = FluidIterable(randInts)
  259. fRandIntsIter = fRandInts.__iter__()
  260. # for each value in the list (outer loop)
  261. # test against every other value in the list (inner loop)
  262. for i in fRandIntsIter:
  263. print(' ')
  264. print('outer val: ', i)
  265. innerIntsIter = fRandInts.__iter__()
  266. for j in innerIntsIter:
  267. innerIndex = innerIntsIter.currentIndex
  268. # skip the element that the outloop is currently on
  269. # because we don't want to test a value against itself
  270. if not innerIndex == fRandIntsIter.currentIndex:
  271. # if the test element, j, is a multiple
  272. # of the reference element, i, then remove 'j'
  273. if j%i == 0:
  274. print('remove val: ', j)
  275. # remove element in place, without breaking the
  276. # iteration of either loop
  277. del fRandInts[innerIndex]
  278. # end if multiple, then remove
  279. # end if not the same value as outer loop
  280. # end inner loop
  281. # end outerloop
  282.  
  283. print('')
  284. print('final list: ', randInts)
  285.  
  286. outer val: 70
  287.  
  288. outer val: 20
  289. remove val: 80
  290.  
  291. outer val: 61
  292.  
  293. outer val: 54
  294.  
  295. outer val: 18
  296. remove val: 54
  297. remove val: 18
  298.  
  299. outer val: 7
  300. remove val: 70
  301.  
  302. outer val: 55
  303.  
  304. outer val: 9
  305. remove val: 18
  306.  
  307. final list: [20, 61, 7, 55, 9]
  308.  
  309. alist = ['good', 'bad', 'good', 'bad', 'good']
  310. i = 0
  311. for x in alist[:]:
  312. if x == 'bad':
  313. alist.pop(i)
  314. i -= 1
  315. else:
  316. # do something cool with x or just print x
  317. print x
  318. i += 1
  319.  
  320. somelist = [x for x in somelist if not determine(x)]
  321.  
  322. for i, item in enumerate(my_list):
  323. if condition:
  324. my_list.pop(i)
Add Comment
Please, Sign In to add comment