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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:52:15+00:00 2026-05-18T09:52:15+00:00

Consider the following code, integral to my questions below: import functools N = 3

  • 0

Consider the following code, integral to my questions below:

import functools

N = 3

class Struct:
    """Create an instance with argument=value slots.
    This is for making a lightweight object whose class doesn't matter."""
    def __init__(self, **entries):
        self.__dict__.update(entries)

    def __repr__(self):

        args = ['%s=%s' % (k, repr(v)) for (k, v) in vars(self).items()]
        return '\nStruct(%s)' % ', '.join(args)

def doit( move ):

    ( rowIn, colIn ) = move
    something = rowIn  + ( 10 * colIn ) # An involved computation here in real life

    return Struct( coord = ( rowIn, colIn ), something = something )

legalStates = [ ( row, col ) for row in xrange( N ) for col in xrange( N ) ] # A more complicated function that generates the list in real life. Call it 'complicatedFunction'

genExpFn = lambda : ( ( s.something, m, s ) for ( m, s ) in ( ( move, doit( move ) ) for move in legalStates ) )  #Q1

successorsSortedGenFn = lambda : ( p for p in sorted( genExpFn(), reverse = True ) )

def bFunc( s, a ):

    #print "a * s ->", a * s
    return a * s # An involved computation here in real life

def aFunc( ( v, m, s ) ): #Q2

    assert( s.something == v )
    return bFunc( s.something, 10 )

print "min( successorsSortedGen ) -> " + str( min( successorsSortedGenFn(), key=functools.partial( aFunc )) ) #Q3
print
print "max( successorsSortedGen ) -> " + str( max( successorsSortedGenFn(), key=functools.partial( aFunc )) ) #Q4

My questions are based on the statements marked as “#Q”:

Q1 : It’s apparent that the generator is completely instantiated ( all elements are executed ), as we call sorted() on it ( which generates all the elements and creates a temporary unsorted list which it sorts and returns as a new list? ).

Is there a space efficient way, that minimizes the creation of temporaries and yeilds a sorted list?

I tried to, but could not write a list comprehension that could sort in place using list.sort()

This was the kind of expression I was thinking about:

successorsSorted = [ ( s.something, m, s ) for ( m, s ) in ( ( move, doit( move ) ) for move in legalStates ) ].sort( reverse = True )

Q2 : Notice that ‘aFunc’ is just a wrapper around ‘bFunc’ because I was not able to write an equivalent representation in the functools.partial( aFunc ) call.

What is the expression ‘aFunc’ in functools.partial( aFunc ) that I am looking for that will allow me to call ‘bFunc’ directly?


EDIT : The answer to Q2 is lambda ( v, m, s ): bFunc(s.something, 10)

Thus, the statements become:

print "min( successorsSortedGen ) -> " + str( min( successorsSortedGenFn(), key=functools.partial( lambda ( v, m, s ): bFunc(s.something, 10)) ) )
print
print "max( successorsSortedGen ) -> " + str( max( successorsSortedGenFn(), key=functools.partial( lambda ( v, m, s ): bFunc(s.something, 10)) ) )

I know it kinda seems lame I did not think about this earlier, but oh well ( thanks to aaronasterling for the gentle prodding ).


Q3, Q4 : Note that the elements passed to min() and max() are already sorted.

Is it possible for me to make this hint to min() and max() so that it does not instantiate the whole list as a temporary and then iterate through the whole list to locate the min or max element?

If not, is there a module or custom function in existence that does not instantiate the whole list, but, given that the list passed to it is sorted, returns the min or max element while inspecting the least number of elements?

  • 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-18T09:52:15+00:00Added an answer on May 18, 2026 at 9:52 am

    Q1. [x for x in somelist].sort() creates a list and calls the sort method. This returns None The assignment assigns None to successorSorted. If you want to do this, you’ll have to implement it yourself and it will probably be way slower than the builtin sort creating the temporary list.

    Q2. You could take apart the code object and rearrange the argument list so that a is the first argument and then rewrite all the bytecode to account for the new positions of the locals. (Yes this can actually be done). You could then use functools.partial on that. Or you could use a wrapper like you’re doing now or in a few other ways. I’m +1 on the wrapper. (though let me know if you want a bytecode hack, I think they’re fun and the cool thing about providing them as answers on Stack Overflow is that I get to write them but don’t have to use them 😉

    Q3, Q4. Not really. To get the tenth element of an iterator, you need to go through all of the earlier ones. If you know that you want the first element, you can just do

    smallest = next(sorted_iterator)
    

    and for the last

    for item in iterable: pass
    largest = item
    

    The first will eat the first element of the iterator and the last will eat your whole iterator. Bye Bye iterator.

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

Sidebar

Related Questions

Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Consider the following code: template<int* a> class base {}; int main() { base<(int*)0> test;
Consider the following code: class Program { static void Main(string[] args) { A a
Consider the following code: A.java: import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @interface A{} C.java: import
Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);
Consider the following code: public class Vehicle { public void StartEngine() { // Code
Consider the following code : HTML: <div id='button' class='enabled'>Press here</div> <div id='log'></div> CSS: #button
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider the following code: void Handler(object o, EventArgs e) { // I swear o

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.