Python Forum
Slicing syntax question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Slicing syntax question
#1
I'm seeing examples of what appears to be slicing, but I can't readily re-create an example.

For example, what does this mean: [:,1]?

What data does something like that work for?

Thanks,
Reply
#2
Your syntax is wrong; there are no commas in the [].

Slices are really convenient ways to create a sublist (or tuple or array) from an existing one.

The syntax is [start:end:step]. The step field is frequently omitted and defaults to 1. If you omit start and end, they default to... the start and end.

>>> l = [1,2,3,4]
>>> l[:]     # create a new list from the old one
[1, 2, 3, 4]
The slice creates a new list:
>>> x = l[:]  
>>> x   # x has the same elements as l
[1, 2, 3, 4]
>>> x.append(5)  # add a new one to x
>>> l        # l doesn't get the new element
[1, 2, 3, 4]    
>>> x           # but x does
[1, 2, 3, 4, 5]
When would you use slices? I use them when parsing formatted text if I know, for example that two fields are the first and last name:
>>> x = "Matthew Doe New York"
>>> y = x.split(' ')
>>> y[:2]
['Matthew', 'Doe']
You can easily reverse a list with the step field:

>>> l
[1, 2, 3, 4]
>>> l[::-1]
[4, 3, 2, 1]
So that's a quick rundown of how to use slices. I wouldn't say that I use them a lot but they are an extremely useful tool to be familiar with.
Reply
#3
First, that comma has no place there.

Slicing... Let we have a string.

my_string = 'I feel good.'

So, being a string it has indices. my_string[0] gives us 'I' because in Python the indexing starts at 0 and 'I' is the first character. my_string[-1] gives us the period at the end of the string. The negative index means that we count backward. -1 is 'd', -2 'o' and -4 is 'g'.

The slicing: string[start :stop ]

Starting index is what you are thinking. The slicing starts from there. The stop index means it stops there but it is exclusive.

my_string[0:6] gives us 'I feel'. Well, if you count the characters they are six ( including space ). But we start from 0 so 0 to 5 is exactly 6 characters.
If we need to slice an iterable from the start we can miss the 0 index. Our example becomes my_string[:6] which gives us again 'I feel'. It is the same if we want to slice to the end. my_string[7:] give us 'good'. [:] is the whole string and [2:6] - 'feel'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Yes, I get all this, but in Pandas and some of the machine learning videos I'm watching, there's definitely a (confusing, to me) use of [:, 1] in some context.

I'll try to get a URL that shows that example.

Thanks,
Reply
#5
Or even better show use the complete set of code you working with, which we can compiler.
Reply
#6
numpy_array[:,1]
What you have seen is a numpy array.
You can use multi dimensional slicing access.

The provided example returns the first elemnt of second dimension for the whole first dimension.

In other words. If The first dimension are the rows and the second dimension are columns. You,ll get only the first column of all rows.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
>>> import numpy as np
>>> a = np.arange(12).reshape(3,4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> a[:,[1,2,3]]
array([[ 1,  2,  3],
       [ 5,  6,  7],
       [ 9, 10, 11]])
>>> a[:, [1,3]]
array([[ 1,  3],
       [ 5,  7],
       [ 9, 11]])
Recommended Tutorials:
Reply
#8
(Dec-13-2017, 04:14 PM)metulburr Wrote:
>>> import numpy as np
>>> a = np.arange(12).reshape(3,4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> a[:,[1,2,3]]
array([[ 1,  2,  3],
       [ 5,  6,  7],
       [ 9, 10, 11]])
>>> a[:, [1,3]]
array([[ 1,  3],
       [ 5,  7],
       [ 9, 11]])

Thank you very much. This is a fine example and helped me understand what's going on. :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020