Type of list elements - Python

Asked

Viewed 495 times

0

I have two lists of different sizes. The first is a list of lists. Where I need to compare the type of elements of this first list list with another list containing data type (str, int, float,..).

Example:

lista1 = [['a', 'bb', 1.5, 3, 'a'],['h', 'oo', 9.6, 2, 'n'],...,['u', 'pp', 2.9, 1, 'j']]

lista2 = ['str', 'str', 'float', 'int', 'str']

I’m doing like this:

for linha in range(0,68):
    for linha2 in range(0,11):
        if(type(lista1[linha2][linha]) != lista2[linha]):
            print("Alguma coisa")

But it’s not working, because it returns:

int, <class 'int'>

So you don’t recognize it as equal, but as different. How can I?

2 answers

2

Look buddy, I did this one in a hurry :

lista1 = [['a', 'bb', 1.5, 3, 'a'],['h', 'oo', 9.6, 2, 'n'],['u', 'pp', 2.9, 1, 'j']]

lista2 = ['str', 'float', 'int']

for linha in lista1:
    for linha2 in range(len(linha)):
        tipo = str(type(linha[linha2])).replace("<class '",'') # Retiro [ <class ' ]
        tipo = tipo.replace("'>",'') #retiro [ '> ], ficando só o tipo [ str, float ou int ]
        if tipo in lista2: # Se tiveer outro tipo além de str, float ou int, ele não será exibido.
            print(str(linha[linha2]).ljust(3),'=', tipo)

exit :

a   = str
bb  = str
1.5 = float
3   = int
a   = str
h   = str
oo  = str
9.6 = float
2   = int
n   = str
u   = str
pp  = str
2.9 = float
1   = int
j   = str

1


As the Lista2 comes filled with the names of data types of your database we can not do direct conversion, we will have to create a dictionary that will make the "de->to" of the database types for the Python types.

See how it would look:

lista1 = [['a', 'bb', 1.5, 3, 'a'],['h', 'oo', 9.6, 2, 'n'],...,['u', 'pp', 2.9, 1, 'j']]

# Adequando a lista2 para a situação
# lista2 = ['str', 'str', 'float', 'int', 'str']
lista2 = ['char', 'varchar', 'float', 'int', 'char']

# Dicionário de "de->para" do label que vem do banco para o tipo em Python 
tipo = {'int': int, 'nvarchar': str, 'varchar': str, 'char': str, 'decimal': float, 'decimal': float, 'float': float}

for linha in range(68): # quando o range começa com 0 não é preciso declará-lo
    for linha2 in range(11):

        # Estou criando as variáveis aqui para deixar o IF mais limpo
        typeLista1 = type(lista1[linha2][linha])
        typeLista2 = tipo[lista2[linha]] 

        if typeLista1 is not typeLista2:
            print("Alguma coisa")

I just changed your own example to demonstrate my idea, if you want to further detail your need I can change my code to bring it closer to your real need.

Hug.

  • The first list comes from a database and the second comes from an Excel. What I am doing is a Data Quality, so I need to validate if the type of the database data is the same as they should be (which comes from excel).

  • @Sjokolade so it’s simple, just go back to lista2 for string and change this line: if type(lista1[linha2][linha]) is not lista2[linha]): to: if type(lista1[linha2][linha]) is not eval(lista2[linha])): Abraço

  • It worked, but there was a problem, Python does not recognize nvarchar and varchar as in SQL. So it would need to change to string. Right? You can do it dynamically?

  • If I understand correctly, you take the data types from the database, so the nomenclature of the data types should not match 100% with Python, right? I think I could set up a conversion dictionary, if you pass me the types you use I can mount for you and then you enlarge as your future need.

  • That! I use int, nvarchar, varchar, char, decimal, Numeric and float.

  • See if I’m right: "int" = int, "nvarchar" = str, "varchar" = str, "char" = str, "decimal" = float, "Numeric" = float and "float" = float. That’s right?

  • Exactly that!

  • Worked well? When you get the solution don’t forget to indicate here, your solution should help more people who have the same question. Thank you.

  • Yes, sorry for the delay!! It looks like this: Conversaotipos = {'int': int, 'nvarchar': str, 'varchar': str, 'char': str, 'decimal': float, 'Numeric': float, 'float': float} if type(lista1[Linha2][line]) is (Conversaotipos[Lista2[line]]): print("Something")

  • Beauty. Give an UP in the answer and mark it as your solution, this will indicate to other users what was the solution you used and will give a boost to my reputation. Hug

  • Thank you! Hug!

Show 6 more comments

Browser other questions tagged

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