日誌11月7日

2021-10-10 08:17:42 字數 1814 閱讀 6937

吃透這些易錯點,為2級做準備:

列印出兩個列表的笛卡爾積

解法1:使用生成器表示式產生笛卡爾積,可以幫忙省掉執行 for 迴圈的開銷。

colors = [『blacks』, 『white』]

sizes = [『s』, 『m』, 『l』]

for tshirt in (』%s %s』%(c, s) for c in colors for s in sizes):

print(tshirt)

blacks s

blacks m

blacks l

white s

white m

white l

解法2:使用 itertools 裡的 product 生成器函式。

import itertools

list(itertools.product([『blacks』, 『white』], [『s』, 『m』, 『l』]))

[(『blacks』, 『s』), (『blacks』, 『m』), (『blacks』, 『l』), (『white』, 『s』), (『white』, 『m』), (『white』, 『l』)]

可迭代物件拆包時,怎麼賦值給佔位符

我們經常用 for 迴圈提取元組裡的元素,對於我們不想接收的元素,我們可以用佔位符 _ 接收。

player_infos = [(『kobe』, 『24』), (『james』, 『23』), (『iverson』, 『3』)]

for player_names, _ in player_infos:

print(player_names)

kobe

james

iverson

python3 中,用什麼方式接收不確定值或引數

用 *args 的方式,*args 位置可以在任意位置。

a, b, *c = range(8)

a, b, c

(0, 1, [2, 3, 4, 5, 6, 7])

a, *b, c, d = range(5)

a,b,c,d

(0, [1, 2], 3, 4)

*a, b, c, d = range(5)

a,b,c,d

([0, 1], 2, 3, 4)

用切片將物件倒序

s = 『basketball』

s[::-1]

『llabteksab』

怎麼檢視列表的 id

l = [1, 2, 3]

id(l)

4507638664

可變序列用*=(就地乘法)後,會建立新的序列嗎?

不會,可變序列用*=(就地乘法)後,不會建立新的序列,新元素追加到老元素上,以列表為例,我們看下新老列表的id,相等的。

l = [1, 2, 3]

id(l)

4507939272

l *= 2

l[1, 2, 3, 1, 2, 3]

id(l)

4507939272

日誌11月6日

今天深度學習類 class 這是它基礎用法 類 class 用來描述具有相同的屬性和方法的物件的集合。它定義了該集合中每個物件所共有的屬性和方法。物件是類的例項。類變數 類變數在整個例項化的物件中是公用的。類變數定義在類中且在函式體之外。類變數通常不作為例項變數使用。方法重寫 如果從父類繼承的方法不...

日誌11月9日

今天學習訪問屬性 完整例項 例項class employee 所有員工的基類 empcount 0def init self,name,salary self.name name self.salary salary employee.empcount 1def displaycount self ...

日誌11月13日

12月11日就要靠2級了,做了幾套真題,代嗎如下 class student def index self name none age none gender none english none mathematics none chinese none deftotal points self p...