How to catch the smallest hints from the list, but only from Dice 0 to 1

Asked

Viewed 55 times

1

z  = [-4, -1, 0, 0, 0]
F1 = [ 9,  1, 1, 0, 18]
F2 = [ 3,  1, 0, 1, 12]

#Pega a posição do menor valor das listas
n_min_z = min(z)
n_min_F1 = min(F1)
n_min_F2 = min(F2)

#Guarda a posição em uma variavel
posZ = z.index(n_min_z)
posF1 = F1.index(n_min_F1)
posF2 = F2.index(n_min_F2)

print(posZ)
print(posF1)
print(posF2)

the smallest of F1 is in index 3, but I wanted the lowest in the indexes 0 and 1, as I do and in the list F2 I want to do the same thing

  • Hello, you can sort the list so that the lowest value is always on Indice 0

  • and that the list has to be in that order is only I can get the lowest value of the indices 0 or 1

  • 5

    I understood better now, make a slice: min(z[:2]) this will compare only the first two indices. Then you repeat for the others.

  • Thank you, It worked!

  • @Williamteixeira If you want to copy the part I mention you and post as answer, you can copy, I withdraw from my reply :)

2 answers

2

One of the ways to do this is to @Williamteixeira said, it would be you slice the sequencia.

Some ways you can do this:

min(z[0:2])
min(z[:2])
min(z[-5:2]) #isso só ira funcionar se a lista tiver 5 elementos, por isso não é recomendavel

By doing this the code will look like this:

z  = [-4, -1, 0, 0, 0]
F1 = [ 9,  1, 1, 0, 18]
F2 = [ 3,  1, 0, 1, 12]

#Pega a posição do menor valor das listas
n_min_z = min(z[:2])#ou alguma das outras opções
n_min_F1 = min(F1[:2])#ou alguma das outras opções
n_min_F2 = min(F2[:2])#ou alguma das outras 

#Guarda a posição em uma variavel
posZ = z.index(n_min_z)
posF1 = F1.index(n_min_F1)
posF2 = F2.index(n_min_F2)

print(posZ)
print(posF1)
print(posF2)

But another way you can do that is with a if, only what code will get bigger:

z  = [-4, -1, 0, 0, 0]
F1 = [ 9,  1, 1, 0, 18]
F2 = [ 3,  1, 0, 1, 12]

#Pega a posição do menor valor das listas

if z[0] > z[1]:
    n_min_z = z[0]
else:
    n_min_z = z[1]
if F1[0] > F1[1]:
    n_min_F1 = F1[0]
else:
    n_min_F1 = F1[1]
if F2[0] > F2[1]:
    n_min_F2 = F2[0]
else:
    n_min_F2 = F2[1]


#Guarda a posição em uma variavel
posZ = z.index(n_min_z)
posF1 = F1.index(n_min_F1)
posF2 = F2.index(n_min_F2)

print(posZ)
print(posF1)
print(posF2)
  • 3

    I think it would be nice if you changed from "slice string" to "slice sequence" to better match reality (glossary).

  • 1

    Ready, edited, very interesting this link.

1


If you only want to evaluate two indices, you don’t need to complicate using language functions, statements, or sequence slices.

To compare two values, just use basic mathematics. With two values, one is the smallest and the other is the largest. If they are equal, it makes no difference.

Also do not need to repeat logic N times for N lists. Use a for.

z  = [-4, -1, 0, 0, 0]
F1 = [ 9,  1, 1, 0, 18]
F2 = [ 3,  1, 0, 1, 12]

for seq in [z, F1, F2]:
    print(0 if seq[0] <= seq[1] else 1)

Browser other questions tagged

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