python闈㈣瘯棰�

鐢ㄤ簬python闈㈣瘯鏁寸悊锛屼富瑕佹潵婧愪簬http://www.cnblogs.com/skiler/p/6952707.html锛屽緢鎰熻阿鍗氫富鍒嗕韩锛屾杩庢寚姝d氦娴併��

Python闈㈣瘯200棰橈紙鍚埇铏級

闈㈣瘯棰�(涓�)銆�15銆�

1銆佺幇鏈変袱鍏冪聽 (('a'),('b'),('c'),('d') ) ,璇蜂娇鐢≒ython涓殑鍖垮悕鍑芥暟鐢熸垚鍒楄〃[ {'a':'c',{'c':'d'}]

绛旀锛歷 = list(map(lambda x,y:{x:y},data[0:2],data[2:4]))

data = (('a'),('b'),('c'),('d') )

v = list(map(lambda x,y:{x:y},data[0:2],data[2:4]))

data[0:2] = ('a'),('b')

data[2:4] = ('c'),('d')

鍖垮悕鍑芥暟浼犺繘鍘宦� 鍙傛暟(x,y)聽 杩斿洖{x:y}

map(func,iterable) #瀵逛簬iterable渚濇浼犻�掔粰func锛岃繑鍥炵殑鏄彲杩唬瀵硅薄

2銆佺湅浠g爜 鍐欑粨鏋�

def multipliters():

聽 聽 return [lambda x:i * x for i in range(4)]

print([m(2) for m in multipliters()])


# 绗竴绉嶆槸 灏嗙敓鎴愬櫒瀵硅薄鐢熸垚鐨勫尶鍚嶅嚱鏁拌浆鍖栨垚鍒楄〃 锛屽尶鍚嶅嚱鏁颁娇鐢ㄧ殑鏄浉鍚岀殑鍐呭瓨绌洪棿銆�

# 杞崲鎴愬垪琛ㄥ悗锛屽惊鐜粨鏉燂紝鍛藉悕绌洪棿閲岀殑i閮戒负3

def multipliters():

聽 聽 return [lambda x:i * x for i in range(4)]

print([m(2) for m in multipliters()])

# 浠ヤ笅鏄緷娆¤繑鍥� 鐢熸垚鍣ㄧ敓鎴愮殑姣忎釜鍖垮悕鍑芥暟锛屾瘡娆¤皟鐢╥閮藉湪鍙樺寲

def multipliters2():

聽 聽 return (lambda x: i*x for i in range(4))

print([m(2) for m in multipliters2()])


3銆�1 or 2鍜�1 and 2鍒嗗埆杈撳嚭浠�涔堬紵


# 1聽 聽 2聽

# 0 "" [] {} set() None False鏀惧叆鍒ゆ柇琛ㄨ揪寮忛兘涓哄亣

# or鎴柭� and涓�

# 鏁翠釜寮忓瓙 涓嶆柇鍘诲悜鍙虫煡鐪嬶紝鐩村埌鑳界‘瀹氫负鐪熷氨鍋滀笅鏉�


4銆佺湅浠g爜 鍐欑粨鏋�

value = "B" and "A" or "C"聽

print(value)

# A

# 鏁翠釜寮忓瓙 涓嶆柇鍘诲悜鍙虫煡鐪嬶紝鐩村埌鑳界‘瀹氫负鐪熷氨鍋滀笅鏉�


5銆佺湅浠g爜鍐欑粨鏋�

v1 = [i % 2 for i in range(10)]

v2 = (i % 2 for i in range(10))

print(v1,v2)


# v1 = [i % 2 for i in range(10)]涓哄垪琛ㄧ敓鎴愬紡

# v2 = (i % 2 for i in range(10))涓� at 0x00E4F990>涓虹敓鎴愬紡


6銆佺湅浠g爜锛屽啓缁撴灉

def extendList(val,li=[]):

聽 聽 li.append(val)

聽 聽 return li

list1 = extendList(10)

list2 = extendList(123,[])

list3 = extendList('a')

print(list1)

print(list2)

print(list3)


# [10, 'a']

# [123]

# [10, 'a']

# 鍙傛暟鍙橀噺鍦ㄧ紪璇戜細鐢熸垚锛岃�屼笖鍙細鐢熸垚涓�娆★紝鍏跺唴瀛樺湴鍧�鐨勬寚鍚戜笉鍙戠敓鍙樺寲

# 鎵�浠ヤ細鏈夐粯璁ょ殑 []

# 1銆�3閮戒細浣跨敤榛樿鐨� 缂栬瘧鐢熸垚鍚屼竴涓猍]

# 2鏂扮敓鎴愪竴涓猍]聽


7銆佷笅闈唬鐮佽皝鎶ラ敊锛�

v1 = {}

v2 = {3:5}

v3 = {[11,23]:5}

v4 = {(11,23):5}

v3閿欒锛屽瓧鍏稿厓绱犵殑閿笉鑳戒负鍙彉瀵硅薄

8銆佽緭鍑轰互涓嬬粨鏋�

for i in range(5,0,1):

聽 聽 print(i)

# range(璧峰鍏冪礌,缁撴潫鍏冪礌锛堜笉鍖呭惈锛�,姣忔鐨勫彉鍖�)

# 5 姣忔鍔�1锛屽暐鏃跺�欒兘鍔犲埌0锛孫(鈭鈭�)O鍝堝搱~

9銆佽璇村嚭range鍜寈range鐨勫尯鍒�


# from collections import Iterable,Iterator

# Iterable鍙凯浠e璞�

# Iterator杩唬鍣�

# print(isinstance(xrange(10),Iterator))

# print(isinstance(xrange(10),Iterable))

# python2涓璻ange(start, stop[, step])鍜宺ange(stop)浼氱敓鎴�-> range object,,杩斿洖鏄垪琛紝鏄彲杩唬瀵硅薄锛屽叏閮ㄦ斁鍦ㄥ唴瀛橀噷

# python2涓瓁range(start, stop[, step])鍜宺ange(stop)浼氱敓鎴�-> xrange object锛岃繑鍥炴槸鍙凯浠e璞★紝姣忔璋冪敤鏃舵墠浼氱敓鎴�

# python3 娌℃湁xrange锛屽彧鏈塺ange锛屼篃鏄彲杩唬瀵硅薄锛屾瘡娆¤皟鐢ㄦ椂鎵嶄細鐢熸垚


10銆佽灏�"1,2,3"锛屽彉鎴怺"1","2","3"]


"1,2,3".split(',')


11銆佷竴琛屼唬鐮佺敓鎴怺1,4,9,16,25,36,49,64,81,100]


[i*i for i in range(11)]


12銆佷竴琛岀敓鎴�9 * 9涔樻硶琛�


[ x聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 for x in range(1,10)]

[ x*y for y in range(1,x+1)]

[ ([ x*y for y in range(1,x+1)]) for x in range(1,10)]

[ ([ '{}*{} = {}'.format(x,y,x*y) for y in range(1,x+1)]) for x in range(1,10)]

'\n'.join([ str(x) for x in range(1,10)])

print('\n'.join([ ' '.join( [ '{}*{} = {}'.format(x,y,x*y) for y in range(1,x+1)] ) for x in range(1,10)]) )


13銆乵ap(str,[1,2,3,4,5,6,7,8,9])杈撳嚭浠�涔堬紵Python2鍜孭ython3杈撳嚭鐨勭粨鏋滀竴鏍峰悧锛�


print(isinstance(map(str,[1,2,3,4,5,6,7,8,9]),Iterable))

print(isinstance(map(str,[1,2,3,4,5,6,7,8,9]),Iterator))

# python 3 鏄疘terator

# python 2 ['1', '2', '3', '4', '5', '6', '7', '8', '9'] ,鏄彲杩唬瀵硅薄锛屼絾涓嶆槸Iterator


14銆丏jango濡備綍鍦∕odel淇濆瓨鍓嶅仛涓�瀹氱殑鍥哄畾鎿嶄綔锛屾瘮濡傚啓涓�鏉℃棩蹇�


鍒╃敤Django鐨凪odel鐨凷ignal Dispatcher,閫氳繃django.db.models.signals.pre_save()鏂规硶锛屽湪浜嬩欢鍙戠敓鍓嶏紝鍙戝皠瑙﹀彂淇″彿锛岃繖涓�鍒囬兘琚皟搴︿腑鐨剅eceiver鏂规硶娣辫棌鍔熶笌鍚嶇殑淇濆瓨浜嗐��

淇″彿鐨勫鐞嗕竴鑸兘鍐欏湪Model涓紝涓句釜渚嬪瓙锛�

import logging

from django.db import models

from django.db.models.signals import pre_save

from django.dispatch import receiver


class Order(models.Model):

聽 聽 # ...


logger = logging.getLogger(__name__)


@receiver(pre_save, sender=Order)

def pre_save_handler(sender, **kwargs):


聽 聽 #鎴戜滑鍙互鍦∣rder杩欎釜Model淇濆瓨涔嬪墠灏芥儏璋冩垙浜嗭細锛�

聽 聽 logger.debug("{},{}".format(sender, **kwargs))

鍙傝�冿細http://python.jobbole.com/81871/

15銆�1,2,3,4,5鑳界粍鎴愬灏戜釜浜掍笉鐩稿悓鐨勬棤閲嶅鐨勪笁浣嶆暟

5 * 4 * 3

寤洪�犺�呮ā寮� 銆�1銆�

涓�銆佸唴瀹�

灏嗕竴涓鏉傚璞$殑鏋勫缓涓庡畠鐨勮〃绀哄垎绂伙紝浣垮緱鍚屾牱鐨勬瀯寤鸿繃绋嬪彲浠ュ垱寤轰笉鍚岀殑琛ㄧず銆傚缓閫犺�呮ā寮忎笌鎶借薄宸ュ巶妯″紡鐩镐技锛屼篃鐢ㄦ潵鍒涘缓澶嶆潅瀵硅薄銆備富瑕佸尯鍒槸寤洪�犺�呮ā寮忕潃閲嶄竴姝ユ鏋勯�犱竴涓鏉傚璞★紝鑰屾娊璞″伐鍘傛ā寮忕潃閲嶄簬澶氫釜绯诲垪鐨勪骇鍝佸璞°��

浜屻�佽鑹�

鎶借薄寤洪�犺�咃紙Builder锛�

鍏蜂綋寤洪�犺�咃紙Concrete Builder锛�

鎸囨尌鑰咃紙Director锛�

浜у搧锛圥roduct锛�

涓夈�佷紭鐐�

闅愯棌浜嗕竴涓骇鍝佺殑鍐呴儴缁撴瀯鍜岃閰嶈繃绋�

灏嗘瀯閫犱唬鐮佷笌琛ㄧず浠g爜鍒嗗紑

鍙互瀵规瀯閫犺繃绋嬭繘琛屾洿绮剧粏鐨勬帶鍒�

鍥涖�侀�傜敤鍦烘櫙

褰撳垱寤哄鏉傚璞$殑绠楁硶锛圖irector锛夊簲璇ョ嫭绔嬩簬璇ュ璞$殑缁勬垚閮ㄥ垎浠ュ強瀹冧滑鐨勮閰嶆柟寮忥紙Builder锛夋椂

褰撴瀯閫犺繃绋嬪厑璁歌鏋勯�犵殑瀵硅薄鏈変笉鍚岀殑琛ㄧず鏃讹紙涓嶅悓Builder锛夈��

浜斻�佸疄渚�

# coding : utf-8

# create by ztypl on 2017/5/25

from abc import abstractmethod, ABCMeta

#------浜у搧------

class Player:

聽 聽 def __init__(self, face=None, body=None, arm=None, leg=None):

聽 聽 聽 聽 self.face = face

聽 聽 聽 聽 self.arm = arm

聽 聽 聽 聽 self.leg = leg

聽 聽 聽 聽 self.body = body

聽 聽 def __str__(self):

聽 聽 聽 聽 return "%s, %s, %s, %s" % (self.face, self.arm, self.body, self.leg)

#------寤洪�犺��------

class PlayerBuilder(metaclass=ABCMeta):

聽 聽 @abstractmethod

聽 聽 def build_face(self):

聽 聽 聽 聽 pass

聽 聽 @abstractmethod

聽 聽 def build_arm(self):

聽 聽 聽 聽 pass

聽 聽 @abstractmethod

聽 聽 def build_leg(self):

聽 聽 聽 聽 pass

聽 聽 @abstractmethod

聽 聽 def build_body(self):

聽 聽 聽 聽 pass

聽 聽 @abstractmethod

聽 聽 def get_player(self):

聽 聽 聽 聽 pass

class BeautifulWomanBuilder(PlayerBuilder):

聽 聽 def __init__(self):

聽 聽 聽 聽 self.player = Player()

聽 聽 def build_face(self):

聽 聽 聽 聽 self.player.face = "婕備寒鑴歌泲"

聽 聽 def build_arm(self):

聽 聽 聽 聽 self.player.arm="缁嗚兂鑶�"

聽 聽 def build_body(self):

聽 聽 聽 聽 self.player.body="缁嗚叞"

聽 聽 def build_leg(self):

聽 聽 聽 聽 self.player.leg="闀胯吙"

聽 聽 def get_player(self):

聽 聽 聽 聽 return self.player

class PlayerDirector:

聽 聽 def build_player(self, builder):

聽 聽 聽 聽 builder.build_body()

聽 聽 聽 聽 builder.build_arm()

聽 聽 聽 聽 builder.build_leg()

聽 聽 聽 聽 builder.build_face()

聽 聽 聽 聽 return builder.get_player()

director = PlayerDirector()

builder = BeautifulWomanBuilder()

p = director.build_player(builder)

print(p)


鍏�乁ML鍥�


闈㈣瘯棰橈紙浜岋級銆�12銆�

1銆佷粈涔堟槸lambda鍑芥暟锛熷畠鏈変粈涔堝ソ澶勶紵鍙﹀python鍦ㄥ嚱鏁扮紪绋嬫柟闈㈡彁渚涗簡浜涗粈涔堝嚱鏁板拰璇硶锛�

lambda鏄疨ython涓殑鍖垮悕鍑芥暟銆傚畠璇硶绠�鍗曪紝绠�鍖栦唬鐮侊紝涓嶄細浜х敓鍛藉悕鍐茬獊锛屾薄鏌撳懡鍚嶇┖闂淬�侾ython鎻愪緵浜唌ap锛宺educe锛宖ilter绛夊嚱鏁版柟娉曪紝鎻愪緵浜嗚楗板櫒锛岄棴鍖呯瓑璇硶

2銆佽缁嗚璇磘uple銆乴ist銆乨ict鐨勭敤娉曪紝瀹冧滑鐨勭壒鐐癸紱

tuple 鍏冪锛屽浐瀹氶暱搴︿笉鍙彉鐨勯『搴忓鍣紝璁块棶鏁堢巼楂橈紝鏄拰瀛樺偍涓�浜涘父閲忔暟鎹紝鍙互浣滀负瀛楀吀鐨勯敭浣跨敤

list 鍒楄〃锛屾槸闀垮害鍙彉鏈夊簭鐨勬暟鎹瓨鍌ㄥ鍣紝銆傚彲浠ラ�氳繃涓嬫爣绱㈠紩鍙栧埌鐩稿簲鐨勬暟鎹�

