Feb-02-2018, 07:06 PM
Hi again everyone,
Here is the current problem I am working on.
I have a list of random input:
mixed_list = ['asdf', 33, 'qwerty', 123.45, 890, 3.0, 'xyz', 0, 'blah', 98765., '', 25]
and I have to loop through it to build a dictionary based on the type of input. So strings is a key that returns all strings, etc.
Playing around with the type and isinstance of functons. It seems people prefer isinstance for reasons that are over my head right now (the research I have done talks about concepts about classes and inheritance and things I don't understand yet).
My question is this: I can successfully get the type of input with type but I am having a hard time using a comparative operator on it.
I can provide all the errors and things i have tried if that helps. I have tried probably 3 or 4 ideas that haven't worked. Most have resulted in errors.
Here is the current problem I am working on.
I have a list of random input:
mixed_list = ['asdf', 33, 'qwerty', 123.45, 890, 3.0, 'xyz', 0, 'blah', 98765., '', 25]
and I have to loop through it to build a dictionary based on the type of input. So strings is a key that returns all strings, etc.
Playing around with the type and isinstance of functons. It seems people prefer isinstance for reasons that are over my head right now (the research I have done talks about concepts about classes and inheritance and things I don't understand yet).
My question is this: I can successfully get the type of input with type but I am having a hard time using a comparative operator on it.
ints = () floats = () strings =() for x in mixed_list: y = type (x) list(y)I dont really understand how to work after the y = statement. Comparisons like searching for substrings don't seem to work and trying to reformat it as a list also provides an error.
I can provide all the errors and things i have tried if that helps. I have tried probably 3 or 4 ideas that haven't worked. Most have resulted in errors.
for x in mixed_list: y = type (x) if y == "<class 'int'>": print ("found it")Snipped to try to compare the exact output which fails and prints nothing.
for x in mixed_list: y = type (x) if "int" in y: print ("found it")Produces error: TypeError: argument of type 'type' is not iterable
for x in mixed_list: y = type (x) z = str(y) if "int" in z: print ("found it")This seems to work. I am not going to delete the thread because I might have more questions and maybe someone can learn from this.