简体   繁体   English

关于非常简单的isinstance代码的问题

[英]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 我做了一个列表,并与一些数字和字母,彩车我想在整数填充它lstInt和漂浮在lstflt并串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. 我相信第一个错误是滥用for循环变量。 You has defined "item" as your for loop variable, but in the isinstance and append you are using "i". 您已经将“ item”定义为for循环变量,但是在isinstance和append中,您正在使用“ 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. for循环以元素0(第一个元素)开始,在您的代码中为数字“ 1”,当程序结束第一次迭代时,它现在将使用元素1(第二个元素),但是由于它弹出了第一个元素,现在第一个元素是字母“ a”,第二个元素是数字“ 2”,这会使程序在每次迭代时都忽略该元素。

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不存在。 I think you mean to use item 我想你的意思是使用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. 另外,通过使用pop(x) ,您将按索引从列表中删除项目,这意味着如果列表变小,则删除项目后所有项目的索引会减少,因为要删除的项目较少填。 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' . 因此,尽管在原始列表[1,'a',2,'b',3,'c',4.5,9.9] 'b'在索引3中,但是如果您删除了1 and 'a'则列表现在将是[2,'b',3,'c',4.5,9.9]'b'将位于索引1,而变量x仍将尝试从索引3中删除'b' ,因此pop(3)将不再删除'b' ,它将删除'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. 另外,如果您要编写较长的程序,我不建议您使用python解释器(您可以在其中键入命令并自动编译并给出答案的东西,每行以>>>开头)(对此的更好解释(欢迎在其中进行编辑)我建议要么编写脚本然后执行它们(在某个时候),要么建议现在使用联机编译器。

https://www.onlinegdb.com/online_python_compiler https://www.onlinegdb.com/online_python_compiler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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