dict 瀛楀吀锛岄暱搴﹀彲鍙樼殑hash瀛楀吀瀹瑰櫒銆傚瓨鍌ㄧ殑鏂瑰紡涓洪敭鍊煎锛屽彲浠ラ�氳繃鐩稿簲鐨勯敭鑾峰彇鐩稿簲鐨勫�硷紝key鏀寔澶氱绫诲瀷

3銆佽璇磒ython涓楗板櫒銆佽凯浠e櫒鐨勭敤娉曪紱鎻忚堪涓媎ict鐨刬tems()鏂规硶涓巌teritems()鏂规硶鐨勪笉鍚岋紱

瑁呴グ鍣ㄦ槸鎸囧鍑芥暟鎵ц杩囩▼锛屽仛涓�浜涙墿灞曪紝鐢氳嚦鍙互鏇存敼鏈韩鍑芥暟鐨勬墽琛�

杩唬鍣ㄦ槸鎸囬伒寰凯浠e櫒鍗忚鐨勫璞★紝杩欑被瀵硅薄鍦ㄨfor寰幆鏃讹紝姣忔杩唬鐢熸垚涓嬩竴涓」锛屼笉鐢ㄤ竴寮�濮嬪氨鐢熸垚鏁翠釜鍒楄〃

鍦╬ython3涓笉瀛樺湪iteritems锛宨tems鏂规硶杩斿洖鍙凯浠e璞�

鍦╬ython2涓璱tems()杩斿洖[(key,value)]鐨勫垪琛ㄥ璞★紝iteritems()杩斿洖杩唬鍣ㄥ璞★紝iteritems()寰幆鏃朵笉鍙互澧炲垹dict鐨勫唴瀹�

4銆佽璁插unicode, gbk, utf-8绛夌殑鐞嗚В锛宲ython2.x鏄浣曞鐞嗙紪鐮侀棶棰橈紵

unicode缂栫爜锛氫负浜嗚В鍐冲悇涓浗瀹剁殑璇█鐨勫瓨鍌紝寮曡繘鐨剈nicode鐮侊紝鍖呮嫭UCS2,UCS4,UTF-8,UTF-7绛�

gbk锛氬拰unicode涓嶄竴鏍风殑缂栫爜鏂瑰紡锛屽父鐢ㄧ殑涓篻bk-2312

utf-8: UTF-8 浣跨敤 1-4 涓瓧鑺傛潵瀛樺偍鍗曚釜瀛楃锛屽簲璇ユ槸鐩墠鏈�娴佽鐨勫瓧绗﹂泦銆侺inux 榛樿瀛楃闆嗗氨鏄疷TF-8銆傛棦瑙e喅浜嗗ぇ澶氭暟璇█鐨勭紪鐮佹柟寮忥紝鍙堥伩鍏嶄簡绠�鍗曞瓧绗﹀瓨鍌ㄥ绌洪棿鐨勬氮璐�

python 2.x闇�瑕佸湪鏂囦欢涓婃柟澹版槑鏂囦欢鐨勭紪鐮佹柟寮�# -*- coding: utf8 -*-锛屽彲浠ラ�氳繃str.decode/str.encode澶勭悊瀛楃涓茬殑鏂囨湰缂栫爜

淇濊瘉 python 缂栬緫鍣ㄧ殑缂栫爜姝g‘锛屾垨浣跨敤 u鈥欎腑鏂団�欎繚璇佽杞崲鎴� unicode 缂栫爜锛屾帹鑽愪娇鐢�

sys.setdefaultencoding('utf-8')鏉ヤ繚璇佹垜浠殑缂栫爜

5銆丳ython 鏄浣曡繘琛屽唴瀛樼鐞嗙殑锛焢ython 鐨勭▼搴忎細鍐呭瓨娉勯湶鍚楋紵璇磋鏈夋病鏈変粈涔堟柟闈㈤槻姝㈡垨妫�娴嬪唴瀛樻硠闇诧紵

Python GC涓昏浣跨敤 寮曠敤璁℃暟 鏉ヨ窡韪拰鍥炴敹鍨冨溇銆傚湪寮曠敤璁℃暟鐨勫熀纭�涓婏紝閫氳繃鈥滄爣璁�-娓呴櫎鈥濊В鍐冲鍣ㄥ璞″彲鑳戒骇鐢熺殑寰幆寮曠敤闂銆傞�氳繃鍒嗕唬

浠ョ┖闂存崲鏃堕棿鐨勬柟娉曟彁楂樺瀮鍦惧洖鏀舵晥鐜�

1銆佸紩鐢ㄨ鏁帮細 姣忎釜瀵硅薄涓兘鏈塷b-refcnt鏉ュ仛寮曠敤璁℃暟銆傚綋涓�涓璞�...,ob-refcnt灏变細澧炲姞锛屽綋寮曠敤鐨勫璞″垹闄わ紝閭d箞ob-refcnt灏变細鍑忓皯褰搊b-refcnt涓洪浂锛屽氨浼氶噴鏀捐瀵硅薄鐨勫唴瀛樼┖闂�

2銆佹爣璁版竻闄わ細 瑙e喅寰幆寮曠敤鐨勯棶棰樸�傚厛鎸夐渶鍒嗛厤锛岀瓑鍒版病鏈夌┖闂插唴瀛樼殑鏃跺�欙紝浠庡瘎瀛樺櫒鍜岀▼搴忔爤涓婄殑寮曠敤鍑哄彂锛岄亶鍘嗘墍鏈夊璞″拰寮曠敤鎶婃墍鏈夎兘璁块棶鐨勬墦鏍囪锛屾渶鍚庡皢娌℃湁鏍囪鐨勫璞¢噴鏀炬帀

3銆佸垎浠f妧鏈細 鎻愰珮鏁堢巼锛屾彁楂樺瀮鍦惧洖鏀剁殑鏁堢巼锛屾寜鐓у瓨娲绘椂闂达紝鍒嗘垚涓嶅悓鐨勯泦鍚堛�傚皢鍐呭瓨鍧楁寜鐓у叾瀛樻椿鏃堕棿鍒掑垎涓轰笉鍚岀殑闆嗗悎銆傛瘡涓泦鍚堝氨绉颁负涓�涓�滀唬鈥濓紝鍨冨溇鍥炴敹鐨勯鐜囬殢浠g殑瀛樻椿鏃堕棿澧炲ぇ鑰屽噺灏忋�傘�侾ython榛樿瀹氫箟鍒嗕唬瀵硅薄闆嗗悎锛屽紩鐢ㄦ暟瓒婂ぇ锛屽璞$殑瀛樻椿鏃堕棿瓒婇暱

Python涔熶細鍐呭瓨娉勯湶锛孭ython鏈韩鐨勫瀮鍦惧洖鏀舵満鍒舵棤娉曞洖鏀堕噸鍐欎簡__del__鐨勫惊鐜紩鐢ㄧ殑瀵硅薄

绋嬪簭鍛樼鐞嗗ソ姣忎釜python瀵硅薄鐨勫紩鐢紝灏介噺鍦ㄤ笉闇�瑕佷娇鐢ㄥ璞$殑鏃跺�欙紝鏂紑鎵�鏈夊紩鐢�

灏介噺灏戦�氳繃寰幆寮曠敤缁勭粐鏁版嵁锛屽彲浠ユ敼鐢╳eakref鍋氬急寮曠敤鎴栬�呯敤id涔嬬被鐨勫彞鏌勮闂璞�

閫氳繃gc妯″潡鐨勬帴鍙e彲浠ユ鏌ュ嚭姣忔鍨冨溇鍥炴敹鏈夊摢浜涘璞′笉鑳借嚜鍔ㄥ鐞嗭紝鍐嶉�愪釜閫愪釜澶勭悊

6銆佸叧浜� python 绋嬪簭鐨勮繍琛屾�ц兘鏂归潰锛屾湁浠�涔堟墜娈佃兘鎻愬崌鎬ц兘锛�

1銆佷娇鐢ㄥ杩涚▼锛屽厖鍒嗗埄鐢ㄦ満鍣ㄧ殑澶氭牳鎬ц兘

2銆佸浜庢�ц兘褰卞搷杈冨ぇ鐨勯儴鍒嗕唬鐮侊紝鍙互浣跨敤C鎴朇++缂栧啓

3銆佸浜嶪O闃诲閫犳垚鐨勬�ц兘褰卞搷锛屽彲浠ヤ娇鐢↖O澶氳矾澶嶇敤鏉ヨВ鍐�

4銆佸敖閲忎娇鐢╬ython鐨勫唴寤哄嚱鏁�

5銆佸敖閲忎娇鐢ㄥ眬閮ㄥ彉閲�

7銆乴ist瀵硅薄alist [{'name':'a','age':20},{'name':'b','age':30},{'name':'c','age':25}]锛岃鎸塧list涓厓绱犵殑age鐢卞ぇ鍒板皬鎺掑簭锛�

def sort_by_age(list1):

聽 聽 return sorted(alist,key=lambda x:x['age'],reverse=True)

8銆佷袱涓猯ist瀵硅薄alist ['a','b','c','d','e','f'], blist ['x','y','z','d','e','f']锛岃鐢ㄧ畝娲佺殑鏂规硶鍚堝苟杩欎袱涓猯ist锛屽苟涓攍ist閲岄潰鐨勫厓绱犱笉鑳介噸澶嶏紱

alist = ['a','b','c','d','e','f']

blist = ['x','y','z','d','e','f']

def merge_list(*args):

聽 聽 s = set()

聽 聽 for i in args:

聽 聽 聽 聽 s = s.union(i)

聽 聽 return list(s)

9銆佹墦涔变竴涓帓濂藉簭鐨刲ist瀵硅薄alist锛�

import random

random.shuffle(alist)

10銆佺畝鍗曞疄鐜颁竴涓猻tack

class Stack:

聽 聽 def __init__(self):

聽 聽 聽 聽 self.items = []

聽 聽 def __iter__(self):

聽 聽 聽 聽 return self.items.__iter__()

聽 聽 def pop(self):

聽 聽 聽 聽 return self.items.pop()

聽 聽 def top(self):

聽 聽 聽 聽 if len(self.items) > 0:

聽 聽 聽 聽 聽 聽 return self.items[len(self.items) - 1]

聽 聽 def push(self, item):

聽 聽 聽 聽 self.items.append(item)

聽 聽 def empty(self):

聽 聽 聽 聽 self.items = []

聽 聽 def size(self):

聽 聽 聽 聽 return len(self.items)

11銆佽緭鍏ユ煇骞存煇鏈堟煇鏃ワ紝鍒ゆ柇杩欎竴澶╂槸杩欎竴骞寸殑绗嚑澶╋紵(鍙互鐢� python 鏍囧噯搴�)

import datetime

def dayofyear():

聽 聽 year = input("璇疯緭鍏ュ勾浠斤細")

聽 聽 month = input("璇疯緭鍏ユ湀浠斤細")

聽 聽 day = input("璇疯緭鍏ュぉ锛�")

聽 聽 date1 = datetime.date(year=int(year),month=int(month),day=int(day))

聽 聽 date2 = datetime.date(year=int(year),month=1,day=1)

聽 聽 return (date1 -date2).days

12銆佸皢瀛楃涓诧細"k:1|k1:2|k2:3|k3:4"锛屽鐞嗘垚python瀛楀吀锛歿k:1, k1:2, ... }

str1 = "k:1|k1:2|k2:3|k3:4"

def str2dict(str1):

聽 聽 dict1 = {}

聽 聽 for iterms in str1.split('|'):

聽 聽 聽 聽 key,value = iterms.split(':')

聽 聽 聽 聽 dict1[key] = value

聽 聽 return dict1


闈㈣瘯棰橈紙涓夛級銆�1銆�

璇峰畬鎴愪竴涓▼搴忥紝骞惰兘鎸夋楠ゅ疄鐜颁互涓嬪姛鑳斤細

1.涓嬭浇https://en.wikipedia.org/wiki/Machine_translation椤甸潰鐨勫唴瀹瑰苟淇濆瓨涓簃t.html

聽聽聽聽聽聽 闇�瑕佺紪鍐欎唬鐮佹潵涓嬭浇椤甸潰銆�

2.缁熻mt.html涓�

鏍囩鍐呬笅鎵�鏈夊崟璇嶄互鍙婃暟鐩苟瀛樺偍鍒癿t_word.txt涓��

mt_word.txt鏈夊涓嬪嚑鐐硅姹傦細

聽聽聽聽聽聽 a) 姣忎釜鍗曡瘝涓�琛屻�傚崟璇嶅湪鍓嶏紝鍗曡瘝鍑虹幇鐨勬鏁板湪鍚庯紝涓棿鐢═ab(\t)杩涜鍒嗛殧銆�

聽聽聽聽聽聽 b) 鍗曡瘝瑕佹寜鐓у崟璇嶆暟鐩粠澶氬埌灏戠殑椤哄簭杩涜鎺掑垪銆傛瘮濡傝鍗曡瘝a鍑虹幇浜�100娆★紝鍗曡瘝b鍑虹幇浜�10娆★紝鍒欏崟璇峚瑕佸湪鍗曡瘝b鐨勫墠闈€��

3. 鎻愬彇鍑簃t.html涓墍鏈夌殑骞翠唤淇℃伅锛堟瘮濡傝椤甸潰涓殑1629, 1951杩欎簺鐨勫洓浣嶆暟瀛楀氨鏄勾浠斤級瀛樺偍鍒癿t_year.txt涓��

mt_year.txt鏈夊涓嬪嚑鐐硅姹傦細

a)姣忎釜骞翠唤鏄竴琛屻��

聽聽聽聽聽聽 a) 骞翠唤闇�瑕佷粠杩囧幓鍒扮幇鍦ㄧ殑椤哄簭杩涜鎺掑垪銆傛瘮濡傝鏂囩珷涓嚭鐜颁簡2007鍜�1997锛屽垯1997闇�瑕佹帓鍦�2007鐨勫墠闈€��


瑕佹眰锛�

1. 浠呴檺python缂栫▼锛岃�屼笖浠呬粎鍙互浣跨敤python鑷甫鐨勫嚱鏁版垨搴撱��

2.鎻愪氦鍙墽琛岀殑绋嬪簭浠ュ強mt.html, mt_word.txt, mt_year.txt銆�

3. 闄愬畾鍦ㄤ竴涓皬鏃跺唴瀹屾垚銆�

# 1.涓嬭浇https://en.wikipedia.org/wiki/Machine_translation椤甸潰鐨勫唴瀹瑰苟淇濆瓨涓簃t.html闇�瑕佺紪鍐欎唬鐮佹潵涓嬭浇椤甸潰銆�

session = requests.session()

response = session.get(url="https://en.wikipedia.org/wiki/Machine_translation")

with open('mt.html','wb') as f:

聽 聽 f.write(response.content)

# 2銆佺粺璁t.html涓�

鏍囩鍐呬笅鎵�鏈夊崟璇嶄互鍙婃暟鐩苟瀛樺偍鍒癿t_word.txt涓�

# 瑙f瀽椤甸潰锛屾嬁鍒版墍鏈夌殑p鏍囩涓殑鏂囨湰

soup = BeautifulSoup(response.text,features="lxml")

tag2 = soup.find_all(name='p')

list_p = []

for i in tag2:

聽 聽 list_p.append(i.get_text())

# 灏嗘墍鏈夌殑鏂囨湰鍚堝苟鎴愪竴涓瓧绗︿覆

str_p = ' '.join(list_p)

word_set = set()

for word in str_p.split():

聽 聽 word = word.strip(',.()""/; ')

