简体   繁体   中英

Question about very simple isinstance code

I tried to do some training code, the code is to separate the data types. I made a list and filled it with some numbers and letters and floats I wanted the integer numbers in lstInt and the float in the lstflt and string in lstsrt

>>> lst=[1,'a',2,'b',3,'c',4.5,9.9]
>>> lstInt=[]
>>> lstflt=[]
>>> lststr=[]
>>> x=0
>>> for item in lst:
...     if isinstance(i, int):
...             lstInt.append(i)
...             lst.pop(x)
...     if isinstance(i, str):
...             lststr.append(i)
...             lst.pop(x)
...     if isinstance(i, float):
...             lstflt.append(i)
...             lst.pop(x)
...     x=x+1
...
1
2
3
4.5
>>> lst
['a', 'b', 'c', 9.9]
>>> lstInt
[]
>>> lstflt
[]
>>> lststr
[]
>>>

I believe the first mistake is in misusing the for loop variable. You has defined "item" as your for loop variable, but in the isinstance and append you are using "i".

Also when you pop the element from list you are changing the positions, what disturb the loop. The for loop starts with the element 0 (first element), in your code case the number "1", when the program ends the first iteration it will now take the element 1(second element), but as it has popped the first element, now the fist element is the letter 'a' and the second is the number "2", what will make the program ignore an element every time it iterate.

I think that the code you want is something like that:

>>> for item in lst:
...     if isinstance(item, int):
...         lstInt.append(item)
...     if isinstance(item, str):
...         lststr.append(item)
...     if isinstance(item, float):
...         lstflt.append(item)

For example in

for item in lst:
     if isinstance(i, int):
             lstInt.append(i)
             lst.pop(x)

i does not exist. I think you mean to use item

Also, by using pop(x) , you are removing the items from the list by index this means that if the list gets smaller, the index's of all items after the item removed, gets reduced, since there are less spots for the items to fill. So although in the original list [1,'a',2,'b',3,'c',4.5,9.9] 'b' was in index 3, if you removed 1 and 'a' then the list would now be [2,'b',3,'c',4.5,9.9] and 'b' would be at index 1, while the variable x would still try and remove 'b' from index 3, so pop(3) would not remove 'b' anymore, it would remove 'c' .

Also If you are writing out long programs, I wouldn't recommend use the python interpreter (The thing where you can type commands and it automatically compiles and gives you an answer, each line starts with >>> )(a better explaination for this is welcome to be edited in) I would recommend either writing scripts and then executing them (at some point), or for now using an online compiler.

https://www.onlinegdb.com/online_python_compiler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM