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?
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
@Sjokolade so it’s simple, just go back to
lista2
forstring
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– Henrique Marti
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?
– Sjokolade
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.
– Henrique Marti
That! I use int, nvarchar, varchar, char, decimal, Numeric and float.
– Sjokolade
See if I’m right: "int" = int, "nvarchar" = str, "varchar" = str, "char" = str, "decimal" = float, "Numeric" = float and "float" = float. That’s right?
– Henrique Marti
Exactly that!
– Sjokolade
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.
– Henrique Marti
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")
– Sjokolade
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
– Henrique Marti
Thank you! Hug!
– Sjokolade