This question already has answers here:
Get a list of numbers as input from the user
                                
                                    (17个答案)
                                
                        
                                2年前关闭。
            
                    
输入是整数,中间用空格隔开,格式如下:
180141142175162

busnumbers = input().split()

for n in busnumbers:
    n = int(n)


我想要巴士号码= [180,141,142,175,162]
相反,我有['180','141','142','175','162']
为什么转换无效?

最佳答案

因为您应用nint()变量不是转换列表中的数据。它只是在循环中转换每个数据。

如果要使用上面的代码,它将是

for i,v in enumerate(busnumbers): busnumbers[i] = int(v)

busnumbers = list(map(int, input().split()))

10-04 19:16