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 7633563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:55:14+00:00 2026-05-31T06:55:14+00:00

I’m trying to extend a function I wrote to find the first 3 values

  • 0

I’m trying to extend a function I wrote to find the first 3 values of a dictionary that are “close enough” are also all below a threshold (here N = 70). :

d = {
1: {0: 222, 2:44, 18: 44, 20: 22, 21:72, 105:22, 107:9, 115: 66},
2: {0: 61.0, 993: 65.0, 1133: 84.0, 1069: 48.0, 105:22, 107:9, 115: 24, 214:22, 206:9,       225: 241,412: 83.0, 364: 68.0, 682: 64.0, 172: 58.0} 
}#nested dictionary

def ff(d):
   G = []
   for k,v in sorted(d.iteritems()):
      G.append((k,v))
       #print G
   for i in range(len(G)-2):
      if (G[i+2][0] - G[i][0] < 20) & (G[i][1] <= 70) & (G[i+1][1] <=70) & (G[i+2][1]<=70):
          return i, G[i], G[i+1], G[i+2]

for idnum, ds in sorted(d.iteritems()):
    print ff(ds)

Output:

[(0, 222), (2, 44), (18, 44), (20, 22), (21, 72), (105, 22), (107, 9), (115, 66)]
(1, (2, 44), (18, 44), (20, 22))
[(0, 61.0), (105, 22), (107, 9), (115, 24), (172, 58.0), (206, 9), (214, 22), (225, 241),       (364, 68.0), (412, 83.0), (682, 64.0), (993, 65.0), (1069, 48.0), (1133, 84.0)]
(1, (105, 22), (107, 9), (115, 24)) #first interval fitting criteria

What I’d like to do, is actually find ALL windows of length 20 and keep track of how many values it has <=70. Any thoughts on how to get started would be great. I can’t seem to figure out how to move from the condition using “i”:

if (G[i+2][0] - G[i][0] < 20) & (G[i][1] <= 70) & (G[i+1][1] <=70) & (G[i+2][1]<=70):

to something based on the length 20 and not the indexing??

Ultimately, instead of “the first three” i’d like to keep track of all higher frequency with the minimum being “at least 3 of value <=70, consecutive in order* and in length 20 interval”.

Desired Ouput:

If we have

   d[3] = {0: 61.0, 993: 65.0, 1133: 84.0, 1069: 48.0, 105:22, 107:9, 115: 24, 117:22, 200:100, 225: 241,412: 83.0, 420: 68.0, 423: 64.0, 430: 58.0}

Would result in the output:

[(105, 22), (107, 9), (115, 24),(117,22)], [(420, 68.0),(423,63),(430,58)] 
# These can be of any length as long as the overall interval of the list is <=20. 
  • 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-05-31T06:55:16+00:00Added an answer on May 31, 2026 at 6:55 am

    Here’s something which might help you get started. It’s loop-based, and doesn’t even use zip (much less itertools.takewhile!), but hopefully will make sense:

    def find_windows(d, min_elements=3,upper_length=20,max_value=70):
        G = sorted(d.items())
        for start_index in range(len(G)):
            for width in range(min_elements, len(G)-start_index+1):
                window = G[start_index:start_index+width]
                if not all(v <= max_value for k,v in window):
                    break
                if not window[-1][0] - window[0][0] < upper_length:
                    break
                yield window
    

    I used “break” because as soon as we have any value > max_value or we’re >= upper_length there are no more possible windows starting at start_index.

    If you haven’t seen yield before, it makes the function into a generator function; it’s like a return where the function sends back (yields) the value and then can continue instead of stopping. (See the answers to this question for more information.)

    >>> Ds = {
    ...     1: {0: 222, 2:44, 18: 44, 20: 22, 21:72, 105:22, 107:9, 115: 66},
    ...     2: {0: 61.0, 993: 65.0, 1133: 84.0, 1069: 48.0, 105:22, 107:9, 115: 24, 214:22, 206:9, 225: 241,412: 83.0, 364: 68.0, 682: 64.0, 172: 58.0} 
    ...     }
    >>> 
    >>> for idnum, d in sorted(Ds.items()):
    ...     print idnum, list(find_windows(d))
    ... 
    1 [[(2, 44), (18, 44), (20, 22)], [(105, 22), (107, 9), (115, 66)]]
    2 [[(105, 22), (107, 9), (115, 24)]]
    >>> mydict = dict([(0,55),(1,55),(2,55),(3,55)])
    >>> 
    >>> for window in find_windows(mydict):
    ...     print window
    ... 
    [(0, 55), (1, 55), (2, 55)]
    [(0, 55), (1, 55), (2, 55), (3, 55)]
    [(1, 55), (2, 55), (3, 55)]
    >>> list(find_windows(mydict))
    [[(0, 55), (1, 55), (2, 55)], [(0, 55), (1, 55), (2, 55), (3, 55)], [(1, 55), (2, 55), (3, 55)]]
    

    It’s still not entirely clear to me what you want to do with overlapping windows, but currently it finds them all and you can decide either within the function or in post-processing how you want to handle that.

    Modifying the code to not test for whether all the values are <= max_value and count them instead should be trivial, so I’ll leave that alone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.