聽 聽 word_set.add(word)

# word_dict = {}

word_list = []

for word in word_set:

聽 聽 if word == '':

聽 聽 聽 聽 continue

聽 聽 # word_dict[word] = str_p.count(word)

聽 聽 dict2 = {word:str_p.count(word)}

聽 聽 word_list.append(dict2)

# 灏嗗崟璇嶆寜鐓ф暟鐩弽搴忔帓鍒楋紝鐒跺悗鍐欏叆鏂囦欢

blist = sorted(word_list,key = lambda x:list(x.values())[0],reverse =True)

with open('mt_word.txt','w') as f:

聽 聽 for item in blist:

聽 聽 聽 聽 for k,v in item.items():

聽 聽 聽 聽 聽 聽 line = k + '\t' + str(v) + '\n'

聽 聽 聽 聽 聽 聽 f.write(line)


# 3銆佹彁鍙栧嚭mt.html涓墍鏈夌殑骞翠唤淇℃伅锛堟瘮濡傝椤甸潰涓殑1629, 1951杩欎簺鐨勫洓浣嶆暟瀛楀氨鏄勾浠斤級瀛樺偍鍒癿t_year.txt涓�

year = re.compile(r'\d{4}')

years_list = re.findall(year,response.text)

years_list = sorted(list(set(years_list)))

with open('mt_year.txt','w') as f:

聽 聽 for year in years_list:

聽 聽 聽 聽 line = year + '\n'

聽 聽 聽 聽 f.write(line)

绛旀

闈㈣瘯棰橈紙鍥涳級鐖櫕銆�16銆�

涓�銆佽瘯鍒楀嚭鑷冲皯涓夌鐩墠娴佽鐨勫ぇ鍨嬫暟鎹簱鐨勫悕绉�:________銆乢________銆乢_________,鍏朵腑鎮ㄦ渶鐔熸倝鐨勬槸__________,浠巁_________骞村紑濮嬩娇鐢ㄣ��

Oracle锛孧ysql锛孲QLServer聽 Oracle鏍规嵁鑷繁鎯呭喌

浜屻�佹湁琛↙ist锛屽苟鏈夊瓧娈礎銆丅銆丆锛岀被鍨嬮兘鏄暣鏁般�傝〃涓湁濡備笅鍑犳潯璁板綍锛�

ABC

279

564

3119

鐜板湪瀵硅琛ㄤ竴娆″畬鎴愪互涓嬫搷浣滐細

鏌ヨ鍑築鍜孋鍒楃殑鍊硷紝瑕佹眰鎸塀鍒楀崌搴忔帓鍒�

鍐欏嚭涓�鏉℃柊鐨勮褰曪紝鍊间负{7,9,8}

鏌ヨC鍒楋紝瑕佹眰娑堥櫎閲嶅鐨勫�硷紝鎸夐檷搴忔帓鍒�

鍐欏嚭瀹屾垚瀹屾垚浠ヤ笂鎿嶄綔鐨勬爣鍑嗙殑SQL璇彞锛屽苟涓斿啓鍑烘搷浣�3鐨勭粨鏋溿��

create table List2(A int ,B int,C int)

select B,C from List order by B

insert into List values(7,9,8)

select distinct(C) from List order by 1 desc;

9 8 4

涓夈�佽绠�瑕佽鏄庤鍥剧殑浣滅敤

1. 鏁版嵁搴撹鍥鹃殣钘忎簡鏁版嵁鐨勫鏉傛�с��

2. 鏁版嵁搴撹鍥炬湁鍒╀簬鎺у埗鐢ㄦ埛瀵硅〃涓煇浜涘垪鐨勮闂��

3. 鏁版嵁搴撹鍥句娇鐢ㄦ埛鏌ヨ鍙樺緱绠�鍗曘��

鍥涖�佸垪涓炬偍浣跨敤杩囩殑python缃戠粶鐖櫕鎵�鐢ㄥ埌鐨勭綉缁滄暟鎹寘锛堟渶鐔熸倝鐨勫湪鍓嶏級锛�

requests銆乽rllib銆乽rllib2銆乭ttplib2

浜斻�佸垪涓炬偍浣跨敤杩囩殑python缃戠粶鐖櫕鎵�鐢ㄥ埌鐨勮В鏋愭暟鎹寘锛堟渶鐔熸倝鐨勫湪鍓嶏級锛�

BeautifulSoup銆乸yquery銆乆path銆乴xml

鍏�佸垪涓炬偍浣跨敤杩囩殑python涓殑缂栫爜鏂瑰紡锛堟渶鐔熸倝鐨勫湪鍓嶏級锛�

UTF-8锛孉SCII锛実bk

涓冦�乸ython3.5璇█涓璭numerate鐨勬剰鎬濇槸_______________________

瀵逛簬涓�涓彲杩唬鐨勶紙iterable锛�/鍙亶鍘嗙殑瀵硅薄锛堝鍒楄〃銆佸瓧绗︿覆锛夛紝enumerate灏嗗叾缁勬垚涓�涓储寮曞簭鍒楋紝鍒╃敤瀹冨彲浠ュ悓鏃惰幏寰楃储寮曞拰鍊�

enumerate澶氱敤浜庡湪for寰幆涓緱鍒拌鏁�

鍏��99鐨勫叓杩涘埗琛ㄧず鏄痏______________________

143

涔濄�佽涓惧嚭涓夌甯哥敤鐨勬帓搴忕畻娉�

鍐掓场銆侀�夋嫨銆佸揩閫�

鍗併�佸垪鍑烘瘮杈冪啛鎮夌殑鐖櫕妗嗘灦

Scrapy

鍗佷竴銆佺敤4銆�9銆�2銆�7鍥涗釜鏁板瓧锛屽彲浠ヤ娇鐢�+銆�-銆�*鍜�/锛屾瘡涓暟瀛椾娇鐢ㄤ竴娆★紝浣胯〃杈惧紡鐨勭粨鏋滀负24锛岃〃杈惧紡鏄痏____________________________

锛�9+7-4锛�*2

鍗佷簩銆伮犅犅犅� 瀵逛綘鏈�鏈夊奖鍝嶇殑鎴栨槸鎮ㄨ涓烘渶鏈変环鍊肩殑杞欢鏂归潰鐨勫嚑鏈功鏄紵

鍗佷笁銆佹偍鏈�鐔熸倝鐨刄nix鐜鏄痏____________.Unix涓嬫煡璇㈢幆澧冨彉閲忕殑鍛戒护鏄痏_______,鏌ヨ鑴氭湰瀹氭椂浠诲姟鐨勫懡浠ゆ槸____________________

1 AIX锛宔nv聽 crontab

鍗佸洓銆伮犅犅犅� 鍐欏嚭鍦ㄧ綉缁滅埇铏埇鍙栨暟鎹殑杩囩▼涓紝閬囧埌鐨勯槻鐖櫕闂鐨勮В鍐虫柟妗�

聽閫氳繃headers鍙嶇埇铏細瑙e喅绛栫暐锛屼吉閫爃eaders

聽鍩轰簬鐢ㄦ埛琛屼负鍙嶇埇铏細鍔ㄦ�佸彉鍖栧幓鐖彇鏁版嵁锛屾ā鎷熸櫘閫氱敤鎴风殑琛屼负

聽鍩轰簬鍔ㄦ�侀〉闈㈢殑鍙嶇埇铏細璺熻釜鏈嶅姟鍣ㄥ彂閫佺殑ajax璇锋眰锛屾ā鎷焌jax璇锋眰

鍗佷簲銆侀槄璇讳互涓婸ython绋嬪簭

for i in range(5,0,-1):

聽聽聽聽聽 print(i)

璇峰湪涓嬮潰鍐欏嚭鎵撳嵃缁撴灉

5 4 3 2 1

鍗佸叚銆佸湪鏌愮郴缁熶腑涓�涓暣鏁板崰鐢ㄤ袱涓叓浣嶅瓧鑺傦紝浣跨敤Python鎸変笅闈㈢殑瑕佹眰缂栧啓瀹屾暣绋嬪簭銆�

鎺ユ敹浠庢爣鍑嗚緭鍏ヤ腑渚濇杈撳叆鐨勪簲涓暟瀛楋紝灏嗗叾缁勫悎鎴愪负涓�涓暣鏁帮紝鏀惧叆鍏ㄥ眬鍙橀噺n涓紝闅忓悗鍦ㄦ爣鍑嗚緭鍑鸿緭鍑鸿繖涓暣鏁般�傦紙ord(char)鑾峰彇瀛楃ASCII鍊肩殑鍑芥暟锛�

闈㈣瘯棰橈紙浜旓級鐖櫕 銆�14銆�

1銆乸ost銆乬et鏈変粈涔堝尯鍒紵

1. 鏍规嵁HTTP瑙勮寖锛孏ET涓�鑸敤浜庤幏鍙�/鏌ヨ璧勬簮淇℃伅锛屽簲璇ユ槸瀹夊叏鐨勫拰骞傜瓑銆傝�孭OST涓�鑸敤浜庢洿鏂拌祫婧愪俊鎭�

2. get鏄湪url涓紶閫掓暟鎹紝鏁版嵁鏀惧湪璇锋眰澶翠腑銆� post鏄湪璇锋眰浣撲腑浼犻�掓暟鎹�

3. get浼犻�佺殑鏁版嵁閲忚緝灏忥紝鍙兘鍦ㄨ姹傚ご涓婂彂閫佹暟鎹�俻ost浼犻�佺殑鏁版嵁閲忚緝澶э紝涓�鑸榛樿涓轰笉鍙楅檺鍒躲��

5. get瀹夊叏鎬ч潪甯镐綆锛宲ost瀹夊叏鎬ц緝楂樸�備絾鏄墽琛屾晥鐜囧嵈姣擯ost鏂规硶濂姐��

寤鸿锛�

1銆乬et鏂瑰紡鐨勫畨鍏ㄦ�ц緝Post鏂瑰紡瑕佸樊浜涳紝鍖呭惈鏈哄瘑淇℃伅鐨勮瘽锛屽缓璁敤Post鏁版嵁鎻愪氦鏂瑰紡锛�

2銆佸湪鍋氭暟鎹煡璇㈡椂锛屽缓璁敤Get鏂瑰紡锛涜�屽湪鍋氭暟鎹坊鍔犮�佷慨鏀规垨鍒犻櫎鏃讹紝寤鸿鐢≒ost鏂瑰紡锛�

2銆乭ttp銆乭ttps鍗忚鏈変粈涔堝尯鍒紵

http鍗忚鏄秴鏂囨湰浼犺緭鍗忚锛岃鐢ㄤ簬鍦╳eb娴忚鍣ㄥ拰缃戠珯鏈嶅姟鍣ㄤ箣闂翠紶閫掍俊鎭�俬ttp鍗忚宸ヤ綔鏄互鏄庢枃鏂瑰紡鍙戦�佸唴瀹癸紝涓嶆彁渚涗换浣曞舰寮忕殑鏁版嵁鍔犲瘑锛岃�岃繖涔熸槸寰堝鏄撹榛戝鍒╃敤鐨勫湴鏂癸紝濡傛灉榛戝鎴彇浜唚eb娴忚鍣ㄥ拰缃戠珯鏈嶅姟鍣ㄤ箣闂寸殑浼犺緭淇℃伅锛屽氨鍙互鐩存帴璇绘噦鍏朵腑鐨勪俊鎭紝鍥犳http鍗忚涓嶉�傚悎浼犺緭涓�浜涢噸瑕佺殑銆佹晱鎰熺殑淇℃伅锛屾瘮濡備俊鐢ㄥ崱瀵嗙爜鍙婃敮浠橀獙璇佺爜绛夈��

瀹夊叏濂楁帴瀛楀眰https鍗忚灏辨槸涓轰簡瑙e喅http鍗忚鐨勮繖涓�瀹夊叏缂洪櫡鑰屽嚭鐢熺殑锛屼负浜嗘暟鎹紶杈撶殑瀹夊叏锛宧ttps鍦╤ttp鐨勫熀纭�涓婂姞鍏ヤ簡ssl鍗忚锛宻sl渚濋潬璇佷功鏉ラ獙璇佹湇鍔″櫒鐨勮韩浠斤紝涓烘祻瑙堝櫒鍜屾湇鍔″櫒涔嬮棿鐨勯�氫俊鍔犲瘑锛岃繖鏍风殑璇濆嵆浣块粦瀹㈠�熷幓浜嗗彂閫佽繃绋嬩腑鐨勪俊鎭紝涔熸棤娉曠牬瑙h鎳傚畠锛屾垜浠綉绔欏強鐢ㄦ埛鐨勪俊鎭究寰楀埌浜嗘渶澶х殑瀹夊叏淇濋殰銆�

https鍗忚闇�瑕佸埌ca鐢宠璇佷功锛屼竴鑸厤璐硅瘉涔﹀緢灏戯紝闇�瑕佽垂鐢ㄣ��

http鏄秴鏂囨湰浼犺緭鍗忚锛屼俊鎭槸鏄庢枃浼犺緭锛宧ttps 鍒欐槸鍏锋湁瀹夊叏鎬х殑ssl鍔犲瘑浼犺緭鍗忚

http鍜宧ttps浣跨敤鐨勬槸瀹屽叏涓嶅悓鐨勮繛鎺ユ柟寮忕敤鐨勭鍙d篃涓嶄竴鏍�,鍓嶈�呮槸80,鍚庤�呮槸443銆�

http鐨勮繛鎺ュ緢绠�鍗�,鏄棤鐘舵�佺殑锛� HTTPS鍗忚鏄敱SSL+HTTP鍗忚鏋勫缓鐨勫彲杩涜鍔犲瘑浼犺緭銆佽韩浠借璇佺殑缃戠粶鍗忚 瑕佹瘮http鍗忚瀹夊叏

3銆� 鍩熷悕鍜孖P涔嬮棿鏈変粈涔堝叧绯伙紝濡備綍鏌ョ湅鏌愪釜鍩熷悕瀵瑰簲鐨勬墍鏈塈P锛�

鍥介檯浜掕仈缃�(Internet)涓婃湁鎴愬崈鐧句竾鍙颁富鏈猴紙host锛夛紝涓轰簡鍖哄垎杩欎簺涓绘満锛屼汉浠粰姣忓彴涓绘満閮藉垎閰嶄簡涓�涓笓闂ㄧ殑鈥滃湴鍧�鈥濅綔涓烘爣璇嗭紝绉颁负IP鍦板潃

鐢变簬IP鍦板潃鍏ㄦ槸浜涚殑鏁板瓧锛屼负浜嗕究浜庣敤鎴疯蹇嗭紝Internet涓婂紩杩涗簡鍩熷悕鏈嶅姟绯荤粺DNS锛圖omain Name System锛夈��

褰撴偍閿叆鏌愪釜鍩熷悕鐨勬椂鍊欙紝杩欎釜淇℃伅棣栧厛鍒拌揪鎻愪緵姝ゅ煙鍚嶈В鏋愮殑鏈嶅姟鍣ㄤ笂锛屽啀灏嗘鍩熷悕瑙f瀽涓虹浉搴旂綉绔欑殑IP鍦板潃銆傚畬鎴愯繖涓�浠诲姟鐨勮繃绋嬪氨绉颁负鍩熷悕瑙f瀽銆�

1.ping聽 2.nslookup 3.浣跨敤绔欓暱宸ュ叿绛�

