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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:38:43+00:00 2026-05-27T14:38:43+00:00

I wrote this function (used later to select elite species in the genetic algorithm)

  • 0

I wrote this function (used later to select elite species in the genetic algorithm) to select k best values out of n, where not all n values are unique. First of all, I’d massively appreciate any comments to the code, but I’m primarily concerned with the fact that for some reason values in second vector (var2) are also set to 0. If instead of array I use list, this doesn’t happen, but of course I want to use arrays rather than lists! So any comments are very welcome

import numpy

import tkMessageBox


'v1 is the vector of values from which k best must be selected for maximization problems'

class kbest():

    def val_report(self,k,v1):
        n=len(v1);
        v1=numpy.asarray(v1)
        v2=numpy.zeros(k)
        v2=numpy.asarray(v2,int)
        if k>n:
            self.trigger1()
        else:
            l=0; 
            while (l<k):
                best_now=numpy.asarray(numpy.where(v1==max(v1)),int)[0,0]
                v2[l]=best_now;
                v1[best_now]=0
                'v1=numpy.delete(v1, best_now)'
                'print l,v2;'
                l=l+1;
        return v2

    def trigger1(self):
        tkMessageBox.showwarning('Wrong value','Select the correct value')



var1=numpy.asarray(numpy.random.randint(0,100,10));

var2=var1

var3=numpy.asarray([1,2,3,3,3,3,3,4,5,6,7,8,9])

elite=kbest().val_report(3, var1);   

print elite

print var2
  • 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-27T14:38:44+00:00Added an answer on May 27, 2026 at 2:38 pm

    You are just giving var1 the alias var2. They both point to the same content.

    You must copy the content over to a new object.

    In [1]: x = numpy.arange(5)
    
    In [2]: x
    Out[2]: array([0, 1, 2, 3, 4])
    
    In [3]: y = x.copy()
    
    In [4]: x[:] = 0
    
    In [5]: x
    Out[5]: array([0, 0, 0, 0, 0])
    
    In [6]: y
    Out[6]: array([0, 1, 2, 3, 4])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000" .
I wrote this function for filling closed loop, pixvali is declared globally to store
I wrote this function to get the unread count of google reader items. function
I wrote this function to get a pseudo random float between 0 .. 1
I want to convert function object to function. I wrote this code, but it
I have this Array i wrote a function MostFreq that takes an array of
this is somewhat of an odd question. I wrote a C function. Its 'like'
$(function(){ $('a').each(function(){ var x=this.href; this.href=www.somesitename.com/filter+this.href; }); }); i wrote the above jQuery script to
I'm trying to figure out how to write this function: template <typename Bound> Bound::result_type
I used to write this: $('#MyDiv .TheClass').eq(TheIndex).removeClass('ClassA'); $('#MyDiv .TheClass').eq(TheIndex).removeClass('ClassB'); $('#MyDiv .TheClass').eq(TheIndex).addClass('ClassC'); $('#MyDiv .TheClass').eq(TheIndex).children().each(function ()

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.