Home > Enterprise >  How can I print a part of a list depending on various conditions?
How can I print a part of a list depending on various conditions?

Time:01-06

I have a csv file that I want 2 conditions to be met

I have 2 lists Height(Estatura): [1.60,1.98,1.70,1.611.80,1.72 ...]

Age(Edad): [6,10,15,78,23,21...]

this is a part the csv :

enter image description here

this is my code i can read csv :

def abrir_csv(Doc):
    with open(Doc, encoding='utf-8') as f:
        lector = csv.reader(f)
        next(lector)
        contenido = [Almacenamiento_de_datos(Tipo_de_sangre, Nombres, Genero, Cuerpo, Distrito, DENUNCIADO,
                                             float(Estatura), int(Edad), Fecha) for
                     Tipo_de_sangre, Nombres, Genero, Cuerpo, Distrito, DENUNCIADO, Estatura, Edad, Fecha in
                     lector]
        return contenido

I want a part of the height list where it starts at between 1.60 and 1.90 but starting from the age of 15 to 30 years and try print this code :

def MAXEDAD(Doc): #Valor Maximo y Minimo
    Estatura = {s.Estatura > 1.65 <= 1.80 for s, x in Doc if x.Edad > 15 < 30}
    A = [sorted(Estatura)]
    return A

but i have a error:

    Estatura = {s.Estatura > 1.65 <= 1.80 for s, x in Doc if x.Edad > 15 < 30}
ValueError: too many values to unpack (expected 2)

How can print fulfilling various conditionals. thanks

CodePudding user response:

If you're using csv.reader() to read the CSV, you need to assign to enough variables to get each field, not just two fields. The fields are just strings, not objects with attributes. You could also use csv.DictReader() to convert the rows to dictionaries whose keys are the field names from the header row.

If you want to test multiple conditions, use and to combine them. And you need to convert the values to numbers before comparing them to numbers.

def MAXEDAD(Doc): #Valor Maximo y Minimo
    Estatura = [estatura for tipo, nombres, genero, cuerpo, distrito, denuncia, estatua, eda, fecha in Doc 
        if 1.65 < float(estatura) < 1.80 and 15 < int(edad)  < 30]
    return sorted(Estatura)

There's no need to put a list around sorted(Estatura)

  •  Tags:  
  • Related