4銆乭ttp鍗忚澶翠腑锛宬eep-alive瀛楁鏈変粈涔堜綔鐢紵

HTTP鍗忚閲囩敤鈥滆姹�-搴旂瓟鈥濇ā寮忥紝褰撲娇鐢ㄦ櫘閫氭ā寮忥紝鍗抽潪KeepAlive妯″紡鏃讹紝姣忎釜璇锋眰/搴旂瓟瀹㈡埛鍜屾湇鍔″櫒閮借鏂板缓涓�涓繛鎺ワ紝瀹屾垚 涔嬪悗绔嬪嵆鏂紑杩炴帴锛圚TTP鍗忚涓烘棤杩炴帴鐨勫崗璁級锛�

褰撲娇鐢↘eep-Alive妯″紡锛堝張绉版寔涔呰繛鎺ャ�佽繛鎺ラ噸鐢級鏃讹紝Keep-Alive鍔熻兘浣垮鎴风鍒版湇 鍔″櫒绔殑杩炴帴鎸佺画鏈夋晥锛屽綋鍑虹幇瀵规湇鍔″櫒鐨勫悗缁ц姹傛椂锛孠eep-Alive鍔熻兘閬垮厤浜嗗缓绔嬫垨鑰呴噸鏂板缓绔嬭繛鎺ャ��

閫氳繃浣跨敤keep-alive鏈哄埗锛屽彲浠ュ噺灏憈cp杩炴帴寤虹珛娆℃暟锛屼篃鎰忓懗鐫�鍙互鍑忓皯TIME_WAIT鐘舵�佽繛鎺ワ紝浠ユ鎻愰珮鎬ц兘鍜屾彁楂榟ttpd鏈嶅姟鍣ㄧ殑鍚炲悙鐜�(鏇村皯鐨則cp杩炴帴鎰忓懗鐫�鏇村皯鐨勭郴缁熷唴鏍歌皟鐢�,socket鐨刟ccept()鍜宑lose()璋冪敤)銆�

5銆乺obots鍗忚鏄粈涔堬紵

Robots鍗忚锛堜篃绉颁负鐖櫕鍗忚銆佺埇铏鍒欍�佹満鍣ㄤ汉鍗忚绛夛級涔熷氨鏄痳obots.txt锛岀綉绔欓�氳繃robots鍗忚鍛婅瘔鎼滅储寮曟搸鍝簺椤甸潰鍙互鎶撳彇锛屽摢浜涢〉闈笉鑳芥姄鍙栥��

Robots鍗忚鏄綉绔欏浗闄呬簰鑱旂綉鐣岄�氳鐨勯亾寰疯鑼冿紝鍏剁洰鐨勬槸淇濇姢缃戠珯鏁版嵁鍜屾晱鎰熶俊鎭�佺‘淇濈敤鎴蜂釜浜轰俊鎭拰闅愮涓嶈渚电姱銆傚洜鍏朵笉鏄懡浠わ紝鏁呴渶瑕佹悳绱㈠紩鎿庤嚜瑙夐伒瀹堛��

6銆� 鍒楀嚭鍑犵甯歌鐨勫叧绯诲瀷鏁版嵁搴撳拰闈炲叧绯诲瀷鏁版嵁搴擄紵锛堟瘡绉嶈嚦灏戜袱涓級

Oracle銆丮ysql銆丼QLServer銆丏B2聽 聽 聽 聽 聽 Redis MongoDB Cassandra

7銆� 鍐呭瓨娉勯湶鏄粈涔堬紵濡備綍閬垮厤锛�

鎸囩敱浜庣枏蹇芥垨閿欒閫犳垚绋嬪簭鏈兘閲婃斁宸茬粡涓嶅啀浣跨敤鐨勫唴瀛樼殑鎯呭喌銆�

鍐呭瓨娉勬紡骞堕潪鎸囧唴瀛樺湪鐗╃悊涓婄殑娑堝け锛岃�屾槸搴旂敤绋嬪簭鍒嗛厤鏌愭鍐呭瓨鍚庯紝鐢变簬璁捐閿欒锛屽け鍘讳簡瀵硅娈靛唴瀛樼殑鎺у埗锛屽洜鑰岄�犳垚浜嗗唴瀛樼殑娴垂銆�

瀵艰嚧绋嬪簭杩愯閫熷害鍑忔參鐢氳嚦绯荤粺宕╂簝绛変弗閲嶅悗鏋溿��

鏈� __del__() 鍑芥暟鐨勫璞¢棿鐨勫惊鐜紩鐢ㄦ槸瀵艰嚧鍐呭瓨娉勬紡鐨勪富鍑躲��

涓嶄娇鐢ㄤ竴涓璞℃椂浣跨敤:del object 鏉ュ垹闄や竴涓璞$殑寮曠敤璁℃暟灏卞彲浠ユ湁鏁堥槻姝㈠唴瀛樻硠婕忛棶棰�.

閫氳繃 Python 鎵╁睍妯″潡 gc 鏉ユ煡鐪嬩笉鑳藉洖鏀剁殑瀵硅薄鐨勮缁嗕俊鎭�

鍙互閫氳繃 sys.getrefcount(obj) 鏉ヨ幏鍙栧璞$殑寮曠敤璁℃暟锛屽苟鏍规嵁杩斿洖鍊兼槸鍚︿负 0 鏉ュ垽鏂槸鍚﹀唴瀛樻硠婕�

Python鐨勫唴瀛樼鐞嗘満鍒�

1.寮曠敤璁℃暟锛氶�氳繃寮曠敤璁℃暟鏉ヤ繚鎸佸鍐呭瓨涓殑鍙橀噺璺熻釜锛孭ython鍐呴儴璁板綍涓墍鏈夊湪浣跨敤瀵硅薄鍚勬湁澶氬皯涓紩鐢ㄣ��

Python涓湁涓唴閮ㄨ窡韪彉閲忓彨鍋氬紩鐢ㄨ鏁板櫒锛屾瘡涓彉閲忔湁澶氬皯涓紩鐢紝绠�绉板紩鐢ㄨ鏁般�傚綋瀵硅薄琚垱寤烘椂灏卞垱寤轰簡涓�涓紩鐢ㄨ鏁般��

褰撴煇涓璞$殑寮曠敤璁℃暟涓�0鏃讹紝瀵硅薄灏变笉鍦ㄩ渶瑕侊紝灏卞垪鍏ヤ簡鍨冨溇鍥炴敹闃熷垪銆�

寮曠敤璁℃暟澧炲姞锛�1.瀵硅薄琚垱寤猴細x=4;2.鍙﹀鐨勫埆浜鸿鍒涘缓锛歽=x;3.琚綔涓哄弬鏁颁紶閫掔粰鍑芥暟锛歠oo(x);4.浣滀负瀹瑰櫒瀵硅薄鐨勪竴涓厓绱狅細a=[1,x,'33'];

寮曠敤璁℃暟鍑忓皯鏃讹細1.涓�涓湰鍦板紩鐢ㄧ寮�浜嗗畠鐨勪綔鐢ㄥ煙銆傛瘮濡備笂闈㈢殑foo(x)鍑芥暟缁撴潫鏃讹紝x鎸囧悜鐨勫璞″紩鐢ㄥ噺1;

聽 聽 聽 聽 聽 聽 聽 聽 2.瀵硅薄鐨勫埆鍚嶈鏄惧紡鐨勯攢姣侊細del x锛涙垨鑰卍el y;

聽 聽 聽 聽 聽 聽 聽 聽 3.瀵硅薄鐨勪竴涓埆鍚嶈璧嬪�肩粰鍏朵粬瀵硅薄锛歺=789

聽 聽 聽 聽 聽 聽 聽 聽 4.瀵硅薄浠庝竴涓獥鍙e璞′腑绉婚櫎锛歮yList.remove(x)

聽 聽 聽 聽 聽 聽 聽 聽 5.绐楀彛瀵硅薄鏈韩琚攢姣侊細del myList锛屾垨鑰呯獥鍙e璞℃湰韬寮�浜嗕綔鐢ㄥ煙銆�

2.鍨冨溇鍥炴敹

1.寮曠敤璁℃暟锛� 姣忎釜瀵硅薄涓兘鏈塷b-refcnt鏉ュ仛寮曠敤璁℃暟銆傚綋涓�涓璞�...,ob-refcnt灏变細澧炲姞锛屽綋寮曠敤鐨勫璞″垹闄わ紝閭d箞ob-refcnt灏变細鍑忓皯褰搊b-refcnt涓洪浂锛屽氨浼氶噴鏀捐瀵硅薄鐨勫唴瀛樼┖闂�

2.鏍囪娓呴櫎锛� 瑙e喅寰幆寮曠敤鐨勯棶棰樸�傚厛鎸夐渶鍒嗛厤锛岀瓑鍒版病鏈夌┖闂插唴瀛樼殑鏃跺�欙紝浠庡瘎瀛樺櫒鍜岀▼搴忔爤涓婄殑寮曠敤鍑哄彂锛岄亶鍘嗘墍鏈夊璞″拰寮曠敤鎶婃墍鏈夎兘璁块棶鐨勬墦鏍囪锛屾渶鍚庡皢娌℃湁鏍囪鐨勫璞¢噴鏀炬帀

3.鍒嗕唬鎶�鏈細 鎻愰珮鏁堢巼锛屾彁楂樺瀮鍦惧洖鏀剁殑鏁堢巼锛屾寜鐓у瓨娲绘椂闂达紝鍒嗘垚涓嶅悓鐨勯泦鍚堛�傚皢鍐呭瓨鍧楁寜鐓у叾瀛樻椿鏃堕棿鍒掑垎涓轰笉鍚岀殑闆嗗悎銆傛瘡涓泦鍚堝氨绉颁负涓�涓�滀唬鈥濓紝鍨冨溇鍥炴敹鐨勯鐜囬殢浠g殑瀛樻椿鏃堕棿澧炲ぇ鑰屽噺灏忋�傘�侾ython榛樿瀹氫箟鍒嗕唬瀵硅薄闆嗗悎锛屽紩鐢ㄦ暟瓒婂ぇ锛屽璞$殑瀛樻椿鏃堕棿瓒婇暱

3.鍐呭瓨姹犳満鍒�

鍦≒ython涓紝澶у鏁扮敵璇风殑閮芥槸灏忓潡鐨勫唴瀛橈紝浼氭墽琛屽ぇ閲忕殑malloc鍜宖ree鎿嶄綔銆侾ython寮曞叆浜嗕竴涓唴瀛樻睜鏈哄埗锛岀敤浜庣鐞嗗灏忓潡鍐呭瓨鐨勭敵璇峰拰閲婃斁锛屽嵆Pymalloc鏈哄埗銆�

瀹冨皢涓嶇敤鐨勫唴瀛樻斁鍒板唴瀛樻睜鑰屼笉鏄繑鍥炵粰鎿嶄綔绯荤粺銆�

1. 褰撶敵璇风殑鍐呭瓨灏忎簬256瀛楄妭鏃讹紝PyObject_Malloc浼氬湪鍐呭瓨姹犱腑鐢宠鍐呭瓨锛涘綋鐢宠鐨勫唴瀛樺ぇ浜�256瀛楄妭鏃讹紝PyObject_Malloc鐨勮涓哄皢铚曞寲涓簃alloc鐨勮涓恒�傚綋鐒讹紝閫氳繃淇敼Python婧愪唬鐮侊紝鎴戜滑鍙互鏀瑰彉杩欎釜榛樿鍊硷紝浠庤�屾敼鍙楶ython鐨勯粯璁ゅ唴瀛樼鐞嗚涓恒��

2. 瀵逛簬Python瀵硅薄锛屽鏁存暟锛屾诞鐐规暟鍜孡ist锛岄兘鏈夊叾鐙珛鐨勭鏈夊唴瀛樻睜锛屽璞¢棿涓嶅叡浜粬浠殑鍐呭瓨姹犮�備篃灏辨槸璇村鏋滀綘鍒嗛厤鍙堥噴鏀句簡澶ч噺鐨勬暣鏁帮紝鐢ㄤ簬缂撳瓨杩欎簺鏁存暟鐨勫唴瀛樺氨涓嶈兘鍐嶅垎閰嶇粰娴偣鏁般��

8銆� 鍒椾妇鍑犱釜甯哥敤鐨刣om瑙f瀽椤圭洰銆佹彃浠�

xml銆乴ibxml2銆乴xml銆亁path

9銆� 甯歌鐨勫弽鐖櫕鏈哄埗鏈夊摢浜涳紵

閫氳繃headers鍙嶇埇铏細瑙e喅绛栫暐锛屼吉閫爃eaders

鍩轰簬鐢ㄦ埛琛屼负鍙嶇埇铏細鍔ㄦ�佸彉鍖栧幓鐖彇鏁版嵁锛屾ā鎷熸櫘閫氱敤鎴风殑琛屼负

鍩轰簬鍔ㄦ�侀〉闈㈢殑鍙嶇埇铏細璺熻釜鏈嶅姟鍣ㄥ彂閫佺殑ajax璇锋眰锛屾ā鎷焌jax璇锋眰

10銆佸浣曟彁楂樼埇鍙栨晥鐜囷紵

1. 鐖彇鏂归潰锛屽埄鐢ㄥ紓姝o銆�

2.澶勭悊鏂归潰锛屽埄鐢ㄦ秷鎭槦鍒楀仛鐢熶骇鑰呮秷璐硅�呮ā鍨�


11銆佹ゼ姊棶棰�

1.1銆佺粰涓�涓ゼ姊紝浠庢渶涓嬮潰寰�涓婅蛋锛屾瘡娆″彲浠ヨ蛋1鍒皀姝ワ紝姹傛�诲叡鏈夊灏戠璧版硶锛�

1.2銆佺粰涓�涓ゼ姊紝浠庢渶涓嬮潰寰�涓婅蛋锛屾瘡娆″彲浠ヨ蛋1姝ユ垨2姝ワ紝姹傛�诲叡鏈夊灏戠璧版硶锛�

1.3銆佺粰涓�涓ゼ姊紝浠庢渶涓嬮潰寰�涓婅蛋锛屾瘡娆″彲浠ヨ蛋1姝ユ垨2姝ユ垨3姝ワ紝姹傛�诲叡鏈夊灏戠璧版硶锛�

# 1銆� 缁欎竴涓ゼ姊紝浠庢渶涓嬮潰寰�涓婅蛋锛屾瘡娆″彲浠ヨ蛋1鎴�2姝ワ紝姹傛�诲叡鏈夊灏戠璧版硶锛�

# 鍋囪鍙湁涓�涓彴闃讹紝閭d箞鍙湁涓�绉嶈烦娉曪紝閭e氨鏄竴娆¤烦涓�绾э紝f(1)=1锛涘鏋滄湁涓や釜鍙伴樁锛岄偅涔堟湁涓ょ璺虫硶锛岀涓�绉嶈烦娉曟槸涓�娆¤烦涓�绾э紝绗簩绉嶈烦娉曟槸涓�娆¤烦涓ょ骇,f(2)=2銆�

# 濡傛灉鏈夊ぇ浜�2绾х殑n绾у彴闃讹紝閭d箞鍋囧绗竴娆¤烦涓�绾у彴闃讹紝鍓╀笅杩樻湁n-1绾у彴闃讹紝鏈塮(n-1)绉嶈烦娉曪紝鍋囧绗竴娆℃潯2绾у彴闃讹紝鍓╀笅n-2绾у彴闃讹紝鏈塮(n-2)绉嶈烦娉曘�傝繖灏辫〃绀篺(n)=f(n-1)+f(n-2)銆�

