How would I produce a while loop, to double and therefore change the numbers in the following list?
Thank you.
numberList = [3, 5, 6, 2, 1, 5, 7, 4]
This was my attempt
numberList = [3, 5, 6, 2, 1, 5, 7, 4]
while numberList == numberList:
numberList == numberList * 2
print (numberList)
break
Although not the best way to solve the problem, this is a way of doing it using a while loop:
numberList = [3, 5, 6, 2, 1, 5, 7, 4]
i=0
while i < len(numberList):
numberList[i] *= 2
i += 1
>>> numberList
[6, 10, 12, 4, 2, 10, 14, 8]
Notice that multiplying a list by an integer will output a list repeating the elements, so your attempt actually does something like this:
>>> [1,2,3]*2
[1, 2, 3, 1, 2, 3]
You can map list with a lambda function to perform the desired function!
numberList= [3, 5, 6, 2, 1, 5, 7, 4]
print map(lambda x:x*2,numberList)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.