python - Comprendre la notation des tranches

Mots clés : pythonlistsliceiterablepython

meilleur 5 Réponses python - Comprendre la notation des tranches

vote vote

91

a[start:stop]  # items start through stop-1 a[start:]      # items start through the rest of the array a[:stop]       # items from the beginning through stop-1 a[:]           # a copy of the whole array 
a[start:stop:step] # start through not past stop, by step 
a[-1]    # last item in the array a[-2:]   # last two items in the array a[:-2]   # everything except the last two items 
a[::-1]    # all items in the array, reversed a[1::-1]   # the first two items, reversed a[:-3:-1]  # the last two items, reversed a[-3::-1]  # everything except the last two items, reversed 
a[start:stop:step] 
a[slice(start, stop, step)] 
vote vote

82

 +---+---+---+---+---+---+  | P | y | t | h | o | n |  +---+---+---+---+---+---+  0   1   2   3   4   5   6 -6  -5  -4  -3  -2  -1 
vote vote

72

>>> seq[:]                # [seq[0],   seq[1],          ..., seq[-1]    ] >>> seq[low:]             # [seq[low], seq[low+1],      ..., seq[-1]    ] >>> seq[:high]            # [seq[0],   seq[1],          ..., seq[high-1]] >>> seq[low:high]         # [seq[low], seq[low+1],      ..., seq[high-1]] >>> seq[::stride]         # [seq[0],   seq[stride],     ..., seq[-1]    ] >>> seq[low::stride]      # [seq[low], seq[low+stride], ..., seq[-1]    ] >>> seq[:high:stride]     # [seq[0],   seq[stride],     ..., seq[high-1]] >>> seq[low:high:stride]  # [seq[low], seq[low+stride], ..., seq[high-1]] 
>>> seq[::-stride]        # [seq[-1],   seq[-1-stride],   ..., seq[0]    ] >>> seq[high::-stride]    # [seq[high], seq[high-stride], ..., seq[0]    ] >>> seq[:low:-stride]     # [seq[-1],   seq[-1-stride],   ..., seq[low+1]] >>> seq[high:low:-stride] # [seq[high], seq[high-stride], ..., seq[low+1]] 
>>> class slicee: ...     def __getitem__(self, item): ...         return repr(item) ... >>> slicee()[0, 1:2, ::5, ...] '(0, slice(1, 2, None), slice(None, None, 5), Ellipsis)' 
vote vote

68

                +---+---+---+---+---+---+                 | P | y | t | h | o | n |                 +---+---+---+---+---+---+ Slice position: 0   1   2   3   4   5   6 Index position:   0   1   2   3   4   5  >>> p = ['P','y','t','h','o','n'] # Why the two sets of numbers: # indexing gives items, not lists >>> p[0]  'P' >>> p[5]  'n'  # Slicing gives lists >>> p[0:1]  ['P'] >>> p[0:2]  ['P','y'] 
>>> p[5] # the last of six items, indexed from zero  'n' >>> p[0:5] # does NOT include the last item!  ['P','y','t','h','o'] >>> p[0:6] # not p[0:5]!!!  ['P','y','t','h','o','n'] 
>>> p[0:4] # Start at the beginning and count out 4 items  ['P','y','t','h'] >>> p[1:4] # Take one item off the front  ['y','t','h'] >>> p[2:4] # Take two items off the front  ['t','h'] # etc. 
>>> p[2:3]  ['t'] >>> p[2:3] = ['T'] >>> p  ['P','y','T','h','o','n'] >>> p[2:3] = 't' Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: can only assign an iterable 
>>> p[2:4]  ['T','h'] >>> p[2:4] = ['t','r'] >>> p  ['P','y','t','r','o','n'] 
>>> p = ['P','y','t','h','o','n'] # Start over >>> p[2:4] = ['s','p','a','m'] >>> p  ['P','y','s','p','a','m','o','n'] 
>>> p = ['P','y','t','h','o','n'] >>> p[0:4]  ['P','y','t','h'] >>> p[1:4]  ['y','t','h'] >>> p[2:4]  ['t','h'] >>> p[3:4]  ['h'] >>> p[4:4]  [] 
>>> p = ['P','y','t','h','o','n'] >>> p[2:4] = ['x','y'] # Assigned list is same length as slice >>> p  ['P','y','x','y','o','n'] # Result is same length >>> p = ['P','y','t','h','o','n'] >>> p[3:4] = ['x','y'] # Assigned list is longer than slice >>> p  ['P','y','t','x','y','o','n'] # The result is longer >>> p = ['P','y','t','h','o','n'] >>> p[4:4] = ['x','y'] >>> p  ['P','y','t','h','x','y','o','n'] # The result is longer still 
>>> p = ['P','y','t','h','o','n'] >>> p[0:4]  ['P','y','t','h'] >>> p[1:4]  ['y','t','h'] >>> p[2:4]  ['t','h'] >>> p[3:4]  ['h'] >>> p[4:4]  [] >>> p[5:4]  [] >>> p[6:4]  [] 
>>> p[5:3:-1]  ['n','o'] 
>>> p[4:4]  [] >>> p[5:4]  [] >>> p[6:4]  [] >>> p[6] Traceback (most recent call last):   File "<stdin>", line 1, in <module> IndexError: list index out of range 
>>> p[100:200]  [] >>> p[int(2e99):int(1e99)]  [] 
>>> p  ['P', 'y', 't', 'h', 'o', 'n'] >>> p[int(2e99):int(1e99)] = ['p','o','w','e','r'] >>> p  ['P', 'y', 't', 'h', 'o', 'n', 'p', 'o', 'w', 'e', 'r'] 
>>> r=[1,2,3,4] >>> r[1:1] [] >>> r[1:1]=[9,8] >>> r [1, 9, 8, 2, 3, 4] >>> r[1:1]=['blah'] >>> r [1, 'blah', 9, 8, 2, 3, 4] 
vote vote

52

sliceable[start:stop:step] 
 +---+---+---+---+---+---+  | P | y | t | h | o | n |  +---+---+---+---+---+---+    0   1   2   3   4   5    -6  -5  -4  -3  -2  -1 
sequence[start:stop:step] 
my_list[-9:] 
my_list[-9:None:None] 
my_list[-9:len(my_list):1] 
list_copy = sequence[:] 
del my_list[:] 
 step_is_negative = step_sign < 0; 
if (step_is_negative) {     lower = PyLong_FromLong(-1L);     if (lower == NULL)         goto error;      upper = PyNumber_Add(length, lower);     if (upper == NULL)         goto error; } 
else {     lower = _PyLong_Zero;     Py_INCREF(lower);     upper = length;     Py_INCREF(upper); } 
if (self->start == Py_None) {     start = step_is_negative ? upper : lower;     Py_INCREF(start); } 
if (self->stop == Py_None) {     stop = step_is_negative ? lower : upper;     Py_INCREF(stop); } 
last_nine_slice = slice(-9, None) 
>>> list(range(100))[last_nine_slice] [91, 92, 93, 94, 95, 96, 97, 98, 99] 
>>> range(100)[last_nine_slice] range(91, 100) 
length = 100 last_nine_iter = itertools.islice(list(range(length)), length-9, None, 1) list_last_nine = list(last_nine_iter) 
>>> list_last_nine [91, 92, 93, 94, 95, 96, 97, 98, 99] 

Questions similaires