What does this Array split syntax means in python [duplicate] - python

This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 years ago.
(X_train, X_test) = X[50:], X[:50]
How does the split happens here? Please provide the inner explanation.

Take the following example on a simple list:
a = [0, 1, 2, 3, 4, 5, 6, 7]
print(a[:3])
print(a[3:])
Will output
[0, 1, 2]
# [:3] returns everything from the start, to before the third index
[3, 4, 5, 6, 7]
# [3:] returns everything from the third index to the end
Expanded to [50:] and [:50] will return everything from the 50th index to the end and everything from the beginning to before the 50th index respectively.
The second part of your question is about tuple unpacking. If there are the same number of variables to set as there are elements in some collection such as a tuple or list, it'll unpack them. For example:
(a, b) = 42, 9001
print("Val A:", a)
print("Val B:", b)
Will output:
Val A: 42
Val B: 9001
You actually don't even need the () brackets around the variables.
Expanded to your question, it's actually just a simplified, one line version of saying:
X_train = X[50:]
X_test = X[:50]

Assuming X is a list, tuple or an array, the first slice ([50:]) will return all the elements from the 50th element to the end. The second slice ([:50]) will return the first through the 49th element.

Related

Why does 2d index update a list but not 1d?

I am trying to understand indexing in a list. I try:
x= [1,2,3,[4]]
x[0]=[34]
x[3][0]=95
which gives
[1, 2, 3, [95]]
but why is it not:
[34 2, 3, [95]]
?
Edit: apologies my code was:
x= [1,2,3,[4]]
y=list(x)
x[0]=[34]
x[3][0]=95
print (y)
Which gives the results I stated.
In this case, I believe python list mutability doing strange things.
When you call
y = list(x)
It copies each element in the list x over to the list y.
This means it avoids mutability in the first dimension, but because the third element is copied normally under the hood, and it is a list, it remains mutable.
You can find out a bit more in the docs (https://docs.python.org/3/library/stdtypes.html#list)
Also, as others have pointed out, you are setting
x[0] = [34]
Which sets the first element of x to a list, not a number so x would actually be
[ [34], 2, 3, [95] ]
Your example gives does not gives 1 at index 0.
You replace at index 0 the int 1 with a list containing one element of int 34 at index 0.
That will give you:
[[34], 2, 3, [95]]

Indexing and slicing in numpy [duplicate]

This question already has answers here:
Numpy slicing with bound checks
(2 answers)
Closed 2 years ago.
x = np.array([[[1],[2],[3]], [[4],[5],[6]]])
x.shape
(2,3,1)
x[1:3]
array([[[4],
[5],
[6]]])
I am expecting error in x[1:4] because index out bound but it gives output how it is possible.
>>> "hi"[1:500]
'i'
Python will adjust the end of the slice to match then end of the sequence: there are no more entries at indices 2-499, so it just stops at index 1.
>>> "what"[3000:]
''
It will also clamp the beginning of the slice to match the end of the sequence: there are no entries at index 3000, so an empty string is returned.
Same with your case: x[1] == [[4],[5],[6]]], but x[2:3] is an empty sequence, so you got [[[4],[5],[6]]]] + [] == [[[4],[5],[6]]]].
For the interval index it is the behavior of Numpy. If you give a single index (out of range) it will raise error. For Example:
x = np.array([1, 2, 3, 4, 5, 6])
x[7]
If you want to have both raise error and interval indexing you can use Numpy take:
a = [4, 3, 5, 7, 6, 8]
indices = range(3, 7)
np.take(a, indices)
in both the above cases, Numpy will raise error

Meaning of list[-1] in Python

I have a piece of code here that is supposed to return the least common element in a list of elements, ordered by commonality:
def getSingle(arr):
from collections import Counter
c = Counter(arr)
return c.most_common()[-1] # return the least common one -> (key,amounts) tuple
arr1 = [5, 3, 4, 3, 5, 5, 3]
counter = getSingle(arr1)
print (counter[0])
My question is in the significance of the -1 in return c.most_common()[-1]. Changing this value to any other breaks the code as the least common element is no longer returned. So, what does the -1 mean in this context?
One of the neat features of Python lists is that you can index from the end of the list. You can do this by passing a negative number to []. It essentially treats len(array) as the 0th index. So, if you wanted the last element in array, you would call array[-1].
All your return c.most_common()[-1] statement does is call c.most_common and return the last value in the resulting list, which would give you the least common item in that list. Essentially, this line is equivalent to:
temp = c.most_common()
return temp[len(temp) - 1]

