python基礎鞏固 資料結構和演算法6 10

2021-10-10 09:34:38 字數 4111 閱讀 8964

乙個字典就是乙個鍵對應乙個單值的對映, 如果想要乙個鍵對映多個值, 就需要將多個值放大另外的容器裡, 比如列表和集合

d =

e =,

'b':

}

也可以使用collections模組中的defaultdict來構造這樣的字段

from collections import defaultdict

d = defaultdict(

list)d[

'a']1)

d['a']2

)d['b']4)

d = defaultdict(

set)

d['a'

].add(1)

d['a'

].add(2)

d['b'

].add(

3)

defaultdict 會自動為將要訪問的鍵(就算目前字典中並不存在這樣的鍵)建立對映實體。如果你並不需要這樣的特性,你可以在乙個普通的字典上使用 setdefault() 方法來代替

from collections import defaultdict

d =d.setdefault(

'a',

)d.setdefault(

'a',

)2)d.setdefault(

'b',

)4)print

(d)

使用collections模組中的ordereddict類, 在迭代操作的時候它會保持元素被插入時的順序

from collections import ordereddict

d = ordereddict()d[

'foo']=

1d['bar']=

2d['spam']=

3d['grok']=

4for key in d:

# foo 1

# bar 2

# spam 3

# grok 4

print

(key, d[key]

)

ordereddict 內部維護著乙個根據鍵插入順序排序的雙向鍊錶。每次當乙個新的元素插入進來的時候,它會被放到鍊錶的尾部。對於乙個已經存在的鍵的重複賦值不會改變鍵的順序。

prices =

min_price =

min(

zip(prices.values(

), prices.keys())

)max_price =

max(

zip(prices.values(

), prices.keys())

)

a =

b =# 相同的鍵

print

(a.keys(

)& b.keys())

# # 相同鍵和值

print

(a.items(

)& b.items())

# # 查詢a和b不相同的鍵

print

(a.keys(

)- b.keys())

#

def

dedupe

(items)

: seen =

set(

)for item in items:

if item not

in seen:

yield item

seen.add(item)

a =[1,

5,2,

1,9,

1,5,

10]list1 = dedupe(a)

print

(list

(list1)

)

items =[0

,1,2

,3,4

,5,6

]a =

slice(2

,4)print

(items[a]

)

使用collections.counter類

from collections import counter

words =

['look'

,'into'

,'my'

,'eyes'

,'look'

,'into'

,'my'

,'eyes'

,'the'

,'eyes'

,'the'

,'eyes'

,'the'

,'eyes'

,'not'

,'around'

,'the'

,'eyes'

,"don't"

,'look'

,'around'

,'the'

,'eyes'

,'look'

,'into'

,'my'

,'eyes'

,"you're"

,'under'

]word_nums = counter(words)

top_three = word_nums.most_common(3)

print

(top_three)

使用 operator 模組的 itemgetter 函式

from operator import itemgetter

rows =[,

,,]rows_by_fname =

sorted

(rows, key=itemgetter(

'fname'))

print

(rows_by_fname)

itemgetter()函式也支援多個keys

rows_by_lfname =

sorted

(rows, key=itemgetter(

'lanme'

,'fname'

))

from operator import itemgetter

from itertools import groupby

rows =[,

,,,,

,,,]

# 先根據data排序

rows.sort(key=itemgetter(

'date'))

for date, items in groupby(rows, key=itemgetter(

'date'))

:print

(date)

for i in items:

print

(' '

, i)

mylist =[1

,4,-

5,10,

-7,2

,3,-

1]list1 =

[n for n in mylist if n >0]

print

(list1)

# [1, 4, 10, 2, 3]

mylist =[1

,4,-

5,10,

-7,2

,3,-

1]pos =

(n for n in mylist if n >0)

for x in pos:

print

(x)

prices =

p1 =

print

(p1)

python基礎鞏固 資料結構和演算法 1

迭代工具模組 import itertools 產生abcd的全排列 itertools.permutations abcd 產生abcde的五選三組合 itertools.combinations abcde 3 產生abcd和123的笛卡爾積 itertools.product abcd 123...

鞏固python基礎

1.變數由字母,數字,下劃線組成 2.不能用數字開頭 3.禁止使用關鍵字 and as assert break class continue def del elif else except ex ec finally for from global if import in is lambda ...

Python基礎(1) 資料結構和序列

不可變列表,使用圓括號來表示 dimensions 200 50 像列表一樣,用for迴圈來遍歷 for dimension in dimensions print dimension 修改元組變數,需要重新定義 dimensions 400 100 print nmodified dimensio...