Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Python: List Slices


  • Please log in to reply
4 replies to this topic

#1 techgnostic

techgnostic

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:59 PM

Posted 06 May 2015 - 10:21 AM

Okay, here is the correct code.
 
suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

first  = suitcase[0:2]  # The first and second items (index zero and one)
middle = suitcase[2:4]  # Third and fourth items (index two and three)
last   = suitcase[4:6]  # The last two items (index four and five)
 
Now, the comments after each slice are misleading to me.  "Index zero and one" == [0:2]?  Those two aren't the same.  First slice is supposed to be "sunglasses" and "hat".  "Sunglasses" and "hat" are at [0:1], not at [0:2].  
 
What logic am I missing?


BC AdBot (Login to Remove)

 


#2 AndroidOS

AndroidOS

    Malware Search++ developer


  • Security Developer
  • 146 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:10:59 PM

Posted 06 May 2015 - 11:16 AM

Hi there techgnostic,

 

In python, the end value (assuming suitcase[beginning:end]) is the index of the first item not in that slice. So in reality:

 

suitcase[0:2]

 

means "get the value of suitcase[0] and suitcase[1]". And:

 

suitcase[0:end]

 

means get the value of suitcase[0] TO AND INCLUDING suitcase[end-1].

 

Does that make any sense?



#3 techgnostic

techgnostic
  • Topic Starter

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:59 PM

Posted 06 May 2015 - 12:43 PM

Right, that much I gathered.  What I'm wondering is: is there a reason for this counter-intuitive method?  This seems to me to be terribly illogical (if you're picking the last number on the outside, why not the first number?  Why not [-1:2] when you really mean [0:1]?).  

 

I grasp things better when I understand the why.  And logic dictates, to me, that if you want [0:1], you represent this by typing [0:1], not by typing [0:2].  Or if the rule were to list the numbers you don't want on the outside like bread, you'd use both ends, not just the trailing one.

 

P.S.  Hello and thanks again!


Edited by techgnostic, 06 May 2015 - 12:43 PM.


#4 AndroidOS

AndroidOS

    Malware Search++ developer


  • Security Developer
  • 146 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:10:59 PM

Posted 06 May 2015 - 12:53 PM

I can't take the credit for this explanation as it comes from the Python website, but hopefully it helps.

 

Think of the numbers as representing the points in between list items. For example:

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

So from this, we can see that [0:1] would give us the item between 0 and 1 - in this case, "P".


Edited by AndroidOS, 06 May 2015 - 12:54 PM.


#5 techgnostic

techgnostic
  • Topic Starter

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:59 PM

Posted 06 May 2015 - 01:03 PM

Ah, now I get it.  That graphic definitely helped.  Thanks a lot!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users