How do we use 'and' operator in array indexing? [duplicate] - python

This question already has answers here:
How does the logical `and` operator work with integers? [duplicate]
(2 answers)
Python's Logical Operator AND [duplicate]
(7 answers)
Closed 4 years ago.
I found one code that used A[i][j] += min(A[i - 1][j and j - 1:j + 2])
I tried similar implementation as shown below which gave me unexpected results.Can anyone explain me? I am not able to understand how 'and' is interpreted in such situations.
a = [1,2,3,4,5,6,7,8,9,10,11,12]
print(a[0 and 3:4]) #[1, 2, 3, 4]
print(a[1 and 3:4]) #[4]
print(a[1 and 3:6]) #[4, 5, 6]
print(a[0 and 3:6]) #[1, 2, 3, 4, 5, 6]
print(a[2 and 3:6]) #[4, 5, 6]
edit : in a[0 and 3:4] it includes elements from 0 to 3 but if I use a[1 and 3:4] it gives only 3rd element whereas I expect elements from 1 to 3

AND has higher precedence than slice : operator.
Your expressions works as,
a[0 and 3:4] --> a[(0 and 3):4] --> a[0:4] --> [1, 2, 3, 4]

You are simply comparing integer values by "and" in your case - not the values behind the index values. If you compare anything with 0, you get 0. In all the other cases, you get the value of the second integer (Maybe not for every python interpreter).

Related

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

Why, when slicing the list, the number of the items in a list is always more than the ending index? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
Why for example when slicing a list with square brackets in this way
spam = [1, 2, 3]
When we print(spam[0:2]) the 3 is not typed while the index of that is 2?
This style of indexing is just very beautiful and comfortable. There're lots of cases where this can be useful. The easiest one is when you have to do something on two halves of an array:
spam = [1, 2, 3, 4, 5]
center_index = len(spam) / 2
func(spam[center_index:])
func(spam[:center_index])
In this example, you don't have to think about +1/-1
Because Python slicing is inclusive # start and exclusive # the end

What does a pipe character in python slice notation do? [duplicate]

This question already has answers here:
Pipe character in Python
(6 answers)
Closed 3 years ago.
In python slice notation for lists and tuples, I noticed that a pipe character doesn't throw an error. I'm not sure what exactly it does though, as the results seem a little random.
testA = [1,2,3,4,5,6,7,8,9]
Given that
testA[0:3]
gives
[1, 2, 3]
and
testA[4:6]
gives
[5, 6]
What does this pipe do?
testA[0:3|4:6]
it gives
[1, 7]
Any ideas?
testA[0:3|4:6]
evaluates as
testA[0 : (3 | 4) : 6]
Which, in turn, evaluates via bitwise-or to
testA[0 : 7 : 6]
And this corresponds to the range 0 : 7 with a step size of 6. Hence only the first and last index are used.

What are the default values for extended slice with negative and positive step [duplicate]

This question already has answers here:
What are the default slice indices *really*?
(8 answers)
Why does list[::-1] not equal list[:len(list):-1]?
(2 answers)
Closed 5 years ago.
What are the default values of start and stop in list slice? Do they change if the step is positive or negative?
For example:
a = [1,2,3,4,5,6]
print(a[::1])
>>> [1, 2, 3, 4, 5, 6]
Above code makes it look that default value of start and stop are 0 and len(a).
But if we use a step = -1
a = [1,2,3,4,5,6]
print(a[::-1])
>>> [5, 4, 3, 2, 1, 0]
Following python documenttation from https://docs.python.org/3/tutorial/introduction.html#strings
Slice indices have useful defaults; an omitted first index defaults to
zero, an omitted second index defaults to the size of the string being
sliced.
If that is the case we should get an empty list.
a = [1,2,3,4,5,6]
print(a[0:len(a):-1])
>>> []
a = "python"
a[0:len(a):-1]
>>>> ''

slice operator understanding [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
good primer for python slice notation
I am a little confused as to what the slice operator does in python. Can anyone explain to me how it works?
The slice operator is a way to get items from lists, as well as change them. See http://docs.python.org/tutorial/introduction.html#lists.
You can use it to get parts of lists, skipping items, reversing lists, and so on:
>>> a = [1,2,3,4]
>>> a[0:2] # take items 0-2, upper bound noninclusive
[1, 2]
>>> a[0:-1] #take all but the last
[1, 2, 3]
>>> a[1:4]
[2, 3, 4]
>>> a[::-1] # reverse the list
[4, 3, 2, 1]
>>> a[::2] # skip 2
[1, 3]
The first index is where to start, the (optional) second one is where to end, and the (optional) third one is the step.
And yes, this question is a duplicate of Explain Python's slice notation.

Categories

Resources