How do you divide each element in a list by an int?

后端 未结 7 2136
野的像风
野的像风 2020-12-04 08:02

I just want to divide each element in a list by an int.

myList = [10,20,30,40,50,60,70,80,90]
myInt = 10
newList = myList/myInt

This is the

7条回答
  •  既然无缘
    2020-12-04 08:13

    The idiomatic way would be to use list comprehension:

    myList = [10,20,30,40,50,60,70,80,90]
    myInt = 10
    newList = [x / myInt for x in myList]
    

    or, if you need to maintain the reference to the original list:

    myList[:] = [x / myInt for x in myList]
    

提交回复
热议问题