def walk_stairs(stairs):

聽 聽 if stairs == 1:

聽 聽 聽 聽 return 1

聽 聽 if stairs == 2:

聽 聽 聽 聽 return 2

聽 聽 else:

聽 聽 聽 聽 return walk_stairs(stairs-1) + walk_stairs(stairs-2)

# 2銆佺粰涓�涓ゼ姊紝浠庢渶涓嬮潰寰�涓婅蛋锛屾瘡娆″彲浠ヨ蛋1鍒皀姝ワ紝姹傛�诲叡鏈夊灏戠璧版硶锛�

#涓�1涓彴闃�1

#涓�2涓彴闃�2

#涓�3涓彴闃�4

#涓�4涓彴闃�8

#涓妌涓彴闃�2^(n-1)

# 3銆佺粰涓�涓ゼ姊紝浠庢渶涓嬮潰寰�涓婅蛋锛屾瘡娆″彲浠ヨ蛋1姝ユ垨2姝ユ垨3姝ワ紝姹傛�诲叡鏈夊灏戠璧版硶锛�

#涓�1涓彴闃�2^(1-1)

#涓�2涓彴闃�2^(2-1)

#涓�3涓彴闃�2^(3-1)

# f(n) = f(n-1) + f(n-2) + f(n-3)

12銆� 缁欎竴涓瓧绗︽暟缁勶紝瀛楃鍖呭惈a-z銆�1-9锛屾瘮濡傦細a b c 4 b 2 a c 1 1 3锛屾眰鍙嚭鐜颁竴娆$殑绗竴娆″嚭鐜扮殑瀛楃

str_list = ['a', 'b', 'c', 4, 'b', 2, 'a', 'c', 1, 1, 3]

def find_only_one(alist):

聽 聽 for string in alist:

聽 聽 聽 聽 count = alist.count(string)

聽 聽 聽 聽 if count == 1:

聽 聽 聽 聽 聽 聽 return string

聽 聽 return None

13銆� 鏈変竴涓猦tml鏂囨湰瀛楃涓诧紝璁╂垜鍙栧嚭sflkj杩欎釜a鏍囩閲岄潰鐨刪ref鐨勯摼鎺ュ湴鍧�锛�

from bs4 import BeautifulSoup

text = "sflkj"

the_html = BeautifulSoup(text,features='lxml')

print(the_html.find('a').attrs['href'])

14銆� 涓嬮潰鏄竴涓崟绾跨▼鐨勪唬鐮侊紝璇锋敼鍐欐垚澶氱嚎绋嬬殑锛�

start = "http://google.com"

queue = [start]

visited = {start}

while queue:

聽 聽 url = queue.pop(0)

聽 聽 print(url)聽

聽 聽 for next_url in extract_url(url):

聽 聽 聽 聽 if next_url not in visited:

聽 聽 聽 聽 聽 聽 queue.append(next_url)

聽 聽 聽 聽 visited.add(next_url)


from concurrent.futures import ThreadPoolExecutor

start = "http://google.com"

queue = [start]

visited = {start}

pool = ThreadPoolExecutor(10)

def func(url):

聽 聽 for next_url in extract_url(url):

聽 聽 聽 聽 if next_url not in visited:

聽 聽 聽 聽 聽 聽 queue.append(next_url)

聽 聽 聽 聽 visited.add(next_url)

while queue:

聽 聽 url = queue.pop(0)

聽 聽 pool.submit(func,url)

pool.shutdown(wait=True)


闈㈣瘯棰橈紙鍏級[17]

1銆� 鑾峰彇list鐨勫厓绱犱釜鏁帮紝鍚憀ist鐨勬湯灏捐拷鍔犲厓绱犳墍鐢ㄧ殑鏂规硶鍒嗗埆鏄�( ),( )

count锛宎ppend

2銆� 鍒ゆ柇dict鏈夋病鏈夋煇涓猭ey鐢ㄧ殑鏂规硶鏄�( )

in

3銆� L = range(100) 锛屽彇绗竴鍒扮涓変釜鍏冪礌鐢紙锛夛紝鍙栧�掓暟绗簩涓厓绱狅紙锛夛紝鍙栧悗鍗佷釜鍏冪礌锛堬級

L[:3]

L[-2]

L[-10:]

4銆� 鎶奓澶嶅埗缁橪1鐢紙锛� 娉ㄦ剰锛氶潪寮曠敤浼犻��

L1 = L.copy()

5銆乨 = {'a':1,'b':2,'c':3}璇锋墦鍗板嚭key锛寁alue瀵�

for k,v in d.items():

聽 聽 print(k,v)

6銆� 濡備綍鍒ゆ柇涓�涓彉閲忔槸涓嶆槸瀛楃涓�

isinstance(a,str)

7銆乴ist鍜宼uple鏈変粈涔堜笉鍚岋紵

8銆亁range鍜宺ange鏈変粈涔堜笉鍚岋紵

9銆佲��1,2,3鈥欏浣曞彉鎴怺鈥�1鈥�,鈥�2鈥�,鈥�3鈥橾 ?聽 [鈥�1鈥�,鈥�2鈥�,鈥�3鈥橾濡備綍鍙樻垚[1,2,3]

s1 = "1,2,3"

s2 = list(s1.split(','))

s3 = list(map(int,s2))

10銆佽鍥炵瓟浠ヤ笅闂锛�

def add_end(L= []):

聽 聽 L.append('END')

聽 聽 return L

print(add_end()) #杈撳嚭浠�涔堬紵

print(add_end()) #鍐嶆璋冪敤杈撳嚭浠�涔堬紵涓轰粈涔堬紵

["END"] ["END",""END] 鍑芥暟鐨勯粯璁ゅ弬鏁板湪缂栬瘧鏃讹紝鍒嗛厤鍐呭瓨绌洪棿銆傛病鏈夌粰浜堝弬鏁伴兘鏄娇鐢ㄩ粯璁ょ殑鍙傛暟锛屾槸鍚屼竴娈靛唴瀛樺湴鍧�鐨勫悓涓�瀵硅薄銆�

11銆乕36,5,12,9,21]鎬庝箞鎺掑簭锛�

a = [36,5,12,9,21]

a.sort()

12銆佽鍥炵瓟浠ヤ笅闂锛�

def func(a,b,c=0,*args,**kwargs):

聽 聽 pass

*args,**kwargs鐨勪綔鐢ㄦ槸浠�涔堬紵

褰撳嚱鏁扮殑鍙傛暟涓嶇‘瀹氭椂锛屽彲浠ヤ娇鐢�*args 鍜�**kwargs鏉� 浼犻�掑彲鍙樺弬鏁般��

*args鍌ㄥ瓨鍙彉鐨勪綅缃弬鏁帮紝瀹冧細鎺ユ敹浠绘剰澶氫釜鍙傛暟骞舵妸杩欎簺鍙傛暟浣滀负鍏冪粍浼犻�掔粰鍑芥暟銆�**kwargs瀛樺偍鍙彉鐨勫叧閿瓧鍙傛暟锛屽厑璁镐綘浣跨敤娌℃湁浜嬪厛瀹氫箟鐨勫弬鏁板悕锛屽皢鎺ユ敹鍒颁换鎰忓涓叧閿瓧鍙傛暟浣滀负瀛楀吀浼犻�掔粰鍑芥暟銆�

娉ㄦ剰鍑芥暟鐨勫弬鏁扮殑椤哄簭锛歛rgs蹇呴』鍦╧wargs鍓嶉潰锛岃皟鐢ㄥ嚱鏁颁紶閫掑弬鏁颁篃蹇呴』渚濈収姝ら『搴�

13銆乮s鍜�==鐨勫尯鍒槸锛�

Python涓殑瀵硅薄鍖呭惈涓夎绱狅細id銆乼ype銆乿alue

鍏朵腑id鐢ㄦ潵鍞竴鏍囪瘑涓�涓璞★紝type鏍囪瘑瀵硅薄鐨勭被鍨嬶紝value鏄璞$殑鍊�

is鍒ゆ柇鐨勬槸a瀵硅薄鏄惁灏辨槸b瀵硅薄锛屾槸閫氳繃id鏉ュ垽鏂殑

==鍒ゆ柇鐨勬槸a瀵硅薄鐨勫�兼槸鍚﹀拰b瀵硅薄鐨勫�肩浉绛夛紝鏄�氳繃value鏉ュ垽鏂殑

14銆佸浣曠敓鎴怺1,4,9,16,25,36,49,64,81,100]锛熷敖閲忕敤涓�琛屽疄鐜�

[x*x for x in range(1,11)]

15銆佺敓鎴愬櫒鏄粈涔堬紵鏈変粈涔堜綔鐢紵璇峰啓鍑轰竴涓敓鎴愬櫒

a = ( i for i in range(11))


16銆乵ap(str,[1,2,3,4,5,6,7,8,9])杈撳嚭浠�涔堬紵

# 鐢熸垚鐨勬槸map瀵硅薄

print(list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9])))

# ['1', '2', '3', '4', '5', '6', '7', '8', '9']

17銆佽鍐欏嚭log鐨勫疄鐜帮紙涓昏鍔熻兘鏄墦鍗板嚱鏁板悕锛�

@log

def now():

聽 聽 print('2013-12-25')

now()

# 杈撳嚭锛�

# call now()

# 2013-12-25


import functools

def log(func):

聽 聽 @functools.wraps(func)

聽 聽 def inner():

聽 聽 聽 聽 print('call '+ func.__name__ + '()')

聽 聽 聽 聽 func()

聽 聽 return inner


闈㈣瘯棰橈紙涓冿級[13]

涓�銆佸崟椤归�夋嫨

1聽 浠ヤ笅鍙欒堪姝g‘鐨勬槸锛�

A continue璇彞鐨勪綔鐢ㄦ槸缁撴潫鏁翠釜寰幆鐨勬墽琛�

B 鍙兘鍦ㄥ惊鐜綋鍐呭拰switch璇彞浣撲腑浣跨敤break璇彞

C 鍦ㄥ惊鐜綋鍐呬娇鐢╞reak璇彞鎴朿ontinue璇彞鐨� 浣滅敤鐩稿悓

D 浠庡灞傚惊鐜祵濂椾腑閫�鍑烘椂锛屽彧鑳戒娇鐢╣oto璇彞

# B

2 Python濡備綍瀹氫箟涓�涓嚱鏁�( 3)

class ( arg1 , arg 2, ... , argN)

function (arg1 ,arg2 ,鈥� , argN)

def (arg1 ,arg2 ,鈥� , argN)

def ( arg1 , arg 2, ... , argN)

3 涓嬮潰鍝釜鍑芥暟鑳藉鍦↙inux鐜涓嬪垱寤轰竴涓瓙杩涚▼锛�

os.popen

os.fork

os.system

os.link聽

# 2


4宸茬煡x=43锛宑h = 'A' ,y = 1锛屽垯琛ㄨ揪寮�(x > y and ch< 鈥楤鈥� and y)鐨勫�兼槸

0

1

鍑洪敊

True(鈥滅湡鈥�)

# 2

5 涓嬮潰鐨勮鍙ュ摢涓細鏃犻檺寰幆涓嬪幓

鍚姩range鍑芥暟鍘熷瀷涓簉ange([start],stop[,step])

1銆乫or a in range(10):

聽 聽 聽 聽 聽 聽 聽 聽 聽time.sleep(10)

銆�銆�2銆亀hile 1 < 10:

銆�銆�銆�銆�銆�銆�time.sleep(10)

銆�銆�3銆亀hile True:

銆�銆�銆�銆�銆�銆�break;

銆�銆�4銆乤 = [3,-1, 5 , 7]

銆�銆�銆�銆�for I in a[:]

銆�銆�if a > 10:

銆�銆�break;

# 2


6 涓嬪垪琛ㄨ揪寮忎腑杩斿洖涓篢rue鐨勬槸锛�

聽聽聽聽聽聽聽聽 A. 3 > 2 >2

聽聽聽聽聽聽聽聽 B. 鈥榓bc鈥� > 鈥榵yz鈥�

聽聽聽聽聽聽聽聽 C. 0x56 < 56

聽聽聽聽聽聽聽聽 D. (3,2) < (鈥榓鈥�,鈥檅鈥�)

# D

7 Python涓嶆敮鎸佺殑鏁版嵁绫诲瀷鏈�

char

int

float

list

# 1

浜屻�佷笉瀹氶�夐」

8聽聽 涓嬮潰鐨勫嚱鏁帮紝閭d簺浼氳緭鍑�1,2,3涓変釜鏁板瓧

聽for I in range(3)

銆�銆�銆�銆�print i

銆�銆�2.聽aList = [0,1,2]

聽 聽 聽 聽 聽 for I in aList:

聽 聽 聽 聽 聽 聽 聽 聽 print i+1

銆�銆�3.聽I = 1

銆�銆�while I < 3:

聽聽聽聽聽聽聽聽 print i

聽聽聽聽聽聽聽聽 I = I +1

銆�銆�4.for I in range(3):

銆�銆�銆�銆�print I + 1

# 2,4


涓夈�佸~绌洪

9聽 浠ヤ笅鍑芥暟闇�瑕佽皟鐢ㄥ湪鍏朵腑寮曠敤涓�涓叏灞�鍙橀噺k锛岃濉啓璇彞锛�

def func():

聽聽聽聽聽聽聽聽 ___________

聽聽聽聽聽聽聽聽 k = k +1

global k


10璇锋妸浠ヤ笅鍑芥暟杞寲涓篜ython lambda鍖垮悕鍑芥暟

聽聽聽聽聽聽聽聽 def add(x,y):

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return x+y

lambda x,y:x+y


11璇风畝鍗曡В閲奝ython涓璼tatic method锛堥潤鎬佹柟娉曪級鍜宑lass method锛堢被鏂规硶锛�

璇峰皢浠ヤ笅浠g爜濉啓瀹屾暣


class A(object):

聽 聽 def foo(self,x):

聽 聽 聽 聽 print("executing foo(%s,%s)" % (self,x))

聽 聽 @classmethod

聽 聽 def class_foo(cls,x):

聽 聽 聽 聽 print("executing class_foo(%s,%s)" % (cls,x))

聽 聽 @staticmethod

聽 聽 def static_foo(x):

聽 聽 聽 聽 print("executing static_foo(%s)" % (x))

a = A()

# 璋冪敤 foo鍑芥暟锛屽弬鏁颁紶鍏�1

a.foo(1)

#璋冪敤class_foo鍑芥暟锛屽弬鏁颁紶鍏�1

a.class_foo(1)

A.class_foo(1)

# 璋冪敤闈欐�佹柟娉曪紝浼犲叆鍙傛暟1

a.static_foo(1)

A.static_foo(1)


鍥涖�佺▼搴忛

12銆� 璇蜂功鍐欎竴涓嚱鏁帮紝鐢ㄤ簬鏇挎崲鏌愪釜瀛楃涓茬殑涓�涓垨鏌愬嚑涓瓧涓�

鍑芥暟鍘熷瀷strreplace(str,oldString,newString);

渚嬪锛�

pstr = 鈥淗ello World!鈥�

afterReplaceStr = strreplace(pstr,鈥漌orld鈥�,鈥漈om鈥�)

閭d箞afterReplaceStr鐨勫�间负鈥滺ello Tom鈥�

