Posts: 47
Threads: 20
Joined: Dec 2017
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,
Posts: 100
Threads: 3
Joined: Dec 2017
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.
Posts: 2,953
Threads: 48
Joined: Sep 2016
Dec-13-2017, 01:20 PM
(This post was last modified: Dec-13-2017, 01:22 PM by wavic.)
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'
Posts: 47
Threads: 20
Joined: Dec 2017
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,
Posts: 103
Threads: 15
Joined: Nov 2017
Or even better show use the complete set of code you working with, which we can compiler.
Posts: 2,125
Threads: 11
Joined: May 2017
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.
Posts: 5,151
Threads: 396
Joined: Sep 2016
>>> 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:
Posts: 47
Threads: 20
Joined: Dec 2017
(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. :)
|