top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is negative index in Python?

+1 vote
473 views
What is negative index in Python?
posted Dec 3, 2014 by Kali Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Python sequences can be index in positive and negative numbers. For positive index, 0 is the first index, 1 is the second index and so forth. For negative index, (-1) is the last index and (-2) is the second last index and so forth.

answer Dec 3, 2014 by Amit Kumar Pandey
Similar Questions
+1 vote

For example gcd(3, -7) returns -1, which means that a co-prime test that would work in many other languages 'if gcd(x, y) == 1' will fail in Python for negative y.

And, of course, since -|x| is less than |x|, returning -|x| rather than |x| is not returning the greatest common divisor of x and y when y is negative.

0 votes

I have a list of a list of integers. The lists are long so i cant really show an actual example of on of the lists, but I know that they contain only the integers 1,2,3,4.

so for example. s2 = [[1,2,2,3,2,1,4,4],[2,4,3,2,3,1]]

I am calculating the product, sum, max, min.... of each list in s2 but I get negative or 0 for the product for a lot of the lists. (I am doing this in ipython)

for x in s2: Â  Â  print(len = , len(x), sum = , sum(x), prod = , prod(x), max = , max(x), min = , min(x))
...
 (len = , 100, sum = , 247, prod = , 0, max = , 4, min = , 1) (len = , 100, sum = , 230, prod = , -4611686018427387904, max = , 4, min = , 1) (len = , 100, sum = , 261, prod = , 0, max = , 4, min = , 1)
 .....
 (prod =, 0, max =, 4, min =, 1) (prod =, 1729382256910270464, max =, 4, min =, 1) (prod =, 0, max =, 4, min =, 1)
.... 

Whats going on?

0 votes

I have table names in this form:

MY_TABLE
MY_TABLE_CTL
MY_TABLE_DEL
MY_TABLE_RUN
YOUR_TABLE
YOUR_TABLE_CTL
YOUR_TABLE_DEL
YOUR_TABLE_RUN

I am trying to create a regular expression that will return true for only these tables:

MY_TABLE
YOUR_TABLE

I tried these:

pattern = re.compile(r"_(?!(CTL|DEL|RUN))")pattern = re.compile(r"w+(?!(CTL|DEL|RUN))")
pattern = re.compile(r"(?!(CTL|DEL|RUN)$)")

But, both match.I do not need to capture anything.

+1 vote

For example:

a=[-15,-30,-10,1,3,5]

I want to find a negative and a positive minimum.

example: negative
print(min(a)) = -30

positive
print(min(a)) = 1
...