def strreplace(str,oldString,newString):

聽 聽 return str.replace(oldString,newString)

13銆� 骞宠 鐐归棶棰橈細

鍋囧涓�涓暟缁勪腑鐨勫厓绱狅紝鍏跺墠闈㈢殑閮ㄥ垎绛変簬鍚庨潰鐨勯儴鍒嗭紝閭d箞杩欎釜鐐圭殑浣嶅簭灏辨槸骞宠 鐐�

姣斿涓�涓垪琛╪umbers = [1,3,20,2,18,6]锛�2鍓嶉潰鐨勬�诲拰涓�24锛�2鍚庨潰鐨勬�诲拰涔熸槸24锛岄偅涔�2灏辨槸骞宠 鐐�

瑕佹眰缂栧啓绋嬪簭锛屽鎵惧苟杩斿洖浠绘剰涓�涓垪琛ㄧ殑骞宠 鐐�


def balance_num(numbers):

聽 聽 for i in range(0,len(numbers)):

聽 聽 聽 聽 if sum(numbers[:i]) == sum(numbers[i+1:]):

聽 聽 聽 聽 聽 聽 return numbers[i]

聽 聽 return None


闈㈣瘯棰橈紙鍏級銆�11銆�

1 Python濡備綍瀹炵幇鍗曚緥妯″紡锛�

2浠�涔堟槸lambda鍑芥暟锛�

3 璇峰啓鍑轰竴娈礟ython浠g爜瀹炵幇鍒犻櫎list閲岄潰鐨勯噸澶嶅厓绱狅紵

4 濡備綍鐢≒ython鍒犻櫎涓�涓枃浠讹紵

5 Python閲岄潰濡備綍鐢熸垚闅忔満鏁帮紵

6 璇风敤Python鍐欏嚭涓�涓幏鍙栫敤鎴疯緭鍏ユ暟瀛楋紝骞舵牴鎹暟瀛楀ぇ灏忚緭鍑轰笉鍚屼俊鎭殑鑴氭湰锛�

7 range鍜寈range鍖哄埆

8 瑙i噴鐢熸垚鍣紙generator锛変笌鍑芥暟鐨勪笉鍚岋紝骞跺疄鐜板拰浣跨敤绠�鍗昰enerator

9杈撳叆涓�涓瓧绗︿覆锛岃繑鍥炲�掑簭鎺掑垪鐨勭粨鏋滐紱濡傗�檃bcdef鈥欙紝杩斿洖鈥檉edcba鈥櫬� str1 = 'abcdefg'

str2 = str1[::-1]

str3 = list(str1)

str3.reverse()

str4 = ''.join(str3)

10 璇蜂娇鐢ㄨ嚜宸辩殑绠楁硶锛屾寜鍗囧簭鍚堝苟濡備笅涓や釜list锛屽苟鍘婚櫎閲嶅鐨勫厓绱狅細

銆�銆�list1 = [2,3,8,4,9,5,6]

銆�銆�list2 = [5,6,10,17,11,2]

11.Django濡備綍鍦∕odel淇濆瓨鍓嶅仛涓�瀹氱殑鍥哄畾鎿嶄綔锛屾瘮濡傚啓涓�鍙ユ棩蹇�

闈㈣瘯涔濄��4銆�

1銆佹祬璋堜綘瀵筽ython缂栫爜瑙勮寖鐨勮璇嗭紝骞跺啓鍑轰綘鐭ラ亾鐨勭紪鐮佽鑼�

璇风偣鍑�聽

Python缂栫爜瑙勮寖鍜孭ython椋庢牸瑙勮寖

涓�銆佸師鍥�

1銆侀暱鏈熺殑宸ヤ綔涓紝鍙戠幇澶у鏁扮▼搴忓憳鐨勪唬鐮佸彲璇绘�у樊

2銆佷笉鍚岀殑绋嬪簭鍛樹箣闂寸殑鍗忎綔寰堥噸瑕侊紝浠g爜鍙鎬у繀椤诲緢濂�

3銆佺増鏈崌绾ф椂锛岃鍩轰簬婧愮爜鍗囩骇

4銆佷笉鍙嬪ソ鐨勪唬鐮佷細褰卞搷python鐨勬墽琛屾晥鐜�

浜屻�佸熀浜庣殑瑙勮寖

銆�銆�1銆丳EP 8:聽Style Guide for Python Code---------Python鐨勭紪鐮侀鏍煎缓璁�

銆�銆�2銆丟oogle Python Style Guide--------------Goole Python缂栫爜椋庢牸寤鸿

涓夈�丳ython璇█瑙勮寖

1. imports 浠呬粎鐢ㄥ仛鍖呭拰妯″潡鐨勫鍏ワ紝鍖呯殑瀵煎叆姣忎釜灏介噺鐙崰涓�琛�

2. packages 瀵煎叆妯″潡灏介噺浣跨敤妯″潡鐨勫叏璺緞

銆�銆�3. Exceptions蹇呴』灏忓績浣跨敤

銆�銆�4. Global variables閬垮厤浣跨敤鍏ㄥ眬鍙橀噺

銆�銆�5. Generator

6. lambda 鍑芥暟浠呬粎閫傜敤浜庝竴琛屼唬鐮佽兘瀹炵幇鐨勭畝鍗曞嚱鏁�

銆�銆�7. True or False灏介噺浣跨敤[],'',{},0,None鏉ラ殣寮忚〃绀篎alse


鍥涖�丳ython椋庢牸瑙勮寖

1銆佷唬鐮佺紪鎺�

1 缂╄繘锛�4涓┖鏍煎疄鐜扮缉杩涳紝灏介噺涓嶄娇鐢═ab锛岀姝㈡贩鐢═ab鍜岀┖鏍�

2 琛岋細姣忚鏈�澶ч暱搴︿笉瓒呰繃79锛屾崲琛屽彲浠ヤ娇鐢ㄥ弽鏂滄潬(\)銆傛渶濂戒娇鐢ㄥ渾鎷彿灏嗘崲琛屽唴瀹规嫭璧锋潵锛屼笉寤鸿浣跨敤鈥�;鈥�

3 绌鸿锛氱被鍜宼op-level鍑芥暟瀹氫箟涔嬮棿绌轰袱琛岋紱绫讳腑鐨勬柟娉曞畾涔変箣闂寸┖涓�琛岋紱鍑芥暟鍐呴�昏緫鏃犲叧娈佃惤涔嬮棿绌轰竴琛岋紱鍏朵粬鍦版柟灏介噺涓嶈鍐嶇┖琛屻��

4 绌烘牸锛氭嫭鍙峰唴鐨勭涓�涓綅缃紝涓嶈绌烘牸銆傜揣闈犲彸鎷彿鐨勪綅缃篃涓嶈绌烘牸銆傚啋鍙�(:)銆侀�楀彿(,)銆佸垎鍙�(;)涔嬪墠涓嶈鍔犵┖鏍笺�傚垏鐗囨湪鏈夊弬鏁帮紝涓嶈鍔犵┖鏍肩瓑

5 鎷彿锛氬浜庡崟鍏冪礌tuple涓�瀹氳鍔�,鍜屾嫭鍙�

2銆佸懡鍚嶈鑼�

銆�銆�銆�銆�module_name

銆�銆�銆�銆�package_name

銆�銆�銆�銆�ClassName

銆�銆�銆�銆�method_name

銆�銆�銆�銆�ExceptionName

銆�銆�銆�銆�function_name

銆�銆�銆�銆�GLOBAL_CONSTANT_NAME

銆�銆�銆�銆�global_var_name

銆�銆�銆�銆�instance_var_name

銆�銆�銆�銆�function_parameter_name

銆�銆�銆�銆�local_var_name

3銆佹敞閲婅鑼�

1.鍧楁敞閲婏紝鍦ㄤ竴娈典唬鐮佸墠澧炲姞鐨勬敞閲娿�傚湪鈥�#鈥欏悗鍔犱竴绌烘牸銆傛钀戒箣闂翠互鍙湁鈥�#鈥欑殑琛岄棿闅斻�傛瘮濡傦細

銆�銆�銆�銆�# Description : Module config.銆�銆�銆�銆�#銆�銆�銆�銆�# Input : None銆�銆�銆�銆�#銆�銆�銆�銆�# Output : None

2.聽琛屾敞閲婏紝鍦ㄤ竴鍙ヤ唬鐮佸悗鍔犳敞閲娿�傛瘮濡傦細x = x + 1 # Increment x聽浣嗘槸杩欑鏂瑰紡灏介噺灏戜娇鐢ㄣ��

3.聽閬垮厤鏃犺皳鐨勬敞閲娿��

4銆佺紪绋嬪缓璁�

1. 瀛楃涓叉嫾鎺ワ紝灏介噺浣跨敤join銆備娇鐢╯tr鐨勬柟娉曡�屼笉鏄唴缃柟娉曘�備娇鐢╯tartswith鎴杄ndswith鎷夋鏌ュ墠缂�鍜屽悗缂�

2. 鍗曚緥瀵硅薄锛屽敖閲忎娇鐢╥s 銆乮s not锛屼笉瑕佷娇鐢�==

銆�銆�銆�銆�3.浣跨敤is not鑰屼笉鏄痭ot is

4. 浣跨敤def鏉ュ畾涔夊嚱鏁帮紝鑰屼笉鏄皢鍖垮悕鍑芥暟璧嬬粰鏌愪釜鍙橀噺

5. 灏介噺浣夸唬鐮佹暣榻愶紝绠�娲�

銆�銆�銆�銆�6.浣跨敤isinstance()鏉ュ垽鏂璱nstance鐨勭被鍨�

2銆佹祬璋堜綘瀵筽ython瀹夊叏缂栫爜鐨勮璇嗭紝骞朵妇渚嬭鏄�



1銆佸浘涔︾鐞嗙郴缁熸槸瀵瑰浘涔﹁繘琛屽綍鍏ャ�佸�熼槄鐨勭郴缁燂紝璇风敤django妗嗘灦瀹炵幇鏂板銆佹煡鐪嬪浘涔︾殑鍔熻兘锛屽垎鍒啓鍑簃odel.py銆乽rls.py銆乿iews.py鐨勯�昏緫



2銆佽鏈変竴涓儗鍖呰兘鎵垮彈閲嶉噺s(s>0)锛屽張鏈塶锛坣>=1锛変欢鐗╁搧锛屽叾璐ㄩ噺鍒楄〃涓� w = [w1,w2....,wn]锛岃浠庣墿鍝佷腑鎸戦�夎嫢骞蹭欢鏀惧叆鑳屽寘锛屼娇寰楁斁鍏ョ殑鍥句功璐ㄩ噺涔嬪拰姝eソ鏄痵銆傝璁捐閫掑綊鍑芥暟f(w,s,n):

褰撴湁瑙f椂锛岃繑鍥濼rue锛屽苟鏄剧ず鎵�閫夌墿鍝佹槸閭d簺锛熸棤瑙f椂杩斿洖False銆傛彁绀猴細[閫掑綊涔嬪鍦ㄤ簬f(w,s-wn,n-1) or f(w,s,n-1)]

python闈㈣瘯棰橈紙鍗侊級銆�10銆�

Python涓熀鏈暟鎹粨鏋勭殑鎿嶄綔

聽鍏冪粍鍒楄〃瀛楀吀闆嗗悎

瀹氫箟聽聽聽聽

鏂板聽聽聽聽

鏇存敼聽聽聽聽

鍒犻櫎聽聽聽聽

2.璇峰敖鍙兘鍒椾妇python鍒楄〃鐨勬垚鍛樻柟娉曪紝骞剁粰鍑轰竴涓嬪垪琛ㄦ搷浣滅殑绛旀锛�

銆�銆�锛�1锛塧=[1, 2, 3, 4, 5], a[::2]=锛�, a[-2:] =锛�

锛�1锛�1 3 5

锛�2锛�4 5


锛�2锛� 涓�琛屼唬鐮佸疄鐜板鍒楄〃a涓殑鍋舵暟浣嶇疆鐨勫厓绱犺繘琛屽姞3鍚庢眰鍜岋紵


sums = sum(map(lambda x: x + 3, a[1::2]))



銆�銆�3.List = [-2, 1, 3, -6]锛屽浣曞疄鐜颁互缁濆鍊煎ぇ灏忎粠灏忓埌澶у皢List涓唴瀹规帓搴忋��

sorted(list1,key = abs)

(1) 鍒楄〃鐨剆ort鏂规硶鍜� sorted鐨勫尯鍒槸浠�涔堬紵


sort鏄痩ist鐨勬柟娉曪紝鏀瑰彉list瀵硅薄鐨勯『搴忥紝杩斿洖鍊间负None

sorted鏄疨ython鐨勫唴缃柟娉曪紝閫傜敤iterable瀵硅薄锛岃繑鍥炲�兼槸鏂板垪琛紝涓嶅奖鍝嶅師鏉ョ殑iterable鐨勯『搴�


4.鏈変竴绡囪嫳鏂囨枃绔犱繚瀛樺湪 a.txt 涓紝璇风敤python瀹炵幇缁熻杩欑瘒鏂囩珷鍐呮瘡涓崟璇嶇殑鍑虹幇棰戠巼锛屽苟杩斿洖鍑虹幇棰戠巼鏈�楂樼殑鍓�10涓崟璇嶅強鍏跺嚭鐜版鏁帮紙鍙�冭檻绌烘牸锛屾爣鐐圭鍙峰彲蹇界暐锛�

from collections import Counter

聽 聽 c = Counter()

with open('a.txt','r',encoding='utf-8') as f:

聽 聽 for line in f.readlines():

聽 聽 聽 聽 words = line.split()

聽 聽 聽 聽 c1 = Counter(words)

聽 聽 聽 聽 c.update(c1)

锛�1锛夎拷鍔犻渶姹傦細寮曞彿鍐呭厓绱犻渶瑕佺畻浣滀竴涓崟璇嶏紝濡備綍瀹炵幇锛�

浠�"鍒嗗壊锛岃浆鎹㈡垚鍒楄〃锛屽彇鍏跺鏁板垎鍓诧紝鍏跺伓鏁颁笉鍋氬鐞�


5.Python鍑芥暟涓粡甯告湁 *args锛�**kwargs 杩欎袱涓弬鏁帮紝瀹冧滑鏄粈涔堟剰鎬濓紝涓轰粈涔堣浣跨敤瀹冧滑锛�


澶畝鍗曪紝灏变笉鍐欏暒锛屽搱鍝堝搱鍝�


6.Python涓殑鍙橀噺浣滅敤鍩燂紙鍙橀噺鏌ユ壘椤哄簭锛夈��


LEGB

local灞�閮ㄥ彉閲�--->enclosed闂寘浣滅敤鍩�----> Global鍏ㄥ眬---->built-in鍙橀噺


7.涓嬮潰杩欐浠g爜鐨勮緭鍑虹粨鏋滃皢鏄粈涔堬紵璇疯В閲娿��


# 1 1 1 缁ф壙鑷埗绫荤殑绫诲睘鎬锛屾墍浠ラ兘涓�鏍凤紝鎸囧悜鍚屼竴鍧楀唴瀛樺湴鍧�

# 1 2 1鏇存敼Child1锛孋hild1鐨剎鎸囧悜浜嗘柊鐨勫唴瀛樺湴鍧�

# 3 2 3鏇存敼Parent锛孭arent鐨剎鎸囧悜浜嗘柊鐨勫唴瀛樺湴鍧�


