最佳答案
我只想将列表中的每个元素除以一个整数。
myList = [10,20,30,40,50,60,70,80,90]
myInt = 10
newList = myList/myInt
这是错误:
TypeError: unsupported operand type(s) for /: 'list' and 'int'
我明白为什么我会收到这个错误。但我很沮丧,因为我找不到解决办法。
也试过:
newList = [ a/b for a, b in (myList,myInt)]
错误:
ValueError: too many values to unpack
预期结果:
newList = [1,2,3,4,5,6,7,8,9]
编辑:
下面的代码给出了我预期的结果:
newList = []
for x in myList:
newList.append(x/myInt)
但有没有更简单/更快的方法来做到这一点?