简体   繁体   English

将一个数字拆分为一串单独的整数。 'int' object 不可迭代?

[英]Splitting a number into a string of separate ints. 'int' object is not iterable?

Simple problem.简单的问题。

I have a number (integer):我有一个数字(整数):

mynumber = 1239

I want to convert it into a list of the separate integers like [1,2,3,9] :我想将它转换成一个单独的整数列表,如[1,2,3,9]

numbersplit =  [int(x) for x in mynumber]

But I get the error:但我得到了错误:

TypeError: 'int' object is not iterable

Why doesn't this work?为什么这不起作用? I'm just making sure the ints are actually ints .我只是确保ints实际上是ints

However it does work when I wrap it in str ..?但是,当我将它包装在str中时它确实有效 ..?

[int(x) for x in str(mynumber)]

[1, 2, 3, 9, 8, 9, 8, 7, 2, 3, 9, 4, 7, 8]

Do lists need to be converted into a string before I can do this?在执行此操作之前是否需要将列表转换为字符串?

You cannot iterate on an int, but you can do it on a string.您不能在 int 上进行迭代,但可以在字符串上进行。

One approach is to convert it to a string:一种方法是将其转换为字符串:

mynumber = 1239
number_str = str(mynumber)

Then you can create your list, converting it back to int然后您可以创建您的列表,将其转换回 int

[int(x) for x in number_str]

Try this:尝试这个:

[int(i) for i in str(mynumber)]

When you try to do for x in mynumber you are assuming that mynumber is an iterable but it is actually an integer.当您尝试for x in mynumber您假设mynumber是可迭代的,但它实际上是 integer。 You need to type cast it into a str, then it could be considered as an iterable.您需要将其类型转换为 str,然后可以将其视为可迭代的。 You can do for x in str(mynumber) - that way it would give you each character in each iteration and you can then convert each char into integer.您可以for x in str(mynumber)执行此操作 - 这样它会在每次迭代中为您提供每个字符,然后您可以将每个字符转换为 integer。 Try this:尝试这个:

numbersplit =  [int(x) for x in str(mynumber)]

Do lists need to be converted into a string before I can do this?在执行此操作之前是否需要将列表转换为字符串?

No. You can iterate over a list just fine.不,您可以很好地遍历列表。

Iterating over a string gives you the individual letters, while you cannot iterate over an int .迭代一个字符串会给你单个字母,而你不能迭代一个int

The reason you can iterate over a string is because a Python string is actually a sequence type.您可以迭代字符串的原因是因为 Python 字符串实际上是一个sequence类型。

for iterates on an iterable data such as list, string, dictionaries etc. int object is not iterable data type ans we cannot access int digits separately as we can access strings or list with the help of indexes.用于迭代列表、字符串、字典等可迭代数据。 int object 不是可迭代数据类型,我们不能单独访问 int 数字,因为我们可以在索引的帮助下访问字符串或列表。

Or use:或使用:

numbersplit = list(map(int, str(mynumber)))

Integers aren't iterable, to know whether an object is iterable, try using:整数不可迭代,要知道 object 是否可迭代,请尝试使用:

>>> iter(1)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    iter(1)
TypeError: 'int' object is not iterable
>>> iter('a')
<str_iterator object at 0x000000CE599A40B8>
>>> 

If it throws an error it means it isn't iterable.如果它抛出错误,则意味着它不可迭代。

an integer is not iterable, you can iterate over his digits after you covert him to a string integer 不可迭代,您可以在将他转换为字符串后迭代他的数字

list(map(int, str(mynumber)))

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

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