8.鎻忚堪Python GIL鐨勬蹇碉紝 浠ュ強瀹冨python澶氱嚎绋嬬殑褰卞搷锛熺紪鍐欎竴涓绾跨▼鎶撳彇缃戦〉鐨勭▼搴忥紝骞堕槓鏄庡绾跨▼鎶撳彇绋嬪簭鏄惁鍙瘮鍗曠嚎绋嬫�ц兘鏈夋彁鍗囷紝骞惰В閲婂師鍥犮��

Guido鐨勫0鏄庯細http://www.artima.com/forums/flat.jsp?forum=106&thread=214235

he language doesn't require the GIL -- it's only the CPython virtual machine that has historically been unable to shed it.

Python璇█鍜孏IL娌℃湁鍗婃瘺閽卞叧绯汇�備粎浠呮槸鐢变簬鍘嗗彶鍘熷洜鍦–python铏氭嫙鏈�(瑙i噴鍣�)锛岄毦浠ョЩ闄IL銆�

GIL锛氬叏灞�瑙i噴鍣ㄩ攣銆傛瘡涓嚎绋嬪湪鎵ц鐨勮繃绋嬮兘闇�瑕佸厛鑾峰彇GIL锛屼繚璇佸悓涓�鏃跺埢鍙湁涓�涓嚎绋嬪彲浠ユ墽琛屽瓧鑺傜爜銆�

绾跨▼閲婃斁GIL閿佺殑鎯呭喌锛�

鍦↖O鎿嶄綔绛夊彲鑳戒細寮曡捣闃诲鐨剆ystem call涔嬪墠,鍙互鏆傛椂閲婃斁GIL,浣嗗湪鎵ц瀹屾瘯鍚�,蹇呴』閲嶆柊鑾峰彇GIL

Python 3.x浣跨敤璁℃椂鍣紙鎵ц鏃堕棿杈惧埌闃堝�煎悗锛屽綋鍓嶇嚎绋嬮噴鏀綠IL锛夋垨Python 2.x锛宼ickets璁℃暟杈惧埌100

Python浣跨敤澶氳繘绋嬫槸鍙互鍒╃敤澶氭牳鐨凜PU璧勬簮鐨勩��

澶氱嚎绋嬬埇鍙栨瘮鍗曠嚎绋嬫�ц兘鏈夋彁鍗囷紝鍥犱负閬囧埌IO闃诲浼氳嚜鍔ㄩ噴鏀綠IL閿�

9.Python涓浣曞姩鎬佽幏鍙栧拰璁剧疆瀵硅薄鐨勫睘鎬с��

if hasattr(Parent,'x'):

聽 聽 print(getattr(Parent,'x'))

聽 聽 setattr(Parent,'x',3)

聽 聽 print(getattr(Parent,'x'))

10.(鍓嶇鍩虹)

(1)鐢–SS濡備綍闅愯棌涓�涓厓绱�

dispaly:none

(2)涓�琛孋SS瀹炵幇padding涓婁笅宸﹀彸鍒嗗埆涓�1px锛�2px锛�3px锛�4px

padding:1 4 2 3

(3)聽聽聽 JavaScript(鎴杍Query)濡備綍閫夋嫨涓�涓猧d涓簃ain鐨勫鍣�

$('#main')

(4)聽聽聽 JavaScript(鎴杍Query)濡備綍閫夋嫨涓�涓猚lass涓簃enu鐨勫鍣�

$('.menu')聽

python闈㈣瘯锛堝崄鍥涳級銆�4銆懧�

map(lambda x:x*x,[y for y in range(3)])

[0, 1, 4]


2銆佷笅闈唬鐮佷細杈撳嚭浠�涔�?

def f(x, l = [] )

銆�銆�for i in range(x):

銆�銆�銆�銆�l.append(i*i)

銆�銆�print l

f(2)

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

f(3)

1銆乕0, 1]

2銆乕3, 2, 1, 0, 1, 4]

3銆乕0, 1, 0, 1, 4]

聽涔嬩竴锛岀ず渚嬪涓嬶細

灞傜骇閬嶅巻锛�0 1 2 3 4 5 6 7 8 9

鍏堝簭閬嶅巻锛�0 1 3 7 8 4 9 2 5 6

涓簭閬嶅巻锛�7 3 8 聽1 9 4 0 5 2 6

鍚庡簭鎺掑垪锛�7 8 3 9 4 1 5 6 2 0


class Node(object):

聽 聽 def __init__(self, data, left=None, right=None):

聽 聽 聽 聽 self.data = data

聽 聽 聽 聽 self.left = left

聽 聽 聽 聽 self.right = right

r涓篘ode绫诲瀷鐨勬牴鑺傜偣

瀹炵幇鍑芥暟traverse(r)杈撳嚭鍏堝簭閬嶅巻缁撴灉锛岃緭鍑洪儴鍒嗕娇鐢╬rint r.data鍗冲彲

node7 = Node(7)

node8 = Node(8)

node9 = Node(9)

node3 = Node(3,node7,node8)

node4 = Node(4,node9)

node5 = Node(5)

node6 = Node(6)

node1 = Node(1,node3,node4)

node2 = Node(2,node5,node6)

node0 = Node(0,node1,node2)

def traverse(r):

聽 聽 print(r.data)

聽 聽 if r.left:

聽 聽 聽 聽 traverse(r.left)

聽 聽 if r.right:

聽 聽 聽 聽 traverse(r.right)


4銆佹湁涓�涓�3G澶у皬鐨勬枃浠讹紝鏂囦欢姣忚涓�涓猻tring锛屽唴瀹逛负閰掑簵鐨刬d鍜屼竴涓浘鐗囩殑鍚嶅瓧锛屼娇鐢ㄢ�淺t鈥濆垎鍓�

绀轰緥锛歨t_1023134 + "\t" + hidfadsfadsfdfadsf2r234523,jpg

琛ㄧず鐨勬槸涓�涓厭搴楀寘鍚殑涓�寮犲浘鐗囷紝缁熻鍚湁鍥剧墖鏁伴噺涓篬20,鏃犵┓澶鐨勯厭搴梚d锛屽惈鏈夊浘鐗囨暟閲忎负[10,20]鐨勯厭搴梚d銆佸惈鏈夊浘鐗囨暟閲忎负[10,5]鐨勯厭搴梚d锛屽惈鏈夊浘鐗囨暟閲忎负[0锛�5]鐨勯厭搴梚d锛屽苟灏嗙粨鏋滆緭鍑哄埌鏂囦欢涓�

0-5 + 鈥淺t鈥� + id1 +聽聽鈥淺t鈥� + id2 + .....

5-10 + 鈥淺t鈥� + id1 +聽聽鈥淺t鈥� + id2 + .....

10-20 + 鈥淺t鈥� + id1 +聽聽鈥淺t鈥� + id2 + .....

20-鏃犵┓澶�+ 鈥淺t鈥� + id1 +聽聽鈥淺t鈥� + id2 + .....

from collections import Counter

count_dict = {}

cou = Counter()

with open('a.txt', encoding='utf-8') as f:

聽 聽 for line in f:

聽 聽 聽 聽 hotel, image = line.split()

聽 聽 聽 聽 hotel_id = hotel.split('_')[1]

聽 聽 聽 聽 cou.update({hotel_id,1})

聽 聽 聽 聽 if hotel_id in count_dict:

聽 聽 聽 聽 聽 聽 count_dict[hotel_id] += 1

聽 聽 聽 聽 else:

聽 聽 聽 聽 聽 聽 count_dict[hotel_id] = 1

del cou[1]

zero_five = ['0-5']

five_ten = ['5-10']

ten_twenty = ['10-20']

twenty_infinite = ['10-鍘荤┓澶�']

for hotel_id,count in count_dict.items():

聽 聽 if count < 5 :

聽 聽 聽 聽 zero_five.append(hotel_id)

聽 聽 elif count < 10 :

聽 聽 聽 聽 five_ten.append(hotel_id)

聽 聽 elif count < 20:

聽 聽 聽 聽 ten_twenty.append(hotel_id)

聽 聽 else:

聽 聽 聽 聽 twenty_infinite.append(hotel_id)

with open('b.txt','w',encoding='utf-8') as b:

聽 聽 b.write('\t'.join(zero_five))

聽 聽 b.write('\n')

聽 聽 b.write('\t'.join(five_ten))

聽 聽 b.write('\n')

聽 聽 b.write('\t'.join(ten_twenty))

聽 聽 b.write('\n')

聽 聽 b.write('\t'.join(twenty_infinite))

闈㈣瘯棰橈紙鍗佷簲锛�銆�13銆�

1銆佽绠�瑕佹弿杩癲jango-admin.py makemessages鐨勪綔鐢�

2銆丳ython濡備綍鎹曡幏寮傚父锛屽浣曞湪绋嬪簭鎵ц杩囩▼涓姏鍑哄紓甯告満鍒讹紵

Python涓娇鐢╰ry ... except SomeException as e: ...鏉ユ崟鑾峰紓甯�

raise SomeException("Some Message")鏉ユ姏鍑哄紓甯�

3銆佷粈涔堟槸lambda鍑芥暟锛熷畠鏈変粈涔堝ソ澶勶紵

4銆�*args鍜�**kwargs鐨勪娇鐢ㄦ柟娉曪紝璇风畝杩颁竴涓嬶紵

5銆佺畝瑕佷粙缁嶄笅python閲岄潰鐨勮楗板櫒锛�

6銆丠ttp璇锋眰涓璆ET鍜孭OST鏈変粈涔堝尯鍒紵Cookies鍜孲ession鏈変粈涔堝尯鍒紵

7銆乤 = 1锛宐 = 2锛屼笉鐢ㄤ腑闂村彉閲忎氦鎹鍜宐鐨勫�硷紵

b, a = a, b

Python鏀寔涓嶄娇鐢ㄤ腑闂村彉閲忎氦鎹袱涓彉閲忕殑鍊�


8銆佺紪鍐欎竴涓嚱鏁板垹闄ist閲岀殑閲嶅鍏冪礌锛岀敤涓ょ鏂瑰紡瀹炵幇锛�

9銆佽绠�瑕佹鎷琩jango娴嬭瘯宸ュ叿锛�

10銆佸浣曟墿灞昦uth_user鐨勫瓧娈�

11銆佽缁欏嚭Profile鐨勫疄鐜板疄渚�

12銆佸浣曟浛鎹uth_user

13銆乨jango鐨勭户鎵跨殑鏂瑰紡鏈夊摢浜涳紵


Python闈㈣瘯棰橈紙鍗佸叚锛�銆�8銆�164聽

1銆佸彇鍑轰袱涓崌搴忔暟缁勪腑鐨勬渶灏忕殑涓�鍗婄殑鍏冪礌缁勬垚涓�涓柊鐨勫崌搴忔暟缁勩��

map(lambda x, y: x if x < y else y, a, b)


2銆佺敤鑷冲皯2绉嶄笉鍚岀殑鏂瑰紡鍒犻櫎涓�涓猯ist閲岄潰鐨勯噸澶嶅厓绱�

a = [1, 2, 2, 4, 34, 3, 4, 56, 65, 456, 5, 6456, 456, 54, 45, 6, 464, 564]

# 鏂规硶涓�

new_list3 = list(set(a))

# 鏂规硶浜�

new_list = []

for num in a:

聽 聽 if num not in new_list:

聽 聽 聽 聽 new_list.append(num)

# 鏂规硶涓�

dict1 = dict.fromkeys(a)

new_list2 = dict1.keys()


3銆佸埄鐢ㄧ嚎绋嬫睜锛�10涓嚎绋嬶級瀹炵幇涓�涓苟琛屾墦鍗�10琛屼俊鎭殑绋嬪簭锛屾墦鍗癟his is thread xx.(娉細xx涓虹嚎绋媔d)


4銆佸叧绯诲瀷鏁版嵁搴撶殑浜嬪姟闅旂绾у埆鏈夊摢浜涳紝鍒嗗埆鏈変粈涔堝奖鍝嶏紵

1銆佹湭鎻愪氦璇�(Read Uncommitted)聽

聽 聽 鐩磋瘧灏辨槸"璇绘湭鎻愪氦",鎰忔�濆氨鏄嵆浣夸竴涓洿鏂拌鍙ユ病鏈夋彁浜�,浣嗘槸鍒殑浜嬪姟鍙互璇诲埌杩欎釜鏀瑰彉.杩欐槸寰堜笉瀹夊叏鐨勩�傚厑璁镐换鍔¤鍙栨暟鎹簱涓湭鎻愪氦鐨勬暟鎹洿鏀癸紝涔熺О涓鸿剰璇汇�偮�

2銆佹彁浜よ(Read Committed)聽

聽聽 鐩磋瘧灏辨槸"璇绘彁浜�",鍙槻姝㈣剰璇伙紝鎰忔�濆氨鏄鍙ユ彁浜や互鍚庡嵆鎵ц浜咰OMMIT浠ュ悗鍒殑浜嬪姟灏辫兘璇诲埌杩欎釜鏀瑰彉. 鍙兘璇诲彇鍒板凡缁忔彁浜ょ殑鏁版嵁銆侽racle绛夊鏁版暟鎹簱榛樿閮芥槸璇ョ骇鍒�

3銆佸彲閲嶅璇�(Repeatable Read)锛毬�

聽聽 鐩磋瘧灏辨槸"鍙互閲嶅璇�",杩欐槸璇村湪鍚屼竴涓簨鍔¢噷闈㈠厛鍚庢墽琛屽悓涓�涓煡璇㈣鍙ョ殑鏃跺��,寰楀埌鐨勭粨鏋滄槸涓�鏍风殑.鍦ㄥ悓涓�涓簨鍔″唴鐨勬煡璇㈤兘鏄簨鍔″紑濮嬫椂鍒讳竴鑷寸殑锛孖nnoDB榛樿绾у埆銆傚湪SQL鏍囧噯涓紝璇ラ殧绂荤骇鍒秷闄や簡涓嶅彲閲嶅璇伙紝浣嗘槸杩樺瓨鍦ㄥ够璞¤聽

4銆佷覆琛岃(Serializable)聽

聽聽 鐩磋瘧灏辨槸"搴忓垪鍖�",鎰忔�濇槸璇磋繖涓簨鍔℃墽琛岀殑鏃跺�欎笉鍏佽鍒殑浜嬪姟骞跺彂鎵ц. 瀹屽叏涓茶鍖栫殑璇伙紝姣忔璇婚兘闇�瑕佽幏寰楄〃绾у叡浜攣锛岃鍐欑浉浜掗兘浼氶樆濉�


鍥涳紝闅旂绾у埆瀵逛簨鍔″苟鍙戠殑鎺у埗聽

涓嬭〃鏄悇闅旂绾у埆瀵瑰悇绉嶅紓甯哥殑鎺у埗鑳藉姏:

涓㈠け鏇存柊聽 聽 鑴忚聽 聽 闈為噸澶嶈聽 聽 浜岀被涓㈠け鏇存柊(瑕嗙洊鏇存柊)聽 聽 骞诲儚璇�

鏈彁浜よ聽 聽 Y聽 聽 Y聽 聽 Y聽 聽 Y聽 聽 Y

鎻愪氦璇宦� 聽 N聽 聽 N聽 聽 Y聽 聽 Y聽 聽 Y

鍙噸澶嶈聽 聽 N聽 聽 N聽 聽 N聽 聽 N聽 聽 Y

