如何使 python 中的数字可迭代?

How to make numbers Iterable in python?

我们如何迭代 python 中的整数集合或我们如何将数字集合变成 python 中的列表?

当我尝试遍历整数时出现错误。

TypeError: 'int' 对象不可迭代

输入 n = 7849 9594 9699

n = input()
n1, n2, n3 = list(map(int, n.split(' ')))

for i in n1:
print(i)

回溯(最近调用最后):

文件“E:/Python code/wipro - 2.py”,第 5 行,在

对于 n1 中的 i:

TypeError: 'int' 对象不可迭代

所以我的问题是如何使 n1 可迭代或如何将 n1、n2、n3 转换为列表?

我要,

   n1 = 7849 (as list)

   n2 = 9594 (as list)

   n3 = 9699 (as list)

以便我可以对 n1、n2、n3 执行列表函数。

提前致谢!

如果你需要一个列表,你不需要拆分后的三个对象。

拆分输入后使用此获取列表:

n = input()
n1 = list(map(int, n.split(' ')))

for i in n1:
    print(i)

听起来混淆是“作为列表”的概念。我最好的猜测是你想要的是

n1 = ['7', '8', '4', '9']
n2 = ['9', '5', '9', '4']
n3 = ['9', '6', '9', '9']

所以你可能只需要使用

n1, n2, n3 = list(map(list, n.split(' ')))

或类似的东西。有优化的空间,但在不了解您的用例的情况下很难说什么是完美的。