Python?List計算列表平方的9種常見方法
整理9種Python常見的計算列表平方的方法:
1. 使用for循環(huán)
此方法遍歷列表中的每個數(shù)字,使用 ** 運算符計算其平方,然后將結果添加到新的列表中。
numbers = [1, 2, 3, 4, 5]
squared_numbers = []
for num in numbers:
squared_numbers.append(num ** 2)
print(squared_numbers)
2. 使用列表推導式
此方法使用列表推導式,這是一種更簡潔的方式,可以在現(xiàn)有列表的每個項目上執(zhí)行操作以創(chuàng)建新列表。
numbers = [1, 2, 3, 4, 5] squared_numbers = [num ** 2 for num in numbers] print(squared_numbers)
3. 使用map()函數(shù)和lambda函數(shù)
此方法使用map()函數(shù)和lambda函數(shù)來計算列表中每個數(shù)字的平方。
numbers = [1, 2, 3, 4, 5] squared_numbers = list(map(lambda x: x ** 2, numbers)) print(squared_numbers)
4. 使用map()函數(shù)和定義的函數(shù)
與方法3類似,但是它使用單獨定義的函數(shù)square(x),而不是lambda函數(shù)。
numbers = [1, 2, 3, 4, 5]
def square(x):
return x ** 2
squared_numbers = list(map(square, numbers))
print(squared_numbers)
5. 使用numpy庫
此方法使用numpy.square()函數(shù)來計算列表中每個數(shù)字的平方。
import numpy as np numbers = [1, 2, 3, 4, 5] squared_numbers = np.square(numbers) print(squared_numbers)
6. 使用生成器表達式
此方法使用生成器表達式,這是列表推導式和生成器的高性能、內(nèi)存效率高的泛化。
numbers = [1, 2, 3, 4, 5] squared_numbers = (num ** 2 for num in numbers) squared_numbers = list(squared_numbers) print(squared_numbers)
7. 使用math庫
此方法使用math.pow()函數(shù)來計算列表中每個數(shù)字的平方。
import math numbers = [1, 2, 3, 4, 5] squared_numbers = [math.pow(x, 2) for x in numbers] print(squared_numbers)
8. 使用operator模塊
此方法使用operator.mul()函數(shù)來將列表中的每個數(shù)字與自身相乘。
import operator numbers = [1, 2, 3, 4, 5] squared_numbers = list(map(operator.mul, numbers, numbers)) print(squared_numbers)
9. 使用帶有枚舉的循環(huán)
此方法使用enumerate()函數(shù)來獲取列表中每個數(shù)字的索引和值,然后使用 ** 運算符計算數(shù)字的平方。
numbers = [1, 2, 3, 4, 5]
squared_numbers = []
for i, num in enumerate(numbers):
squared_numbers.append(numbers[i] ** 2)
print(squared_numbers)附:python計算列表所有元素平方的實例代碼
方法一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]))運行python文件,得到輸出:
[1, 1, 0, 9, 25]
方法二推導式
>>> list1 = [-1,0,5,6,15] >>> [x**2 for x in list1]
總結
到此這篇關于Python List計算列表平方的9種常見方法的文章就介紹到這了,更多相關Python List計算列表平方內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python爬取NUS-WIDE數(shù)據(jù)庫圖片
本文給大家分享的是使用Python制作爬蟲爬取圖片的小程序,非常的簡單,但是很實用,有需要的小伙伴可以參考下2016-10-10
pytorch1.60 torch.nn在pycharm中無法自動智能提示的解決
這篇文章主要介紹了pytorch1.60 torch.nn在pycharm中無法自動智能提示的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Pycharm打開.py文件和項目的幾種實現(xiàn)方式
這篇文章主要介紹了Pycharm打開.py文件和項目的幾種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
python-leetcode求區(qū)間[M,N]內(nèi)的所有素數(shù)的個數(shù)實現(xiàn)方式
這篇文章主要介紹了python-leetcode求區(qū)間[M,N]內(nèi)的所有素數(shù)的個數(shù)實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
Python使用Socket(Https)Post登錄百度的實現(xiàn)代碼
以前都是用一些高級模塊,封裝的比較好,今天嘗試使用socket模塊登錄百度,弄了半天才弄好,主要由于百度在登陸頁使用了https,我們需要對socket進行一定處理2012-05-05
python高并發(fā)異步服務器核心庫forkcore使用方法
這篇文章主要介紹了python高并發(fā)異步服務器核心庫forkcore的使用方法,大家參考使用吧2013-11-11
Python 使用Opencv實現(xiàn)目標檢測與識別的示例代碼
這篇文章主要介紹了Python 使用Opencv實現(xiàn)目標檢測與識別的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09

