python第七次筆記2018 4 14

2021-08-18 20:10:28 字數 4405 閱讀 2813

#申明乙個函式,第乙個引數是整型,第二個引數是list型別

# l 有乙個預設值,預設值為空列表

def f(x,l=):

for i in range(x):

print(l)

# f(2) = f(2,l=)

f(2)

#輸出 [0,1]

f(3,[3,2,1])

#結果:[3,2,1,0,1,4]

f(3)

#結果:[0,1,4]

如果在同一檔案下執行,會有一點影響,f(3)會記住f(2)記憶體,輸出[0,1,,0,1,4]

或者可以在f(3)更改為f(3,l=),把l重新定義為空list

'''函式的關鍵字

def 定義函式

return 返回值

pass 濾過

exit(1) 直接退出

'''def add1(x,y):

print(x+y)

def add2(x,y):

return x+y

##差別

add1(1,2)

result = add2(1,2)

print(result)

def hello():

pass

print("hello")

#pass直接濾過,不輸出hello

def hello():

exit(1)

print("hello")

#exit(1)直接退出

'''函式的引數

*args tuple引數,對應賦值

**kwargs dict引數,對應賦值

fun(*args,**kwargs)

fun(1,2,3,4,5,a=10,b=40)

'''def test(m,*args,**kwargs):

print("m = ".format(m))

print("args = ".format(args))

print("kwargs = ".format(kwargs))

test(10,args=(1,11,12))

#test(m=10,1,2,3,a=1,b=2)

匿名函式的定義:

顧名思義就是沒有名字的函式,

lambda 函式是一種快速定義單行的最小函式,可以用在任何需要函式的地方

def fun(x,y)

return x*y

lambda版本:

r = lambda x,y:x*y

ss求絕對值(abs)

'''匿名函式

def add(x,y):

return x+y

add = lambda x,y:x+y

'''def add(x,y):

return x+y

add = lambda x,y:x+y

'''高階函式 裝逼函式

都是可以通過**邏輯實現的

但是你寫到函式的複雜程式,或者演算法不一定有人家內建的好

'''def f(x):

return x*x

for i in map(f,[1,2,3,4]):

print(i)

def f(x):

return x*x

print(list((map(f,[1,2,3,4]))))

def testmap(fun,iter):

l = list()

for i in iter:

return l

print(testmap(f,[1,2,3,4]))

#reduce

#filter(lambda x:x%2 == 1,[1,2,3,4,5])

#能符合lambda x:x%2 的留下,不符合的去掉

# def add(x,y):

# return x+y

# reduce(f,[1,2,3,4,5])

# 1,2 = 3

# 3, 3 = 6

# 6,4 = 14

# 10,5 = 15

唯一用得比較多的,就是sorted函式

# sorted(iterable,key,reverse)

# iterable 乙個可迭代的物件

# key 對什麼進行排序

# reverse bool型別,如果為true為反序,如果為false為正序

# 返回值是乙個list

print(sorted([1,4,342,3,45,76,435,34],reverse=true))

m = dict(a=1,c=10,b=20,d=15)

print(m)

print(sorted(m.items(),key=lambda x:x[1],reverse=false))

#print(sorted(m.items(),key = lambda d:d[1],reverse = true))

#字典有三種初始化的方法

#第一種 dict(a=1,b=2 )

#第二種

#第三種 dict([("a",1),("b",2)])

m = dict(a=1,c=10,b=20,d=15)

print(m)

print(dict(sorted(m.items(),key=lambda x:x[1],reverse=false)))

'''列表生成式

[exp for val in collection if condition]

生成器'''

def jgg():

number = list()

for i in range(1,10):

for a in [x for x in range(1,10)]:

for b in [x for x in range(1,10) if x != a]:

for c in [x for x in range (1, 10) if x != a and x != b]:

for d in [x for x in range (1, 10) if x != a and x != b and x != c]:

for e in [x for x in range (1, 10) if x != a and x != b and x != c and x != d]:

for f in [x for x in range (1, 10) if x != a and x != b and x != c and x != d and x != e]:

for g in [x for x in range (1, 10) if x != a and x != b and x != c and x != d and x != e and x != f]:

for h in [x for x in range (1, 10) if x != a and x != b and x != c and x != d and x != e and x != f and x != g]:

for i in [x for x in range (1, 10) if x != a and x != b and x != c and x != d and x != e and x != f and x != g and x != h]:

if (a+b+c == d+e+f == g+h+i ==15):

print("a = b = c = d = e = f = g = h = i = ".format(a,b,c,d,e,f,g,h,i))

jgg()

a1 = (x for x in range(1,10) if x%2==0)

print(a1)

#python2 a1.next()

print(next(a1)) #python3是直接呼叫next方法

print("##"*10)

for i in a1:

print(i)

def test():

a= 1

for i in range(1,10):

yield i

a += 1

# return i

#returun 和yield的區別

#yield 可以理解成return,但是比return多一些角色

# yield 每次

m = test()

print(m)

def px(item):

pass

result = ""

with codecs.open("passwd","r") as f:

result = sorted(f.readlines(),key=lambda item: int(item.split(":")[2]))

with codecs.open("sortpasswd","w") as f:

f.writelines(result)

第七次作業

磁碟管理和維護 磁碟檔名 dev sd a p 1 128 dev hd a p 1 128 dev vd a p 1 128 heads 磁面 sectors track 扇區 cylinders磁柱 分割槽命令 fdisk dev sd 格式化 mkfs.ext4 dev sda mkfs.xf...

第七次試驗

include include include include include include using namespace std define maxsize 100 typedef char elemtype typedef struct sqqueue void initqueue sqq...

第七次作業

共有一下六步 1.客戶機提出網域名稱解析請求,並將該請求傳送給本地的網域名稱伺服器。2.當本地的網域名稱伺服器收到請求後,就先查詢本地的快取,如果有該紀錄項,則本地的網域名稱伺服器就直接把查詢的結果返回。3.如果本地的快取中沒有該紀錄,則本地網域名稱伺服器就直接把請求發給根網域名稱伺服器,然後根網域...