Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8695607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:04:54+00:00 2026-06-13T01:04:54+00:00

I have an array of integers derived from a gradient of a line. The

  • 0

I have an array of integers derived from a gradient of a line. The array is called sign_slope and looks like this:

sign_slope = array([-1, 1, -1, ..., -1, -1, -1])

I am searching for the cases whereby consecutive items within the array are: 1, -1
For example you can see from the above output of sign_slope that:
sign_slope[1] = 1 and sign_slope[2] = -1
This would be the first case of many that I would like to detect item/index numbers for. I would like the code to output an array or list of index numbers corresponding to the (n-1)’th index i.e. sign_slope[1] in the above case.
I have written the below printing statement which appears to work. However, I do not know how to output the index numbers rather than the values as currently is the case and append them to a list or input them into an array.

for n in range(0, len(sign_slope)):
    if sign_slope[n] < 0 and sign_slope[n - 1] > 0:
        print sign_slope[n - 1]
    else:
        print 0

Thanks is advance,

Kane

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:04:54+00:00Added an answer on June 13, 2026 at 1:04 am

    Looping over a range of indicies is generally considered very unpythonic. It reads very poorly and masks what you are really trying to do. As such, a better solution to finding your sublist is to loop through using the enumerate() builtin to get the indicies alongside the values. We can also provide a much more generic solution, and make it a generator which makes it easy to use.

    def find_sublists(seq, sublist):
        length = len(sublist)
        for index, value in enumerate(seq):
            if value == sublist[0] and seq[index:index+length] == sublist:
                yield index
    

    What we do here is loop through the list, taking the indicies where the value matches the beginning of our sublist. We then check to see if that segment of the list matches our sublist, if it does, we yield the index. This allows us to quickly find all matching sublists in a list.

    We can then use this like so, using the list builtin to create a list from the generator:

    >>> list(find_sublists([-1, 1, -1, -1, -1, 1, -1], [1, -1]))
    [1, 5]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of integers from [0 .. 255]. I'd like to modify
I have an array of Integers in Java, I would like use only a
I have an array of integers which represent a RGB image and would like
I have an array of integers like these; dim x as integer()={10,9,4,7,6,8,3}. Now I
I have an array with integers of values from 0 to 100. I wish
Possible Duplicate: Cumulative array sum in Ruby I have an array of integers like
I can't figure this interview question. You have an array of integers. You need
Say I have an array of n integers, m , each chosen randomly from
Say I have a dynamic array like: int* integers = new int[100]; Is there
This is the basic problem: I have an array of integers with possibly duplicate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.