Problem with array in Python

4

I'm starting with Python and I have a problem with the code I'm doing ...

from numpy import *
import random

def busquedaLineal(buscado, arreglo):
    encontrado = 0
for i in range(len(arreglo)):
    if arreglo[i] == buscado:
        encontrado = 1
return encontrado

arreglo = random.sample(range(25), 10)
buscado = random.sample(range(25), 1)

print arreglo
print buscado

buscarElemento = busquedaLineal(buscado, arreglo)

print buscarElemento

if buscarElemento == 1:
   print 'Si se encontro en el arreglo'
 else:
    print 'No se encontro en el arreglo'

the cycle for I have changed it to use only fix or len (fix) but it does not work, for the return data (found) there is always 0 (not found) even if the random number is in the array . Does anyone have any ideas?

First line in the terminal: arrangement (random)

Second line in the terminal: number that we will look for in the array (random)

Third line in the terminal: 0 that returns from the function of búsquedaLineal (not found)

    
asked by Gero 24.05.2017 в 23:54
source

2 answers

1

The reason is very simple, random.sample() returns a list. When you do buscado = random.sample(range(25), 1) you get a list of an element, for example:

  

[2]

What you are doing is comparing each item in your list arreglo (which are integers ) and see if it equals a list This will never give True , because you are comparing a list with an int, when it reaches 17 on your list arreglo the comparison is:

if 17 == [17]:

this is always False , it should be:

if 17 == 17:

Simply unpack the item from the list buscando :

import random

def busquedaLineal(buscado, arreglo):
    encontrado = 0
    for i in range(len(arreglo)):
        if arreglo[i] == buscado:
            encontrado = 1
    return encontrado

arreglo = random.sample(range(25), 10)
buscado = random.sample(range(25), 1)[0] #<<<<<Usamos el indice para obtener el entero

print arreglo
print buscado
buscarElemento = busquedaLineal(buscado, arreglo)

print buscarElemento

if buscarElemento:
    print 'Si se encontro en el arreglo'
else:
    print 'No se encontro en el arreglo'

However, two observations:

  • In Python, do not go through a list or another iterable one using indexes, use for-in , it's much more 'pythonic' and more efficient:

    def busquedaLineal(buscado, arreglo):
        for i in arreglo:
            if i == buscado:
                return 1
        return 0
    

    Notice that I have changed the way to return. What you want to know is if the number is in the array, it is more efficient to return 1 as soon as you find it and not always go through the whole array. This also avoids the use of an intermediate variable ( encontrado ) that is unnecessary, unless you intend to count the times it appears.

  • On the other hand, this already exists in Python implemented efficiently and intuitively: if buscado in arreglo :

    import random
    
    arreglo = random.sample(range(25), 10)
    buscado = random.sample(range(25), 1)[0]
    
    print arreglo
    print buscado
    
    if buscado in arreglo:
        print 'Si se encontro en el arreglo'
    else:
        print 'No se encontro en el arreglo'
    
answered by 25.05.2017 / 00:14
source
0

As a tip, if you start in Python, always take care of the tabulation of your Python programs.

In this case, obtain the value of the element searched in this way:

buscado[0]

since you really need the value of the array buscado to make a correct comparison. ( if arreglo[i] == buscado ):

your code would be:

from numpy import *
import random

def busquedaLineal(buscado, arreglo):
    encontrado = 0
    for i in range(len(arreglo)):
        if arreglo[i] == buscado:
            print "encontro:: ", buscado
            encontrado = 1
    return encontrado

arreglo = random.sample(range(25), 10)
buscado = random.sample(range(25), 1)

buscarElemento = busquedaLineal(buscado[0], arreglo)  # envia el valor de el elemento almacenado en el array buscado.

print "buscarElemento " , buscarElemento

if buscarElemento == 1:
   print 'Si se encontro en el arreglo'
else:
   print 'No se encontro en el arreglo'
    
answered by 25.05.2017 в 00:23