Why doesn`t list[:][0] get me the first row of the list? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 6 years ago.
For the following:
list=[[2, 3, 5], [7, 8, 9]]
Why does [list[0][0], list[1][0]] represent the first row ([2, 7]), but the command list[:][0] or list[0:2][0] returns the first column ([2, 3, 5])?
The way I see it list[:][0] should get all the possible values for the first parameter (that is 0 and 1) and 0 for the second, meaning it would return the first row. Instead what it does is return the first column and I can't understand why.
In python, the [a:b:c] syntax creates a new list. That is,
list = [1,2,3]
print(list[:])
is going to print a list, not a value.
Therefore, when you say list[:][0] you are making a copy of the original list (list[:]) and then accessing item 0 within it.
Of course you know, item 0 of the original list (list[0]) is another list.
I think you want:
[sl[0] for sl in list]
Elaboration:
This is called a "comprehension." It is a compact special syntax for generating lists, dicts, and tuples by processing or filtering other iterables. Basically {..}, [..], and (..) with an expression inside involving a for and optionally an if. Naturally, you can have multiples (like [x for x in y for y in z]) of the for, and multiples of the if.
In your case, it's pretty obvious you want a list. So []. You want to make the list by taking the first item from each sublist. So [sl[0] for sl in list].
Here's a more-detailed article: http://carlgroner.me/Python/2011/11/09/An-Introduction-to-List-Comprehensions-in-Python.html

Sorting Function. Explanantion

def my_sort(array):
length_of_array = range(1, len(array))
for i in length_of_array:
value = array[i]
last_value = array[i-1]
if value<last_value:
array[i]=last_value
array[i-1]=value
my_sort(array)
return array
I know what the function does in general. Its a sorting alogarithm.... But i dont know how what each individual part/section does.
Well, I have to say that the best way to understand this is to experiment with it, learn what it is using, and, basically, learn Python. :)
However, I'll go through the lines one-by-one to help:
Define a function named my_sort that accepts one argument named array. The rest of the lines are contained in this function.
Create a range of numbers using range that spans from 1 inclusive to the length of array non-inclusive. Then, assign this range to the variable length_of_array.
Start a for-loop that iterates through the range defined in the preceding line. Furthermore, assign each number returned to the variable i. This for-loop encloses lines 4 through 9.
Create a variable value that is equal to the item returned by indexing array at position i.
Create a variable last_value that is equal to the item returned by indexing array at position i-1.
Test if value is less than last_value. If so, run lines 7 through 9.
Make the i index of array equal last_value.
Make the i-1 index of array equal value.
Rerun my_sort recursively, passing in the argument array.
Return array for this iteration of the recursive function.
When array is finally sorted, the recursion will end and you will be left with array all nice and sorted.
I hope this shed some light on the subject!
I'll see what I can do for you. The code, for reference:
def my_sort(array):
length_of_array = range(1, len(array))
for i in length_of_array:
value = array[i]
last_value = array[i-1]
if value<last_value:
array[i]=last_value
array[i-1]=value
my_sort(array)
return array
def my_sort(array):
A function that takes an array as an argument.
length_of_array = range(1, len(array))
We set the variable length_of_array to a range of numbers that we can iterate over, based on the number of items in array. I assume you know what range does, but if you don't, in short you can iterate over it in the same way you'd iterate over a list. (You could also use xrange() here.)
for i in length_of_array:
value = array[i]
last_value = array[-1]
What we're doing is using the range to indirectly traverse the array because there's the same total of items in each. If we look closely, though, value uses the i as its index, which starts off at 1, so value is actually array[1], and last_value is array[1-1] or array[0].
if value<last_value:
array[i]=last_value
array[i-1]=value
So now we're comparing the values. Let's say we passed in [3, 1, 3, 2, 6, 4]. We're at the first iteration of the loop, so we're essentially saying, if array[1], which is 1, is less than array[0], which is 3, swap them. Of course 1 is less than 3, so swap them we do. But since the code can only compare each item to the previous item, there's no guarantee that array will be properly sorted from lowest to highest. Each iteration could unswap a properly swapped item if the item following it is larger (e.g. [2,5,6,4] will remain the same on the first two iterations -- they will be skipped over by the if test -- but when it hits the third, 6 will swap with 4, which is still wrong). In fact, if we were to finish this out without the call to my_sort(array) directly below it, our original array would evaluate to [1, 3, 2, 3, 4, 6]. Not quite right.
my_sort(array)
So we call my_sort() recursively. What we're basically saying is, if on the first iteration something is wrong, correct it, then pass the new array back to my_sort(). This sounds weird at first, but it works. If the if test was never satisfied at all, that would mean each item in our original list was smaller than the next, which is another way (the computer's way, really) of saying it was sorted in ascending order to begin with. That's the key. So if any list item is smaller than the preceding item, we jerk it one index left. But we don't really know if that's correct -- maybe it needs to go further still. So we have to go back to the beginning and (i.e., call my_sort() again on our newly-minted list), and recheck to see if we should pull it left again. If we can't, the if test fails (each item is smaller than the next) until it hits the next error. On each iteration, this teases the same smaller number leftward by one index until it's in its correct position. This sounds more confusing than it is, so let's just look at the output for each iteration:
[3, 1, 3, 2, 6, 4]
[1, 3, 3, 2, 6, 4]
[1, 3, 2, 3, 6, 4]
[1, 2, 3, 3, 6, 4]
[1, 2, 3, 3, 4, 6]
Are you seeing what's going on? How about if we only look at what's changing on each iteration:
[3, 1, ... # Wrong; swap. Further work ceases; recur (return to beginning with a fresh call to my_sort()).
[1, 3, 3, 2, ... # Wrong; swap. Further work ceases; recur
[1, 3, 2, ... # Wrong; swap. Further work ceases; recur
[1, 2, 3, 3, 6, 4 # Wrong; swap. Further work ceases; recur
[1, 2, 3, 3, 4, 6] # All numbers all smaller than following number; correct.
This allows the function to call itself as many times as it needs to pull a number from the back to the front. Again, each time it's called, it focuses on the first wrong instance, pulling it one left until it puts it in its proper position. Hope that helps! Let me know if you're still having trouble.

Categories

Resources