Temarad
Temarad

Reputation: 13

Check if 2 values are in a nested list and print the values of the list

I ask for an Identification number, example(6373383-D) and a pin. I have a nested list like this:

cliente = [[0, "Carlos", "Perez Rodriguez", "123342344-D", 4, "ES6621000418401234567891", 1234, 3000, "No"], 
[1, "Ana", "Martin Perez", "19786444-E", 7, "ES6000491500051234567892", 6789, 8000, "No"], 
[2, "Maria", "Sanchez Agudo", "67893456-A", 2, "ES9420805801101234567891", 1111, 10000, "No"], 
[3, "Paco", "Sainz Sanchez", "56782341-B", 2, "ES9000246912501234567891", 9876, 2345, "No"],
[4, "Marcos", "Calvo Rodriguez", "56783452-D", 9, "ES7100302053091234567895", 3456, 8000, "No"]]

If he puts for example 123342344-D and 1234 in PIN i have to show the values of that list.

The code I have is:

    #coding=utf-8
cliente=[["Carlos", "Perez Rodriguez", "123342344-D", 4, "ES6621000418401234567891", 1234, 3000, "No"], ["Ana", "Martin Perez", "19786444-E", 7, "ES6000491500051234567892", 6789, 8000, "No"], ["Maria", "Sanchez Agudo", "67893456-A", 2, "ES9420805801101234567891", 1111, 10000, "No"], ["Paco", "Sainz Sanchez", "56782341-B", 2, "ES9000246912501234567891", 9876, 2345, "No"], ["Marcos", "Calvo Rodriguez", "56783452-D", 9, "ES7100302053091234567895", 3456, 8000, "No"] ]
def clientes(): 
    respuesta=True
    while respuesta:
        print ("Ha elegido la opcion clientes, por favor, identifiquese usando DNI y PIN: ")
        dni = input("DNI: ")
        pin = int(input("PIN: "))
        if dni in (dni[2] for dni in cliente):
            if pin in (pin[5] for pin in cliente):
                for r in cliente:
                    if dni == cliente[r][3] and pin == cliente[r][6]:
                        print("*-----------------------------------------------------------------*\n")
                        print("\nNombre: "+ cliente[r][0])
                        print("\nApellidos: "+ cliente[r][1])
                        print("\nDNI: "+ cliente[r][2])
                        print("\nAntiguedad: "+ cliente[r][3])
                        print("\nCuenta IBAN: "+ cliente[r][4])
                        print("\nPIN: "+ cliente[r][5])
                        print("\nBalance: "+ cliente[r][6])
                        print("\nTiene oferta: "+ cliente[r][7])
                        print("\n*-----------------------------------------------------------------*")
        else:
            print("El cliente no está en la lista")

I want something similar, we never used dynamic and I´m not allowed

Upvotes: 0

Views: 70

Answers (3)

Louis Caron
Louis Caron

Reputation: 1332

You can do:

res = [i for i in cliente if i[3]=='123342344-D' and i[6] == 1234]
print(repr(res[0]) if len(res) else 'Not found')

Upvotes: 0

hamza tuna
hamza tuna

Reputation: 1497

You can write dynamic filter function:

filter_func = lambda id,pin: lambda row: (row[3]==id) and (row[6]==pin )
list(filter(filter_func("123342344-D", 1234), cliente))

Results:

[[0, 'Carlos', 'Perez Rodriguez', '123342344-D', 4, 'ES6621000418401234567891', 1234, 3000, 'No']]

Upvotes: 0

Arkistarvh Kltzuonstev
Arkistarvh Kltzuonstev

Reputation: 6935

Considering that the fourth item of any sublist of cliente would contain identification number and seventh item of the sublists contain the pin, you can print the sublists which contain those items like this :

print([k for k in cliente if (k[3]=='123342344-D' and k[6] == 1234)])

or if the sublists are not ordered then try :

print([k for k in cliente if ('123342344-D' in k and 1234 in k)])

Upvotes: 3

Related Questions