Displaying certain vectors in a 2D Python list

Asked

Viewed 202 times

3

I have the following situation: - I have a variable called (vector_distances). This variable receives a 2D list with several vectors. - I wish it to be displayed, only vectors whose sum of their indices is less than 100. - I have the code below, but you are not doing what I need.


def funcao_fitness():
    fitness = 0
    i = 0
    vetor_distancias = [[10,20,30,40],[50,60,70,80],[5,6,7,8],[100,200,300,400],[9,15,25,30]]
    for i in vetor_distancias:
        fitness = (fitness + i) if i <= 100
        print (fitness[i])
    return fitness
print (funcao_fitness())

Thanks for your help.

  • The sum of its indices or its values? ex: [1,2,3] = 6 certain?

  • Of their values. Ex. [1,2,3] = 6

  • the fitness will increase this sum if it is less than 100 certain?

  • Yes. Exactly.

2 answers

2


You can do it like this:

def funcao_fitness():
    fitness = []
    vetor_distancias = [[10,20,30,40],[50,60,70,80],[5,6,7,8],[100,200,300,400],[9,15,25,30]]
    for subl in vetor_distancias:
        if sum(subl) <= 100:
            fitness.append(subl)
    return fitness
print funcao_fitness()

Or with list compreension:

def funcao_fitness():
    vetor_distancias = [[10,20,30,40],[50,60,70,80],[5,6,7,8],[100,200,300,400],[9,15,25,30]]
    return [subl for subl in vetor_distancias if sum(subl) <= 100]
print funcao_fitness()

The function will return a list of all sublists whose sum of their values is <= 100

  • This is exactly what you did. But I need instead of the sum, the vectors to be displayed. Ex. instead of returning the sum 205, it returns to me the vectors: [10,20,30,40],[[5,6,7,8] and [9,15,25,30].

  • Edited @Danilo, I think this is it. It will return a list of all sublists whose sum of their values is <= 100

  • Perfect Miguel. Exactly that now. Again, thank you very, very much friend.

  • @Danilo of nothing as to your previous question I made a mistake but I already corrected, go check in case you didn’t notice

  • In the other question I asked, your answer answered me perfectly. But I’ll take a look at your issue now. A big hug. You’ve been helping me so much.

1

Good as I was trying to solve also for study, I decided to post post...

array = [[10,20,30,40],[50,60,70,80],[5,6,7,8],[100,200,300,400],[9,15,25,30]]
result = [i for i in array if sum(i) < 100]
print(result)

The only difference from ( @Miguel’s reply) will be the same result..

Where in the solution presented the return will be:

[[10, 20, 30, 40], [5, 6, 7, 8], [9, 15, 25, 30]]

And the right thing would be:

[[5, 6, 7, 8], [9, 15, 25, 30]]

But it could be a misinterpretation... due to the operator <= where the correct statement is <...

See on Ideone

  • Hello Magic, this is because your condition should be <= 100 and not only < 100, by the code of the question is <= , everything else is fine

  • No man, the statement is clear : "I wish it to be displayed, only the vectors whose sum of its indices are menores que 100"... But as I said may be a difference in interpretations...@Miguel

  • You’re right yes, it’s really different in interpretations. I followed myself by the code posted and assumed it would be <= , "(fitness + i) if i <= 100" , I assumed it really was <=

  • It is sometimes the AP asks something and puts an initial code that contains errors, but only he can state that he wants smaller or if he wants smaller or equal...@Miguel so posted my answer, so that in case any user has the same doubt he analyzes the differences,,,

  • It’s correct too, you did well to post

Browser other questions tagged

You are not signed in. Login or sign up in order to post.