Collect 9 common methods of calculating list square in Python:
1. Use a for loop
This method traverses each number in the list, using**
The operator calculates its squared and then adds the result to the new list.
numbers = [1, 2, 3, 4, 5] squared_numbers = [] for num in numbers: squared_numbers.append(num ** 2) print(squared_numbers)
2. Use list comprehensions
This method uses list comprehension, a cleaner way to perform actions on each item of an existing list to create a new list.
numbers = [1, 2, 3, 4, 5] squared_numbers = [num ** 2 for num in numbers] print(squared_numbers)
3. Use map() function and lambda function
This method uses the map() function and the lambda function to calculate the square of each number in the list.
numbers = [1, 2, 3, 4, 5] squared_numbers = list(map(lambda x: x ** 2, numbers)) print(squared_numbers)
4. Use map() function and defined functions
Similar to method 3, but it uses a separate defined function square(x), instead of the lambda function.
numbers = [1, 2, 3, 4, 5] def square(x): return x ** 2 squared_numbers = list(map(square, numbers)) print(squared_numbers)
5. Use numpy library
This method uses the() function to calculate the square of each number in the list.
import numpy as np numbers = [1, 2, 3, 4, 5] squared_numbers = (numbers) print(squared_numbers)
6. Use generator expressions
This method uses generator expressions, which is a high performance, memory-efficient generalization of list comprehensions and generators.
numbers = [1, 2, 3, 4, 5] squared_numbers = (num ** 2 for num in numbers) squared_numbers = list(squared_numbers) print(squared_numbers)
7. Use the math library
This method uses the() function to calculate the square of each number in the list.
import math numbers = [1, 2, 3, 4, 5] squared_numbers = [(x, 2) for x in numbers] print(squared_numbers)
8. Use the operator module
This method uses the() function to multiply each number in the list by itself.
import operator numbers = [1, 2, 3, 4, 5] squared_numbers = list(map(, numbers, numbers)) print(squared_numbers)
9. Use a loop with enumeration
This method uses the enumerate() function to get the index and value of each number in the list, and then use**
The operator calculates the square of the number.
numbers = [1, 2, 3, 4, 5] squared_numbers = [] for i, num in enumerate(numbers): squared_numbers.append(numbers[i] ** 2) print(squared_numbers)
Attachment: Python calculates the squared instance code of all elements in the list
Method one map()
#-*- coding:utf-8 -*- def pow2(arg): return arg**2 def pow2List(listarg): mapObj = map(pow2, listarg) result = list(mapObj) return result print(pow2List([1,-1,0,3,5]))
Run the python file and get the output:
[1, 1, 0, 9, 25]
Method 2 derivation
>>> list1 = [-1,0,5,6,15] >>> [x**2 for x in list1]
Summarize
This is the end of this article about 9 common methods for calculating list squared Python List. For more related content on calculating list squared Python List, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!