Python - Python sqllite full row search ?? o al menos un dict.sort[key1,key2]

 
Vista:
sin imagen de perfil

Python sqllite full row search ?? o al menos un dict.sort[key1,key2]

Publicado por perro (13 intervenciones) el 18/02/2022 00:36:10
hola buenos dias

primera pregunta sqllite
dada una tabla_terceros de una base de datos de sqllite que contiene los nombres y los apellidos de los clientes
keys = ["id", "apellido1", "apellido2", "nombre1", "nombre2","type", "telf", "dir" ];


nesecito poder hacer busquedas por apellido y nombre en cualquier columna de forma de que si no hay una coincidencia exacta me devuelva las rows que mas se acerquen a lo que le estoy pidiendo
o se que si search = "daniel martines" cosa que no esta en la tabla al menos me devuelva las rows que contengan las palabras martines y daniel en cualquier columna

####
debe poderse hacer con sqllite pero no di palos con bolas con el concat



segunda pregunta sort()

lsta = [
{ "index": 1, "match": "17", "row_str": "abc" },
{ "index": 2, "match": "3", "row_str": "bccd" },
{ "index": 3, "match": "-1", "row_str": "xyz" },
{ "index": 4, "match": "-1", "row_str": "alfaalberto" },
{ "index": 5, "match": "0", "row_str": "julian na" },
{ "index": 6, "match": "3", "row_str": "adxu" },
{ "index": 7, "match": "3", "row_str": "zuuu" },
{ "index": 8, "match": "3", "row_str": "c378" },
]

##lista.sort()

lista.sort(key=operator.itemgetter('match',row_str)) ## esto no funciona
print(lista)

en este caso nesecito que me ordena los componentes de la lista basado en en campo "match"
y en el caso de que los "match" sean iguales decida el orden alfabeticamente basado en el valor de "row_str"

de ser posible hacer que tambien ignore los dics donde match sea menor que zero

en javascrip yo lo hago con

lista.sort(function (a, b) {
if (a["match"] == b["match"]) {
return ('' + a["row_str"]).localeCompare(b["row_str"]);

} else {
return a["match"] - b["match"];
}
});


me pueden ayudar con alguno de estos dos preguntas ??
bueno muchas gracias























nesecito hacer algo ya mas complicado que ya tengo hecho en javscript pero no encuentro como configurar aqui el sort para que haga lo que quiero

nesecito hacer una full row search sobre una tabla de una base de datos
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Python sqllite full row search ?? o al menos un dict.sort[key1,key2]

Publicado por perro (13 intervenciones) el 18/02/2022 15:32:47
para el caso dos pude hacer esto
me daba unproblema porque me organizaba alfabeticamente la columna match esa columna es de numeros enteros
me toco crear una funcion solo para que me arregle los numeros
debe haber un forma de arreglar esta lista sin dar tanta vuelta

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def fix_int(str_in ):
 
    if not str_in or str_in == None:
        return 0
 
    str_ = str_in
    str_ = re.sub(r"[^0-9/-]", "", str_)
    negative = bool(re.match( "^\-" , str_))
    str_ = re.sub(r"[^0-9]", "", str_)
 
    if negative :
        str_ = "-0" + str_
    else:
        str_ = "0" + str_
 
    int_ = int(str_)
 
    print( ">{}< => >{}<".format(str_in, int_) )
 
    return int_

lista = [
{ "index": 1, "match": "27", "row_str": "abc" },
{ "index": 2, "match": "18", "row_str": "bccd" },
{ "index": 3, "match": "-1", "row_str": "xyz" },
{ "index": 4, "match": "-1", "row_str": "alfaalberto" },
{ "index": 5, "match": "0", "row_str": "julian na" },
{ "index": 6, "match": "3", "row_str": "adxu" },
{ "index": 7, "match": "3", "row_str": "zuuu" },
{ "index": 8, "match": "3", "row_str": "c378" },
{ "index": 9, "match": "", "row_str": "dddd" },
{ "index": 10, "match": "alfaromero", "row_str": "eeee" },
{ "index": 11, "match": "09", "row_str": "bbAA" },
{ "index": 12, "match": "1", "row_str": "DDba" },
{ "index": 12, "match": "- perrson 7 ", "row_str": "DDba" },
{ "index": 13, "match": "argetham - 7 ", "row_str": "DDba" },
{ "index": 14, "match": "argetham 7 - ", "row_str": "DDba" },
{ "index": 15, "match": "-0", "row_str": "gggg" },
]

import operator
listb = sorted(lista, key=lambda x: ( fix_int(x['match']), x['row_str']))

for dic_ in listb:
print(dic_)

print("-----\n")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
----- ----- ----- -----
 
>27< => >27<
>18< => >18<
>-1< => >-1<
>-1< => >-1<
>0< => >0<
>3< => >3<
>3< => >3<
>3< => >3<
>alfaromero< => >0<
>09< => >9<
>1< => >1<
>- perrson 7  < => >-7<
>argetham - 7  < => >-7<
>argetham 7 -  < => >7<
>-0< => >0<
{'index': 12, 'match': '- perrson 7  ', 'row_str': 'DDba'}
{'index': 13, 'match': 'argetham - 7  ', 'row_str': 'DDba'}
{'index': 4, 'match': '-1', 'row_str': 'alfaalberto'}
{'index': 3, 'match': '-1', 'row_str': 'xyz'}
{'index': 9, 'match': '', 'row_str': 'dddd'}
{'index': 10, 'match': 'alfaromero', 'row_str': 'eeee'}
{'index': 15, 'match': '-0', 'row_str': 'gggg'}
{'index': 5, 'match': '0', 'row_str': 'julian na'}
{'index': 12, 'match': '1', 'row_str': 'DDba'}
{'index': 6, 'match': '3', 'row_str': 'adxu'}
{'index': 8, 'match': '3', 'row_str': 'c378'}
{'index': 7, 'match': '3', 'row_str': 'zuuu'}
{'index': 14, 'match': 'argetham 7 -  ', 'row_str': 'DDba'}
{'index': 11, 'match': '09', 'row_str': 'bbAA'}
{'index': 2, 'match': '18', 'row_str': 'bccd'}
{'index': 1, 'match': '27', 'row_str': 'abc'}
-----
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar