python內置數據結構
sort(key=none,resverse=false)—>none
對列表元素進行排序,就地修改。默認升序
resvers為true,反轉,降序
key為一個函數,指定key如何排序
ls.sort(key=functionaame)
Print(lst.sort(key=str,reverse=Ture)
In
[3,4]in[1,2,[3,4]]
for x in [1,2,3,4]
列表復制
copy() ?–> list
Shadow copy 返回一個新的列表
影子拷貝,也叫淺拷貝,遇到引用類型,只是復制了一個引用
深拷貝
copy模塊提供了deepcopy
Import copy
Lst0 = [1,[2,3,5,],5]
Lst5 = copy.deepcopy(lst0)
Lst5[1][1] = 0
Lst5 == lst0
隨機數
random模塊
Randint(a,b)返回a b 之間的隨機數
Choice(seq)從非空序列的元素中隨機挑選一個元素,比如random.choice(range(10)),從09隨機挑選一個整數
列表練習
求100以內的素數
#求100以內的素數
import math
lst = []
n = 100
for i in range(2,n):
????for j in lst:
????????if not i%j:
????????????break
????else:
????????print(i)
????????lst.append(i)
元組tuple
一個有序的元素組成的集合
使用小括號()表示
元組是不可變的對象
元組元素的訪問
支持索引(下標)
正索引:從左到右,從0開始,為列表中的每一個元素編號
負索引:從右到左,從負一開始
正負索引不可以超界,引發indexerror
Index(value,[start,[stop]])
通過value值,從指定區域查找列表內的元素是否匹配
匹配第一個就立即返回索引
匹配不到就拋出異常valueerror
Count(value)
返回列表中匹配value的次數
時間復雜度
Index和count方法都是O(n)
隨著數據規模的增大,而效率下降
Len(tuple)
反回元素的個數
楊輝三角
1.
trangle = [[1],[1,1]]
n = 6
for i in range(2,n):
????newline = [1]
????pre = trangle[i-1]
????for j in range(i-1):
????????val = pre[j] + pre[j+1]
????????newline.append(val)
????newline.append(1)
????trangle.append(newline)
print(trangle)
2.
??n = 6
for i in range(n):
????newline = [1]
????if i == 0:
????????print(newline)
????????continue
????for j in range(i-1):
????????val = pre[j] + pre[j+1]
????????#print(val)
????????newline.append(val)
????????#print(newline)
????newline.append(1)
????print(newline)
????pre = newline
?
元組tuple
一個有序的元素組成的集合
使用小括號()表示
元祖是不可變的對象
定義:tuple() –>empty tuple
?Tuple(iterable) –>
元組元素的訪問
支持索引
正索引:從左到右,從0開始,為列表中每一個元素編號
負索引:從右到左,從-1開始
正負索引不可以超界,否則引發indexerror
Tuple[index] ?index就是索引,使用中括號訪問
冒泡法
lst = [8,9,5,7,2,6,4,3,1,0]
lenth=len(lst)
for i in range(lenth):
????for j in range(lenth-i-1):
????????if lst[j]>lst[j+1]:
????????????lst[j],lst[j+1]=lst[j+1],lst[j]
print(lst)
字符串
一個個字符串組成的有序的序列,是字符的集合
使用單引號,雙引號,三引號住的字符串序列
字符串是不可變的對象
python3起,字符串就是Unicode類型
字符串元素訪問—下標
sql = “select ?* ?from user where name=’tom’”
Sql[4]#字符串‘c’
Sql[4] = ‘o’
有序的字符集合。字符序列
For c in sql:
Print(c)
Print(type(c)) #什么類型
可迭代
Lst ?= list(sql)
字符串join連接
“string”.join(iterable) -> str
將可迭代對象連接起來,使用string作為分隔符
可迭代對象本身元素都是字符串
返回一個新的字符串
lst = [‘1′,’2′,’3’]
print(“\””.join(lst))
print(” “.join(lst))
print(“\n”.join(lst))
字符串分割
分割字符串的方法分為2類
Split系
將字符串按照分隔符分成若干字符串,并返回列表
Partition系
將字符串按照分隔符分成2段,返回這兩段和分隔符的元組
Rsplit(sep=none,maxsplit= -1)-> lsit of strings
從右到左
sep指定分割字符串,缺省的情況下空白字符串作為分割符
Maxsplit指定分割的次數,-1表示遍歷整個字符串
字符串大小寫
Upper()全大寫 ?lower()全小寫 ???swapcase()交互大小寫
Tile() -> str
標題的每個單詞都大小寫
capitalize()->首個單詞大寫
Center(width[,fillchar])->str
Width 打印寬度
Fillchar 填充的字符
字符串修改
replace(old,new[,count])->str
字符串中找到匹配的替換為新的字符串,返回新字符串count表示替換幾次,不指定默認全部
字符串查找*
find(sub[start[,end]]) —> int
在指定的區間[start,end],從左到右,查找字串sub。找到返回索引,沒找到返回-1
Rfind(sub[start[end]]) —> int
在指定的區間[star,end],從右到左查找子串sub。找到返回索引,沒找到返回-1
Index(sub[start[end]]) ?–>int
在指定的區間[start,end],從左到右查找子串sub,找到返回索引沒找到拋出異常ValueError
rIndex(sub[start[end]]) ?–>int
在指定的區間[start,end],從左到右查找子串sub,找到返回索引沒找到拋出異常
字符串判斷
Endswith(suffix[start,end]])—–> bool
在指定的區間start,end 字符串是否以suffix結尾
Startswith(prefix[start,end]])—>bool
在指定的區間tart,end ??字符串是否以prefix開頭
?
?
?
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/93859