涓茶璇宦� 聽 N聽 聽 N聽 聽 N聽 聽 N聽 聽 N


5銆佽鐢╬ython缂栧啓鍑芥暟find_string锛屼粠鏂囨湰涓悳绱㈠苟鎵撳嵃鍐呭锛岃姹傛敮鎸侀�氶厤绗︽槦鍙峰拰闂彿銆�

渚嬪瓙锛�

>>>find_string('hello\nworld\n','wor')

['wor']

>>>find_string('hello\nworld\n','l*d')

['ld']

>>>find_string('hello\nworld\n','o?')

['or']


6銆佽涓篺ind_string缂栧啓鍗曞厓娴嬭瘯

7銆佸凡鏈変竴涓瓧鍏稿垪琛紝濡備笅锛�

li = [

聽 聽 {'id': '1.1', 'content': "14"},

聽 聽 {'id': '1.2', 'content': "15"},

聽 聽 {'id': '1.3', 'content': "16"},

聽 聽 {'id': '1.4', 'content': "17"},

聽 聽 {'id': '1.5', 'content': "18"},

]

璇峰啓涓�涓被锛�

abc = x()

>>abc['1.1']

14

class x(UserDict):

聽 聽 def __init__(self):

聽 聽 聽 聽 super().__init__()

聽 聽 聽 聽 for dic in li:

聽 聽 聽 聽 聽 聽 # print(dic)

聽 聽 聽 聽 聽 聽 self.data.update({dic['id']:dic['content']})

abc = x()

print(abc['1.1'])

8銆佸浣曞揩閫熷瀛︾敓鐨勬垚缁╄繘琛岃瘎绾�:(婧愯嚜Fluent Python)

60鍒嗕互涓嬭瘎涓篎

銆�銆�60-70璇勪负D

銆�銆�...

90鍒嗕互涓婁负A

import bisect

import sys

def grade(score, breakpoint=[60, 70, 80, 90], grades = 'FDCBA'):

聽 聽 i = bisect.bisect(breakpoint, score)

聽 聽 return grades[i]

if __name__ == '__main__':

聽 聽 level = grade(64)

聽 聽 print(level)


闈㈣瘯闂鎬荤粨銆�25銆�

1銆乄ebSocket鐩稿叧闂

聽璇风畝鍗曡鏄庝竴涓媁ebSocket鐨勬彙鎵嬭繃绋�

1銆乧lient鍙戦�佽姹傦紝璇锋眰澶存惡甯ec-WebSocket-Key

2銆乻erver鏀跺埌璇锋眰锛岃В鏋愯姹傚ご閮ㄤ俊鎭紝鎷垮埌Sec-WebSocket-Key

3銆佹湇鍔″櫒绔皢magic_string(258EAFA5-E914-47DA-95CA-C5AB0DC85B11)鍜孲ec-WebSocket-Key杩涜sha-1鍔犲瘑锛屽皢hash缁撴灉杩涜base64缂栫爜

4銆佸皢缂栫爜缁撴灉鏀惧湪鍝嶅簲澶撮儴Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=锛岃繑鍥炵粰瀹㈡埛绔紝杩炴帴瀹屾垚

聽Tornado濡備綍澶勭悊WebSocket

1銆乀ornado鐨凜ontroller鍖归厤鍒板搷搴旂殑璇锋眰url锛屽皢鍏惰浆鍙戠粰澶勭悊鐨凥andler锛屾瘮濡侰hatHandler

2銆丆hatHandler闇�瑕佺户鎵縯ornado.websocket.WebSocketHandler

3銆丆hatHandler蹇呴』瀹炵幇

open鏂规硶:瀹㈡埛绔繛鎺ユ垚鍔燂紝闇�瑕佹墽琛岀殑鏂规硶

on_message鏂规硶:瀹㈡埛绔悜鏈嶅姟绔彂閫佹秷鎭紝鎵ц鐨勬柟娉�

on_close鏂规硶:瀹㈡埛绔叧闂椂锛屾墽琛岀殑鏂规硶

2銆佽兘绠�鍗曡璇寸嚎绋嬨�佽繘绋嬪拰鍗忕▼鍚楋紵

聽 聽1銆佺嚎绋嬶細鏈�灏忕殑璋冨害鍗曚綅

2銆佸崗绋嬶細鐢ㄦ埛鑷畾涔夌殑绾跨▼鍒囨崲

3銆佽繘绋嬶細

3銆丠ttp鍜孒ttps鐨勫尯鍒紵

4銆佸浜嶵CP/IP鍗忚鐔熸倝鍚楋紵涓夋鎻℃墜锛屽洓娆℃尌鎵嬶紵鑳界畝鍗曡涓�涓嬩竷灞傛ā鍨嬪悧锛�

TCP/IP鍗忚锛屾槸缃戠粶閫氫俊鍗忚锛屽張绉颁紶杈撴帶鍒跺崗璁�/鍥犵壒缃戜簰鑱斿崗璁�傚畾涔変簡鐢靛瓙璁惧濡備綍杩炲叆浜掕仈缃戯紝浠ュ強鏁版嵁濡備綍鍦ㄤ簰鑱旂綉鐩镐簰浼犺緭鐨勬爣鍑嗐��


鎵�璋撲笁娆℃彙鎵嬪氨鏄缓绔婽CP杩炴帴鏃讹紝闇�瑕佸鎴风鍜屾湇鍔$鎬诲叡鍙戦��3涓寘浠ョ‘璁よ繛鎺ョ殑寤虹珛銆�

锛�1锛夌涓�娆℃彙鎵嬶細Client灏嗘爣蹇椾綅SYN缃负1锛岄殢鏈轰骇鐢熶竴涓�約eq=J锛屽苟灏嗚鏁版嵁鍖呭彂閫佺粰Server锛孋lient杩涘叆SYN_SENT鐘舵�侊紝绛夊緟Server纭銆�

聽 锛�2锛夌浜屾鎻℃墜锛歋erver鏀跺埌鏁版嵁鍖呭悗鐢辨爣蹇椾綅SYN=1鐭ラ亾Client璇锋眰寤虹珛杩炴帴锛孲erver灏嗘爣蹇椾綅SYN鍜孉CK閮界疆涓�1锛宎ck=J+1锛岄殢鏈轰骇鐢熶竴涓�約eq=K锛屽苟灏嗚鏁版嵁鍖呭彂閫佺粰Client浠ョ‘璁よ繛鎺ヨ姹傦紝Server杩涘叆SYN_RCVD鐘舵�併��

聽 锛�3锛夌涓夋鎻℃墜锛欳lient鏀跺埌纭鍚庯紝妫�鏌ck鏄惁涓篔+1锛孉CK鏄惁涓�1锛屽鏋滄纭垯灏嗘爣蹇椾綅ACK缃负1锛宎ck=K+1锛屽苟灏嗚鏁版嵁鍖呭彂閫佺粰Server锛孲erver妫�鏌ck鏄惁涓篕+1锛孉CK鏄惁涓�1锛屽鏋滄纭垯杩炴帴寤虹珛鎴愬姛锛孋lient鍜孲erver杩涘叆ESTABLISHED鐘舵�侊紝瀹屾垚涓夋鎻℃墜锛岄殢鍚嶤lient涓嶴erver涔嬮棿鍙互寮�濮嬩紶杈撴暟鎹簡銆�

鎵�璋撳洓娆℃尌鎵嬶紝灏辨槸鎸囨柇寮�涓�涓猅CP杩炴帴鏃讹紝瀹㈡埛绔拰鏈嶅姟鍣ㄦ�诲叡鍙戦��4涓寘鏉ョ‘璁よ繛鎺ョ殑鏂紑銆偮�

鐢变簬TCP杩炴帴鏃跺叏鍙屽伐鐨勶紝鍥犳锛屾瘡涓柟鍚戦兘蹇呴』瑕佸崟鐙繘琛屽叧闂紝杩欎竴鍘熷垯鏄綋涓�鏂瑰畬鎴愭暟鎹彂閫佷换鍔″悗锛屽彂閫佷竴涓狥IN鏉ョ粓姝㈣繖涓�鏂瑰悜鐨勮繛鎺ワ紝鏀跺埌涓�涓狥IN鍙槸鎰忓懗鐫�杩欎竴鏂瑰悜涓婃病鏈夋暟鎹祦鍔ㄤ簡锛屽嵆涓嶄細鍐嶆敹鍒版暟鎹簡锛屼絾鏄湪杩欎釜TCP杩炴帴涓婁粛鐒惰兘澶熷彂閫佹暟鎹紝鐩村埌杩欎竴鏂瑰悜涔熷彂閫佷簡FIN銆傞鍏堣繘琛屽叧闂殑涓�鏂瑰皢鎵ц涓诲姩鍏抽棴锛岃�屽彟涓�鏂瑰垯鎵ц琚姩鍏抽棴锛屼笂鍥炬弿杩扮殑鍗虫槸濡傛銆�

聽锛�1锛夌涓�娆℃尌鎵嬶細Client鍙戦�佷竴涓狥IN锛岀敤鏉ュ叧闂瑿lient鍒癝erver鐨勬暟鎹紶閫侊紝Client杩涘叆FIN_WAIT_1鐘舵�併��

聽 锛�2锛夌浜屾鎸ユ墜锛歋erver鏀跺埌FIN鍚庯紝鍙戦�佷竴涓狝CK缁機lient锛岀‘璁ゅ簭鍙蜂负鏀跺埌搴忓彿+1锛堜笌SYN鐩稿悓锛屼竴涓狥IN鍗犵敤涓�涓簭鍙凤級锛孲erver杩涘叆CLOSE_WAIT鐘舵�併��

聽锛�3锛夌涓夋鎸ユ墜锛歋erver鍙戦�佷竴涓狥IN锛岀敤鏉ュ叧闂璖erver鍒癈lient鐨勬暟鎹紶閫侊紝Server杩涘叆LAST_ACK鐘舵�併��

聽 锛�4锛夌鍥涙鎸ユ墜锛欳lient鏀跺埌FIN鍚庯紝Client杩涘叆TIME_WAIT鐘舵�侊紝鎺ョ潃鍙戦�佷竴涓狝CK缁橲erver锛岀‘璁ゅ簭鍙蜂负鏀跺埌搴忓彿+1锛孲erver杩涘叆CLOSED鐘舵�侊紝瀹屾垚鍥涙鎸ユ墜銆�

涓冨眰妯″瀷锛屽嵆OSI锛圤pen System Interconnect锛夛紝寮�鏀惧紡绯荤粺浜掕仈


5銆佽兘绠�鍗曡涓�涓嬩綘浜嗚В鐨剆ocket鍚楋紵


6銆丳ython鐨凣IL锛堝叏灞�瑙i噴鍣ㄩ攣锛�

7銆丳ython鐨剋ith璇彞锛熶紭鐐癸紵濡備綍瀹炵幇锛�

8銆丳ython鐨勫崟渚嬫ā寮忥紵

9銆丳ython鍨冨溇鍥炴敹鏈哄埗锛�

10銆丳ython鐨勭敓鎴愬櫒鍜岃凯浠e櫒锛�

11銆乸ython鐨勮楗板櫒锛�

12銆丳ython鐨剋ith璇彞鐨勪娇鐢紵

13銆丳ython鍑芥暟涓粡甯告湁 *args锛�**kwargs 杩欎袱涓弬鏁帮紝瀹冧滑鏄粈涔堟剰鎬濓紝涓轰粈涔堣浣跨敤瀹冧滑锛�


14銆丏jango鐨勮姹傜敓鍛藉懆鏈�

鐢ㄦ埛璇锋眰鍒版潵 锛岄�氳繃url杩涜(鑷笂鑰屼笅)鐨勮矾鐢卞尮閰�, 鍖归厤鎴愬姛鍒欐槧灏勫埌瀵瑰簲瑙嗗浘銆�

閫氳繃涓氬姟閫昏緫鐨勫鐞�(鏁版嵁搴�...), 缁忔ā鏉挎覆鏌撶户鑰屽搷搴旂粰鐢ㄦ埛鍐呭

15銆丏jango鐨勪腑闂翠欢鍋氫粈涔堬紵

涓棿浠舵槸鐢ㄦ潵澶勭悊璇锋眰鍜屽搷搴旂殑鎻掍欢鏈哄埗锛屽叏灞�鍦板鐞咲jango鐨勮姹傚拰鍝嶅簲銆傛瘡涓腑闂翠欢閮芥湁鐗瑰畾鍔熻兘銆侻iddleWare鐨勯渶瑕佸湪settings閲岄潰杩涜閰嶇疆锛屾寜椤哄簭閰嶇疆涓婂搴旂被鐨勫湪Python涓叏璺緞銆侻iddleWare涓�鑸兘鏄疢iddlewareMixin鐨勫瓙绫汇�傞�氬父瀹炵幇process_request鏂规硶鍜宲rocess_response鏂规硶銆�

process_request鏄湪璇锋眰鏉ヤ复鏃讹紝灏氭湭姝e垯鍖归厤鍓嶆墽琛岀殑鍑芥暟銆傚鏋滆繖涓嚱鏁版病鏈夎繑鍥炲�硷紝鎴栬�呰繑鍥炰负None锛屼細缁х画鎵ц鍏朵粬涓棿浠剁殑process_request鏂规硶銆傚鏋滆繑鍥濰ttpResponse锛孌jango灏嗕笉浼氭墽琛屽悗搴忕殑涓棿浠跺拰views鍑芥暟锛岀洿鎺ュ皢response杩斿洖銆�

process_view鍑芥暟鏄湪鑾峰彇鍒拌鎵ц鐨剉iew鍑芥暟鍜岀浉搴斿弬鏁帮紝鍦╲iew鍑芥暟鎵ц涔嬪墠鎵ц鐨勫嚱鏁般�備篃鏄彲浠ヨ繑鍥濶one鎴朒ttpResponse銆�

process_response鏄湪鎵ц瀹寁iew鍑芥暟锛屽苟鐢熸垚response涔嬪悗锛屾墽琛岀殑鍑芥暟銆傚彲浠ュresponse鍋氳繘涓�姝ョ殑澶勭悊銆傚繀椤昏繑鍥濰ttpResponse瀵硅薄銆�


16銆丏jango鐨凮RM鎬庝箞鑾峰彇鍒颁竴涓猲ame瀛楁鎬绘暟鎹湁娌℃湁鈥渁bc鈥濓紝ORM鎿嶄綔

17銆乫orm楠岃瘉锛�


18銆佽涓�涓嬬嚎绋嬮攣锛屼粈涔堟椂鍊欑敤锛�

19銆佹弿杩颁綘鎬庝箞鐢ㄧ殑tornado锛�

20銆乀ornado鎬庝箞鍋氬埌寮傛鐨勶紵

21銆丼crapy浜嗚В澶氬皯锛�

22銆佺埇铏殑鏁版嵁鍏ュ簱鎿嶄綔锛岀嚎绋嬮渶瑕佸姞閿佸悧锛�


23銆丮ysql鐨勪紭鍖栵紵


24銆佷簡瑙h楗板櫒鍚楋紵鍙互鐢ㄦ潵鍋氫粈涔堬紵鏈変粈涔堜紭缂虹偣锛�

25銆佷娇鐢ㄨ繃epoll锛熻兘鑱婅亰IO澶氳矾澶嶇敤鍚楋紵

你可能感兴趣的:(python闈㈣瘯棰�)