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

