[問題] slice

作者: tsaiminghan (tsaiminghan)   2020-10-15 21:49:03
請問在step為-1的情況下,取start:stop應該怎麼看才對?
aa=['a', 'b', 'c']
print (aa[::-1]) # ['c', 'b', 'a']
print (aa[1::-1]) # ['b', 'a']
print (aa[2::-1]) # ['c', 'b', 'a']
print (aa[:1:-1]) # ['c']
作者: papple23g (逆道者)   2020-10-15 22:00:00
aa[j:i:-1]==aa[i+1:j+1][::-1]
作者: s0914714 (YA)   2020-10-16 01:09:00
aa[::-1]可以轉換成aa[len(aa)-1:-1:-1]所以變成aa[2:-1:-1] 代表從index=2 往左取2-(-1)個元素其餘依此類推
作者: cuteSquirrel (松鼠)   2020-10-16 13:05:00
起點,終點(不包含),位移(-為逆向)
作者: tsaiminghan (tsaiminghan)   2020-10-16 21:04:00
謝謝

Links booklink

Contact Us: admin [ a t ] ucptt.com