473,750 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

slices - handy summary

If, like me, you're always forgetting which way around your list/seq
slices need to go then worry no more. Just put my handy "slice
lookupper" (TM) ) on a (large!) PostIt beside your screen and, Hey
Presto! no more tediously typing a 1-9 seq into your interpreter and
then getting a slice just to check what you get.. (Yes you. You know
you do that !) ... Cheers Steve

x = '0123456789'

x[-10: ] 0123456789 x[ 0: ]
x[ -9: ] 123456789 x[ 1: ]
x[ -8: ] 23456789 x[ 2: ]
x[ -7: ] 3456789 x[ 3: ]
x[ -6: ] 456789 x[ 4: ]
x[ -5: ] 56789 x[ 5: ]
x[ -4: ] 6789 x[ 6: ]
x[ -3: ] 789 x[ 7: ]
x[ -2: ] 89 x[ 8: ]
x[ -1: ] 9 x[ 9: ]

x[ :-9 ] 0 x[ :1 ]
x[ :-8 ] 01 x[ :2 ]
x[ :-7 ] 012 x[ :3 ]
x[ :-6 ] 0123 x[ :4 ]
x[ :-5 ] 01234 x[ :5 ]
x[ :-4 ] 012345 x[ :6 ]
x[ :-3 ] 0123456 x[ :7 ]
x[ :-2 ] 01234567 x[ :8 ]
x[ :-1 ] 012345678 x[ :9 ]
0123456789 x[ :10 ]

Dec 13 '06 #1
1 1398

meridian wrote:
If, like me, you're always forgetting which way around your list/seq
slices need to go then worry no more. Just put my handy "slice
lookupper" (TM) ) on a (large!) PostIt beside your screen and, Hey
Presto! no more tediously typing a 1-9 seq into your interpreter and
then getting a slice just to check what you get.. (Yes you. You know
you do that !) ... Cheers Steve
Actually, I don't. I just remember that, for a natural (positive
nonzero) number y:
x[:y] is the first y elements
x[-y:] is the last y elements

As shown here:
>>x = range(5)
x
[0, 1, 2, 3, 4]
>>x[:2]
[0, 1]
>>x[-2:]
[3, 4]
>>>
And I just work it out from there.

Just my method for remembering slices, that happens to work pretty well
for me.
x = '0123456789'

x[-10: ] 0123456789 x[ 0: ]
x[ -9: ] 123456789 x[ 1: ]
x[ -8: ] 23456789 x[ 2: ]
x[ -7: ] 3456789 x[ 3: ]
x[ -6: ] 456789 x[ 4: ]
x[ -5: ] 56789 x[ 5: ]
x[ -4: ] 6789 x[ 6: ]
x[ -3: ] 789 x[ 7: ]
x[ -2: ] 89 x[ 8: ]
x[ -1: ] 9 x[ 9: ]

x[ :-9 ] 0 x[ :1 ]
x[ :-8 ] 01 x[ :2 ]
x[ :-7 ] 012 x[ :3 ]
x[ :-6 ] 0123 x[ :4 ]
x[ :-5 ] 01234 x[ :5 ]
x[ :-4 ] 012345 x[ :6 ]
x[ :-3 ] 0123456 x[ :7 ]
x[ :-2 ] 01234567 x[ :8 ]
x[ :-1 ] 012345678 x[ :9 ]
0123456789 x[ :10 ]
Dec 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
2605
by: David Abrahams | last post by:
Can anyone explain the logic behind the behavior of list slicing with negative strides? For example: >>> print range(10) I found this result very surprising, and would just like to see the rules written down somewhere. Thanks,
29
3405
by: George Sakkis | last post by:
Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that tuples are immutable ? I ended up writing a custom ImmutableSequence class that does this, but I wonder why it is not implemented for tuples. George
1
1315
by: Antoon Pardon | last post by:
I'm for the moment writing two classes. A table, which is like a list, but can start at any integer. A tree which is like a dictionary, but will iterate over the keys in sorted order. The problem is that I would like to implemet slices but, that seems to be impossible with how slices are implemented now.
3
2417
slightlybefuddled
by: slightlybefuddled | last post by:
(Exporting ImageReady slices as CSS rather than tables) apparently means it'll work just fine in Firefox, but do wacky stuff in IE? Can anyone help me figure out why on earth the slices are not showing up in their proper places in IE(7)? The button bar I created is perfectly set up in Firefox, but when I preview in IE, not so much. The slices have weird spaces in between. Why.....? Any help greatly appreciated. Thx.
31
1724
by: Antoon Pardon | last post by:
The following is part of the explanation on slices in the tutorial: The best way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example: +---+---+---+---+---+ | H | e | l | p | A |
5
3837
by: NuberSteve | last post by:
I'm very new to using CSS and also the concept of slices for mouse-overs, and have made my first attempt at using ImageReady to generate slices of a world map. I basically wanted a map that would show various countries appearing to be depressed when "moused-over". To keep it simple at first, I just decided to try two countries. After copying the HTML and JavaScript codes generated by ImageReady into the page I wanted to insert the map into,...
3
1168
by: David | last post by:
That xpairs() generator is nice, but it's not the best possible code What do you mean by best possible? Most efficient? Most readable? And why don't you use islice? eg: def xpairs(seq): len_seq = len(seq) for i, e1 in enumerate(seq):
0
9001
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.