Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.80 KB | None | 0 0
  1. a[start:end] # items start through end-1
  2. a[start:] # items start through the rest of the array
  3. a[:end] # items from the beginning through end-1
  4. a[:] # a copy of the whole array
  5.  
  6. a[start:end:step] # start through not past end, by step
  7.  
  8. a[-1] # last item in the array
  9. a[-2:] # last two items in the array
  10. a[:-2] # everything except the last two items
  11.  
  12. +---+---+---+---+---+
  13. | H | e | l | p | A |
  14. +---+---+---+---+---+
  15. 0 1 2 3 4 5
  16. -5 -4 -3 -2 -1
  17.  
  18. >>> seq[:] # [seq[0], seq[1], ..., seq[-1] ]
  19. >>> seq[low:] # [seq[low], seq[low+1], ..., seq[-1] ]
  20. >>> seq[:high] # [seq[0], seq[1], ..., seq[high-1]]
  21. >>> seq[low:high] # [seq[low], seq[low+1], ..., seq[high-1]]
  22. >>> seq[::stride] # [seq[0], seq[stride], ..., seq[-1] ]
  23. >>> seq[low::stride] # [seq[low], seq[low+stride], ..., seq[-1] ]
  24. >>> seq[:high:stride] # [seq[0], seq[stride], ..., seq[high-1]]
  25. >>> seq[low:high:stride] # [seq[low], seq[low+stride], ..., seq[high-1]]
  26.  
  27. >>> class slicee:
  28. ... def __getitem__(self, item):
  29. ... return `item`
  30. ...
  31. >>> slicee()[0, 1:2, ::5, ...]
  32. '(0, slice(1, 2, None), slice(None, None, 5), Ellipsis)'
  33.  
  34. >>> r=[1,2,3,4]
  35. >>> r[1:1]
  36. []
  37. >>> r[1:1]=[9,8]
  38. >>> r
  39. [1, 9, 8, 2, 3, 4]
  40. >>> r[1:1]=['blah']
  41. >>> r
  42. [1, 'blah', 9, 8, 2, 3, 4]
  43.  
  44. >>> x = [1,2,3,4,5,6]
  45. >>> x[::-1]
  46. [6,5,4,3,2,1]
  47.  
  48. >>> x = [1,2,3,4,5,6]
  49. >>> x[::-2]
  50. [6,4,2]
  51.  
  52. sliceable[start:stop:step]
  53.  
  54. +---+---+---+---+---+---+
  55. | P | y | t | h | o | n |
  56. +---+---+---+---+---+---+
  57. 0 1 2 3 4 5
  58. -6 -5 -4 -3 -2 -1
  59.  
  60. sequence[start:stop:step]
  61.  
  62. my_list[-9:]
  63.  
  64. my_list[-9:None:None]
  65.  
  66. my_list[-9:len(my_list):1]
  67.  
  68. list_copy = sequence[:]
  69.  
  70. del my_list[:]
  71.  
  72. last_nine_slice = slice(-9, None)
  73.  
  74. >>> list(range(100))[last_nine_slice]
  75. [91, 92, 93, 94, 95, 96, 97, 98, 99]
  76.  
  77. >>> length = 100
  78. >>> last_nine_iter = itertools.islice(list(range(length)), length-9, None, 1)
  79. >>> list_last_nine = list(last_nine)
  80. >>> list_last_nine
  81. [91, 92, 93, 94, 95, 96, 97, 98, 99]
  82.  
  83. Python indexes and slices for a six-element list.
  84. Indexes enumerate the elements, slices enumerate the spaces between the elements.
  85.  
  86. Index from rear: -6 -5 -4 -3 -2 -1 a=[0,1,2,3,4,5] a[1:]==[1,2,3,4,5]
  87. Index from front: 0 1 2 3 4 5 len(a)==6 a[:5]==[0,1,2,3,4]
  88. +---+---+---+---+---+---+ a[0]==0 a[:-2]==[0,1,2,3]
  89. | a | b | c | d | e | f | a[5]==5 a[1:2]==[1]
  90. +---+---+---+---+---+---+ a[-1]==5 a[1:-1]==[1,2,3,4]
  91. Slice from front: : 1 2 3 4 5 : a[-2]==4
  92. Slice from rear: : -5 -4 -3 -2 -1 :
  93. b=a[:]
  94. b==[0,1,2,3,4,5] (shallow copy of a)
  95.  
  96. [a:b:c]
  97.  
  98. len = length of string, tuple or list
  99.  
  100. c -- default is +1. The sign of c indicates forward or backward, absolute value of c indicates steps. Default is forward with step size 1. Positive means forward, negative means backward.
  101.  
  102. a -- When c is positive or blank, default is 0. When c is negative, default is -1.
  103.  
  104. b -- When c is positive or blank, default is len. When c is negative, default is -(len+1).
  105.  
  106. In forward direction, starts at 0 and ends at len-1
  107.  
  108. In backward direction, starts at -1 and ends at -len
  109.  
  110. -len, -len+1, -len+2, ..., 0, 1, 2,3,4 , len -1
  111.  
  112. ...,-len -2 ,-len-1,-len, -len+1, -len+2, ..., 0, 1, 2,3,4 , len -1, len, len +1, len+2 , ....
  113.  
  114. 0 1 2 3 4 5 6 7 8 9 10 11
  115. a s t r i n g
  116. -9 -8 -7 -6 -5 -4 -3 -2 -1
  117.  
  118. >>> l1
  119. [2, 3, 4]
  120.  
  121. >>> l1[:]
  122. [2, 3, 4]
  123.  
  124. >>> l1[::-1] # a default is -1 , b default is -(len+1)
  125. [4, 3, 2]
  126.  
  127. >>> l1[:-4:-1] # a default is -1
  128. [4, 3, 2]
  129.  
  130. >>> l1[:-3:-1] # a default is -1
  131. [4, 3]
  132.  
  133. >>> l1[::] # c default is +1, so a default is 0, b default is len
  134. [2, 3, 4]
  135.  
  136. >>> l1[::-1] # c is -1 , so a default is -1 and b default is -(len+1)
  137. [4, 3, 2]
  138.  
  139.  
  140. >>> l1[-100:-200:-1] # Interesting
  141. []
  142.  
  143. >>> l1[-1:-200:-1] # Interesting
  144. [4, 3, 2]
  145.  
  146.  
  147. >>> l1[-1:-1:1]
  148. []
  149.  
  150.  
  151. >>> l1[-1:5:1] # Interesting
  152. [4]
  153.  
  154.  
  155. >>> l1[1:-7:1]
  156. []
  157.  
  158. >>> l1[1:-7:-1] # Interesting
  159. [3, 2]
  160.  
  161. (from:to:step)
  162.  
  163. (:to:step)
  164. (from::step)
  165. (from:to)
  166.  
  167. def range(start=0, stop, step=1): # illegal syntax, but that's the effect
  168. i = start
  169. while (i < stop if step > 0 else i > stop):
  170. yield i
  171. i += step
  172.  
  173. def this_is_how_slicing_works(seq, start=None, stop=None, step=1):
  174. if start is None:
  175. start = (0 if step > 0 else len(seq)-1)
  176. elif start < 0:
  177. start += len(seq)
  178. if not 0 <= start < len(seq): # clip if still outside bounds
  179. start = (0 if step > 0 else len(seq)-1)
  180. if stop is None:
  181. stop = (len(seq) if step > 0 else -1) # really -1, not last element
  182. elif stop < 0:
  183. stop += len(seq)
  184. for i in range(start, stop, step):
  185. if 0 <= i < len(seq):
  186. yield seq[i]
  187.  
  188. mylist[X:Y]
  189.  
  190. a[start:end:step]
  191.  
  192. m[::,0:2:] ## slice the first two columns
  193.  
  194. >>> l=[12,23,345,456,67,7,945,467]
  195.  
  196. >>> l[len(l):-len(l)-1:-1]
  197. [467, 945, 7, 67, 456, 345, 23, 12]
  198.  
  199. >>> l[:-len(l)-1:-1]
  200. [467, 945, 7, 67, 456, 345, 23, 12]
  201.  
  202. >>> l[len(l)::-1]
  203. [467, 945, 7, 67, 456, 345, 23, 12]
  204.  
  205. >>> l[::-1]
  206. [467, 945, 7, 67, 456, 345, 23, 12]
  207.  
  208. >>> l[-1:-len(l)-1:-1]
  209. [467, 945, 7, 67, 456, 345, 23, 12]
  210.  
  211. r = [1, 'blah', 9, 8, 2, 3, 4]
  212. >>> r[1:4] = []
  213. >>> r
  214. [1, 2, 3, 4]
  215.  
  216. >>> items = [0, 1, 2, 3, 4, 5, 6]
  217. >>> a = slice(2, 4)
  218. >>> items[2:4]
  219. [2, 3]
  220. >>> items[a]
  221. [2, 3]
  222. >>> items[a] = [10,11]
  223. >>> items
  224. [0, 1, 10, 11, 4, 5, 6]
  225. >>> del items[a]
  226. >>> items
  227. [0, 1, 4, 5, 6]
  228.  
  229. >>> a = slice(10, 50, 2)
  230. >>> a.start
  231. 10
  232. >>> a.stop
  233. 50
  234. >>> a.step
  235. 2
  236. >>>
  237.  
  238. Index:
  239. ------------>
  240. 0 1 2 3 4
  241. +---+---+---+---+---+
  242. | a | b | c | d | e |
  243. +---+---+---+---+---+
  244. 0 -4 -3 -2 -1
  245. <------------
  246.  
  247. Slice:
  248. <---------------|
  249. |--------------->
  250. : 1 2 3 4 :
  251. +---+---+---+---+---+
  252. | a | b | c | d | e |
  253. +---+---+---+---+---+
  254. : -4 -3 -2 -1 :
  255. |--------------->
  256. <---------------|
  257.  
  258. In [122]: alpha = ['a', 'b', 'c', 'd', 'e', 'f']
  259.  
  260. In [123]: alpha
  261. Out[123]: ['a', 'b', 'c', 'd', 'e', 'f']
  262.  
  263. In [124]: alpha[0]
  264. Out[124]: 'a'
  265.  
  266. In [127]: alpha[0] = 'A'
  267.  
  268. In [128]: alpha
  269. Out[128]: ['A', 'b', 'c', 'd', 'e', 'f']
  270.  
  271. In [129]: alpha[0,1]
  272. ---------------------------------------------------------------------------
  273. TypeError Traceback (most recent call last)
  274. <ipython-input-129-c7eb16585371> in <module>()
  275. ----> 1 alpha[0,1]
  276.  
  277. TypeError: list indices must be integers, not tuple
  278.  
  279. In [130]: alpha[0:1]
  280. Out[130]: ['A']
  281.  
  282. In [131]: alpha[0:1] = 'a'
  283.  
  284. In [132]: alpha
  285. Out[132]: ['a', 'b', 'c', 'd', 'e', 'f']
  286.  
  287. In [133]: alpha[0:2] = ['A', 'B']
  288.  
  289. In [134]: alpha
  290. Out[134]: ['A', 'B', 'c', 'd', 'e', 'f']
  291.  
  292. In [135]: alpha[2:2] = ['x', 'xx']
  293.  
  294. In [136]: alpha
  295. Out[136]: ['A', 'B', 'x', 'xx', 'c', 'd', 'e', 'f']
  296.  
  297. In [137]: alpha = ['a', 'b', 'c', 'd', 'e', 'f']
  298.  
  299. In [142]: alpha[1:5:2]
  300. Out[142]: ['b', 'd']
  301.  
  302. In [143]: alpha[-1:-5:-2]
  303. Out[143]: ['f', 'd']
  304.  
  305. In [144]: alpha[1:5:-2]
  306. Out[144]: []
  307.  
  308. In [145]: alpha[-1:-5:2]
  309. Out[145]: []
  310.  
  311. def py_slice_get_indices_ex(obj, start=None, stop=None, step=None):
  312.  
  313. length = len(obj)
  314.  
  315. if step is None:
  316. step = 1
  317. if step == 0:
  318. raise Exception("Step cannot be zero.")
  319.  
  320. if start is None:
  321. start = 0 if step > 0 else length - 1
  322. else:
  323. if start < 0:
  324. start += length
  325. if start < 0:
  326. start = 0 if step > 0 else -1
  327. if start >= length:
  328. start = length if step > 0 else length - 1
  329.  
  330. if stop is None:
  331. stop = length if step > 0 else -1
  332. else:
  333. if stop < 0:
  334. stop += length
  335. if stop < 0:
  336. stop = 0 if step > 0 else -1
  337. if stop >= length:
  338. stop = length if step > 0 else length - 1
  339.  
  340. if (step < 0 and stop >= start) or (step > 0 and start >= stop):
  341. slice_length = 0
  342. elif step < 0:
  343. slice_length = (stop - start + 1)/(step) + 1
  344. else:
  345. slice_length = (stop - start - 1)/(step) + 1
  346.  
  347. return (start, stop, step, slice_length)
  348.  
  349. In [21]: alpha = ['a', 'b', 'c', 'd', 'e', 'f']
  350.  
  351. In [22]: s = slice(None, None, None)
  352.  
  353. In [23]: s
  354. Out[23]: slice(None, None, None)
  355.  
  356. In [24]: s.indices(len(alpha))
  357. Out[24]: (0, 6, 1)
  358.  
  359. In [25]: range(*s.indices(len(alpha)))
  360. Out[25]: [0, 1, 2, 3, 4, 5]
  361.  
  362. In [26]: s = slice(None, None, -1)
  363.  
  364. In [27]: range(*s.indices(len(alpha)))
  365. Out[27]: [5, 4, 3, 2, 1, 0]
  366.  
  367. In [28]: s = slice(None, 3, -1)
  368.  
  369. In [29]: range(*s.indices(len(alpha)))
  370. Out[29]: [5, 4]
  371.  
  372. variable[number1:number2]
  373.  
  374. for any n, lst = lst[:n] + lst[n:]
  375.  
  376. lst = range(10)
  377. lst[:-42] + lst[-42:] == lst
  378.  
  379. s[start:end:step]
  380.  
  381. # mostly used variations
  382. s[start:end]
  383. s[start:]
  384. s[:end]
  385.  
  386. # step related variations
  387. s[:end:step]
  388. s[start::step]
  389. s[::step]
  390.  
  391. # make a copy
  392. s[:]
  393.  
  394. s[-5:] # start at the 5th index from the end of array,
  395. # thus returns the last 5 elements
  396. s[:-5] # start at index 0, end until the 5th index from end of array,
  397. # thus returns s[0:len(s)-5]
  398.  
  399. s[::-1] # reversed slice
  400. s[len(s)::-1] # same as above, reversed slice
  401. s[0:len(s):-1] # empty list
  402.  
  403. s[:len(s)+5] # same as s[:len(s)]
  404. s[-len(s)-5::] # same as s[0:]
  405. s[len(s)+5::-1] # same as s[len(s)::-1], same as s[::-1]
  406.  
  407. # create our array for demonstration
  408. In [1]: s = [i for i in range(10)]
  409.  
  410. In [2]: s
  411. Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  412.  
  413. In [3]: s[2:] # from index 2 to last index
  414. Out[3]: [2, 3, 4, 5, 6, 7, 8, 9]
  415.  
  416. In [4]: s[:8] # from index 0 up to index 8
  417. Out[4]: [0, 1, 2, 3, 4, 5, 6, 7]
  418.  
  419. In [5]: s[4:7] # from index 4(included) up to index 7(excluded)
  420. Out[5]: [4, 5, 6]
  421.  
  422. In [6]: s[:-2] # up to second last index(negative index)
  423. Out[6]: [0, 1, 2, 3, 4, 5, 6, 7]
  424.  
  425. In [7]: s[-2:] # from second last index(negative index)
  426. Out[7]: [8, 9]
  427.  
  428. In [8]: s[::-1] # from last to first in reverse order(negative step)
  429. Out[8]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
  430.  
  431. In [9]: s[::-2] # all odd numbers in reversed order
  432. Out[9]: [9, 7, 5, 3, 1]
  433.  
  434. In [11]: s[-2::-2] # all even numbers in reversed order
  435. Out[11]: [8, 6, 4, 2, 0]
  436.  
  437. In [12]: s[3:15] # end is out of range, python will set it to len(s)
  438. Out[12]: [3, 4, 5, 6, 7, 8, 9]
  439.  
  440. In [14]: s[5:1] # start > end, return empty list
  441. Out[14]: []
  442.  
  443. In [15]: s[11] # access index 11(greater than len(s)) will raise IndexError
  444. ---------------------------------------------------------------------------
  445. IndexError Traceback (most recent call last)
  446. <ipython-input-15-79ffc22473a3> in <module>()
  447. ----> 1 s[11]
  448.  
  449. IndexError: list index out of range
  450.  
  451. #!/usr/bin/env python
  452.  
  453. def slicegraphical(s, lista):
  454.  
  455. if len(s) > 9:
  456. print """Enter a string of maximum 9 characters,
  457. so the printig would looki nice"""
  458. return 0;
  459. # print " ",
  460. print ' '+'+---' * len(s) +'+'
  461. print ' ',
  462. for letter in s:
  463. print '| {}'.format(letter),
  464. print '|'
  465. print " ",; print '+---' * len(s) +'+'
  466.  
  467. print " ",
  468. for letter in range(len(s) +1):
  469. print '{} '.format(letter),
  470. print ""
  471. for letter in range(-1*(len(s)), 0):
  472. print ' {}'.format(letter),
  473. print ''
  474. print ''
  475.  
  476.  
  477. for triada in lista:
  478. if len(triada) == 3:
  479. if triada[0]==None and triada[1] == None and triada[2] == None:
  480. # 000
  481. print s+'[ : : ]' +' = ', s[triada[0]:triada[1]:triada[2]]
  482. elif triada[0] == None and triada[1] == None and triada[2] != None:
  483. # 001
  484. print s+'[ : :{0:2d} ]'.format(triada[2], '','') +' = ', s[triada[0]:triada[1]:triada[2]]
  485. elif triada[0] == None and triada[1] != None and triada[2] == None:
  486. # 010
  487. print s+'[ :{0:2d} : ]'.format(triada[1]) +' = ', s[triada[0]:triada[1]:triada[2]]
  488. elif triada[0] == None and triada[1] != None and triada[2] != None:
  489. # 011
  490. print s+'[ :{0:2d} :{1:2d} ]'.format(triada[1], triada[2]) +' = ', s[triada[0]:triada[1]:triada[2]]
  491. elif triada[0] != None and triada[1] == None and triada[2] == None:
  492. # 100
  493. print s+'[{0:2d} : : ]'.format(triada[0]) +' = ', s[triada[0]:triada[1]:triada[2]]
  494. elif triada[0] != None and triada[1] == None and triada[2] != None:
  495. # 101
  496. print s+'[{0:2d} : :{1:2d} ]'.format(triada[0], triada[2]) +' = ', s[triada[0]:triada[1]:triada[2]]
  497. elif triada[0] != None and triada[1] != None and triada[2] == None:
  498. # 110
  499. print s+'[{0:2d} :{1:2d} : ]'.format(triada[0], triada[1]) +' = ', s[triada[0]:triada[1]:triada[2]]
  500. elif triada[0] != None and triada[1] != None and triada[2] != None:
  501. # 111
  502. print s+'[{0:2d} :{1:2d} :{2:2d} ]'.format(triada[0], triada[1], triada[2]) +' = ', s[triada[0]:triada[1]:triada[2]]
  503.  
  504. elif len(triada) == 2:
  505. if triada[0] == None and triada[1] == None:
  506. # 00
  507. print s+'[ : ] ' + ' = ', s[triada[0]:triada[1]]
  508. elif triada[0] == None and triada[1] != None:
  509. # 01
  510. print s+'[ :{0:2d} ] '.format(triada[1]) + ' = ', s[triada[0]:triada[1]]
  511. elif triada[0] != None and triada[1] == None:
  512. # 10
  513. print s+'[{0:2d} : ] '.format(triada[0]) + ' = ', s[triada[0]:triada[1]]
  514. elif triada[0] != None and triada[1] != None:
  515. # 11
  516. print s+'[{0:2d} :{1:2d} ] '.format(triada[0],triada[1]) + ' = ', s[triada[0]:triada[1]]
  517.  
  518. elif len(triada) == 1:
  519. print s+'[{0:2d} ] '.format(triada[0]) + ' = ', s[triada[0]]
  520.  
  521.  
  522. if __name__ == '__main__':
  523. # Change "s" to what ever string you like, make it 9 characters for
  524. # better representation.
  525. s = 'COMPUTERS'
  526.  
  527. # add to this list different lists to experement with indexes
  528. # to represent ex. s[::], use s[None, None,None], otherwise you get an error
  529. # for s[2:] use s[2:None]
  530.  
  531. lista = [[4,7],[2,5,2],[-5,1,-1],[4],[-4,-6,-1], [2,-3,1],[2,-3,-1], [None,None,-1],[-5,None],[-5,0,-1],[-5,None,-1],[-1,1,-2]]
  532.  
  533. slicegraphical(s, lista)
  534.  
  535. +---+---+---+---+---+---+---+---+---+
  536. | C | O | M | P | U | T | E | R | S |
  537. +---+---+---+---+---+---+---+---+---+
  538. 0 1 2 3 4 5 6 7 8 9
  539. -9 -8 -7 -6 -5 -4 -3 -2 -1
  540.  
  541. COMPUTERS[ 4 : 7 ] = UTE
  542. COMPUTERS[ 2 : 5 : 2 ] = MU
  543. COMPUTERS[-5 : 1 :-1 ] = UPM
  544. COMPUTERS[ 4 ] = U
  545. COMPUTERS[-4 :-6 :-1 ] = TU
  546. COMPUTERS[ 2 :-3 : 1 ] = MPUT
  547. COMPUTERS[ 2 :-3 :-1 ] =
  548. COMPUTERS[ : :-1 ] = SRETUPMOC
  549. COMPUTERS[-5 : ] = UTERS
  550. COMPUTERS[-5 : 0 :-1 ] = UPMO
  551. COMPUTERS[-5 : :-1 ] = UPMOC
  552. COMPUTERS[-1 : 1 :-2 ] = SEUM
  553. [Finished in 0.9s]
  554.  
  555. >>> a
  556. array([[ 1, 2, 3, 4],
  557. [ 5, 6, 7, 8],
  558. [ 9, 10, 11, 12]])
  559. >>> a[:2,0:3:2]
  560. array([[1, 3],
  561. [5, 7]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement