搜尋

python常用函數

Nov 26, 2016 am 10:02 AM

   一、数学相关
1、绝对值:abs(-1)
2、最大最小值:max([1,2,3])、min([1,2,3])
3、序列长度:len('abc')、len([1,2,3])、len((1,2,3))
4、取模:divmod(5,2)//(2,1)
5、乘方:pow(2,3,4)//2**3/4
6、浮点数:round(1)//1.0
二、功能相关
1、函数是否可调用:callable(funcname),注意,funcname变量要定义过
2、类型判断:isinstance(x,list/int)
3、比较:cmp('hello','hello')
4、快速生成序列:(x)range([start,] stop[, step])
三、类型转换
1、int(x)
2、long(x)
3、float(x)
4、complex(x) //复数
5、str(x)
6、list(x)
7、tuple(x) //元组
8、hex(x)
9、oct(x)
10、chr(x)//返回x对应的字符,如chr(65)返回‘A'
11、ord(x)//返回字符对应的ASC码数字编号,如ord('A')返回65
四、字符串处理
1、首字母大写:str.capitalize
2、字符串替换:str.replace
3、字符串切割:str.split
五、序列处理函数
1、len:序列长度
2、max:序列中最大值
3、min:最小值
4、filter:过滤序列
六、并行
  zip
  map 
  reduce

 時間類型處理:

time.time() 返回当前时间戳即,秒数1394647431.90625 
time.asctime()  返回当时时间的ascii表式形势,'Thu Mar 13 02:03:46 2014' 
time.ctime() 当前时间的字符串形式 'Thu Mar 13 02:04:59 2014'和ascii码表显形式一样 
time.ctime();time.ctime(time.time())  可以指定时间戳转找成字符串形式'Wed Mar 12 08:26:26 2014' 
time.localtime() 返回当前时区的内部time格式struct_time格式 time.struct_time(tm_year=2014, tm_mon=3, tm_mday=13, tm_hour=2, tm_min=8, tm_sec=14, tm_wday=3, tm_yday=72, tm_isdst=0) 
time.localtime()[:6]  #(2014, 3, 13, 8, 15, 7, 3, 72) 
time.localtime()[0:9] #(2014, 3, 13, 8, 15, 24, 3, 72, 0) 
time.gmtime() 将时间戳转变成UTC时区(0时区)的struct_time结构, 
time.mktime() 将内部time格式转换成时间戳,即秒数1394583457.0 
time.mktime(time.localtime(time.time())) 如1394583457.0
time.strftime("%Y-%m-%d",time.localtime(time.time())) 有将内部time结构转变成我们人为可读的指定的字符串格式 '2014-3-12'
time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) 如'2014-03-13 01:54:27'
time.strftime("%a %b %d %H:%M:%S %Y",time.localtime()) 如'Thu Mar 13 01:55:05 2014'
time.strptime()将字符串格式的时间转变成内部time格式
time.strptime("Sat Mar 28 22:24:25 2009","%a %b %d %H:%M:%S %Y")返回一个struct_time结构时间time.struct_time(tm_year=2009, tm_mon=3, tm_mday=28, tm_hour=22, tm_min=24, tm_sec=25, tm_wday=5, tm_yday=87, tm_isdst=-1)
a = "Sat Mar 28 22:24:24 2009" 这个是把字符转转变成struct_time内部结构后再用time.mktime()生成时间戳,即生成字符串。
b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python的科學計算中如何使用陣列?Python的科學計算中如何使用陣列?Apr 25, 2025 am 12:28 AM

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

您如何處理同一系統上的不同Python版本?您如何處理同一系統上的不同Python版本?Apr 25, 2025 am 12:24 AM

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

與標準Python陣列相比,使用Numpy數組的一些優點是什麼?與標準Python陣列相比,使用Numpy數組的一些優點是什麼?Apr 25, 2025 am 12:21 AM

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

陣列的同質性質如何影響性能?陣列的同質性質如何影響性能?Apr 25, 2025 am 12:13 AM

數組的同質性對性能的影響是雙重的:1)同質性允許編譯器優化內存訪問,提高性能;2)但限制了類型多樣性,可能導致效率低下。總之,選擇合適的數據結構至關重要。

編寫可執行python腳本的最佳實踐是什麼?編寫可執行python腳本的最佳實踐是什麼?Apr 25, 2025 am 12:11 AM

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

Numpy數組與使用數組模塊創建的數組有何不同?Numpy數組與使用數組模塊創建的數組有何不同?Apr 24, 2025 pm 03:53 PM

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Apr 24, 2025 pm 03:49 PM

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

CTYPES模塊與Python中的數組有何關係?CTYPES模塊與Python中的數組有何關係?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器