python內建資料結構

2021-08-17 19:20:52 字數 2700 閱讀 9060

數列物件

鍵值對

型別轉換(built-in)

int():取整數部分

//:整除且向下取整

min():取最小值

max():取最大值

pow(x,y)等價於x**y

math.sqrt():開平方

進製函式,返回值是字串

math.pi π

math.e 自如常數

count(value)

時間複雜度

len()

不產生新列表,就地修改

時間複雜度o(1)

insert(index,object) -> none

pass

pop([index]) -> item

clear() -> none

sort(key=none,reverse=false) -> none

in 深拷貝

t = tuple()

t = ()

t = tuple(range(1,7,2)

t = (2,3,4,6,7)

t = (1,)

t = (1,2,3) * 6

count(value)

時間複雜度

len(tuple)

s1 = 'string'

s2 = "string2"

s3 = '''this`s a "string" '''

s4 = 'hello \n magedu.com'

s5 = r"hello \n magedu.com"

s6 = 'c:\windows\nt'

s7 = r"c:\windows\\nt"

s8 = 'c:\windows\\nt'

sql = """select * from user where name='tom'"""

partition

split(sep=none,maxsplit=-1) - > list of strings

rsplit(sep=none,maxsplit=-1)-> list of string

splitlines([keepends]) -> list of strings

partition(sep) -> (head,sep,tail)

rpartiton(sep) -> (dead, sep, tail)

lower()

大小寫,做判斷的時候用

swapcase()

capitalize() -> str

center(width[, fillchar]) -> str

zfill(width) -> str

ljust(width[,fillchar]) -> str 左對齊

rjust(width[,fillchar]) -> str 右對齊

strip([chars]) -> str

lstrip([chars]) -> str

rstrip([chars]) -> str

rfind(sub[, strat[, end]]) -> int

index(sub[, start[, end]]) -> int

rindex(sub[, start[, end]]) -> int

時間複雜度

len(string)

count(sub[,start[, end]]) -> int

startwith(prefix[,start[, end]])-> bool

format函式格式字串語法

位置引數

"{}:{}".format('192.168.1.100',8888)

這就是按照位置順序用位置引數替換前面的格式字串的佔位符中

關鍵字引數或命名引數

" :".format(8888,'192.168.1.100', server='web server info:')

位置引數按照序號匹配,關鍵字引數按照名詞匹配

訪問引數

".".format(('magedu','com'))

物件屬性訪問

from collections import namedttuple

point = namedtuple('point','x y')

p = point(4,5)

",}}".format(p)

對齊

'*='.format(3,2,2

*3)'*='.format(3,2,2

*3)'*='.format(3,2,2

*3)''.format('centered')

''.format('centered')

進製

"int: ; hex: ; oct: ; bin: ".format(42)

"int: ; hex: ; oct: ; bin: ".format(42)

octets = [192, 168, 0, 1]

''.format(*octets)

python內建資料結構 Python內建資料結構

分類 數值型int float complex bool 序列物件 list string tuple 鍵值對set集合 dict字典 數值型int python3中的int都是長整型,沒有大小限制,但受限於記憶體區域的大小 float 浮點型,由整數部分和小數部分組成。complex 複數,由實數...

Python內建資料結構

分類 數字處理函式 進製函式,返回字串 型別判斷 列表索引訪問 列表查詢 列表元素修改 列表增加 插入元素 列表尾部追加元素,返回none 即不產生新列表,就地修改 時間複雜度為o 1 追加元素全部輸出為乙個整體 insert index,object 在指定索引位置插入元素object,返回non...

python內建資料結構

序列物件 鍵值對 列表list 可變的 列表訪問 列表查詢 len 時間複雜度 列表元素修改 insert index,object none 列表增加,插入元素 list list 列表刪除元素 pop index item clear none 列表其他操作 sort key none,reve...