python 迭代器 排列組合

2021-09-25 10:19:23 字數 4837 閱讀 2161

概述

迭代器是訪問集合元素的一種方式。迭代器物件從集合的第乙個元素開始訪問,直到所有的元素被訪問完結束。迭代器只能往前不會後退。

可迭代物件

迭代器提供了乙個統一的訪問集合的介面。只要是實現了__iter__()或getitem()方法的物件,就可以使用迭代器進行訪問。

序列:字串、列表、元組

非序列:字典、檔案

自定義類:使用者自定義的類實現了__iter__()或__getitem__()方法的物件

迭代字典

d = 

for k in d:

print(k)

for k in d.keys():

print(k)

for v in d.values():

print(v)

for (k,v) in d.items():

print(k,v)

迭代基礎

f1=open('data.txt')

# for line in f1.readlines(): #把所有行讀入記憶體,遇到大檔案效率差

# print(line)

# for line in f1:

# print(line)

# 檔案物件就是自己的迭代器

print(f1.__next__())

print(f1.__next__())

# 為了手動支援迭代,python3.0提供了乙個next()方法,他會自動呼叫物件的_next_()

print(next(f1))

iter() 和 next()

字串/陣列本身沒有迭代物件

s='hello'

iter01=iter(s)

print(next(iter01))

print(next(iter01))

arr=[1,2,3,4]

e=iter(arr)

# print(e.__next__())

# print(next(e))

while true:

try:

x=e.__next__()

except stopiteration:

break

print(x)

字典物件有乙個迭代器,每次返回字典的key

params=

for key in params:

...#所以不要下面的寫法

for key in params.keys():

...

range()

range()支援多個迭代器,而其他內建函式不支援

arr=list(range(20))

print(arr)

r=iter(range(100))

print(next(r))

print(next(r))

print(next(r))

map() zip() filter()

和range()不同,以上三個函式都是自己的迭代器

m=map(abs,[2,-3,-1,3])

print(next(m))

itertools 是python的迭代器模組,itertools提供的工具相當高效且節省記憶體

###無限迭代的迭代器

count(初值=0, 步長=1)

無限迭代

from itertools import count

for i in count(10): #從10開始無限迴圈

if i > 20:

break

# pass

else:

print(i)

slice(count(10), 5)

從 10 開始,輸出 5 個元素後結束。islice 的第二個引數控制何時停止迭代。但其含義並不是」達到數字 5 時停止「,而是」當迭代了 5 次之後停止「。

from itertools import count,islice

for i in islice(count(10), 5):

print(i)

cycle()

from itertools import cycle

count = 0

for item in cycle('xyz'):

if count > 7:

break

print(item)

count += 1

這裡我們建立了乙個 for 迴圈,使其在三個字母 xyz 間無限迴圈。當然,我們並不真地想要永遠迴圈下去,所以我們新增了乙個簡單的計數器來跳出迴圈。

###可終止迭代器

accumulate(可迭代物件[, 函式])

accumulate 迭代器將返回累計求和結果,或者傳入兩個引數的話,由傳入的函式累積計算的結果。預設設定為相加,我們趕快試一試吧:

>> from itertools import accumulate

>>> list(accumulate(range(10)))

[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

這裡,我們 匯入了 accumulate,然後傳入 10 個數字,0-9。迭代器將傳入數字依次累加,所以第乙個是 0 ,第二個是 0+1, 第三個是 1+2,如此下去。現在我們匯入 operator 模組,然後新增進去:

>>> import operator

>>> list(accumulate(range(1, 5), operator.mul))

[1, 2, 6, 24]

這裡我們傳入了數字 1-4 到 accumulate 迭代器中。我們還傳入了乙個函式:operator.mul,這個函式將接收的引數相乘。所以每一次迭代,迭代器將以乘法代替除法(1×1=1, 1×2=2, 2×3=6, 以此類推)。

accumulate 的文件中給出了其他一些有趣的例子,例如貸款分期償還,混沌遞推關係等。這絕對值得你花時間去看一看。

combinations(iterable, r)

方法可以建立乙個迭代器,返回iterable中所有長度為r的子串行,返回的子串行中的項按輸入iterable中的順序排序。

import itertools

list1 = [1, 3, 4, 5]

list2 = list(itertools.combinations(list1, 2))

print(list2)

返回結果:

[(1, 3), (1, 4), (1, 5), (3, 4), (3, 5), (4, 5)]

實現一組資料的所有排列組合

import itertools

list1 = [1, 3, 4, 5]

list2 =

for i in range(1, len(list1)+1):

iter1 = itertools.combinations(list1, i)

print(list2)

返回結果:

[[(1,), (3,), (4,), (5,)], [(1, 3), (1, 4), (1, 5), (3, 4), (3, 5), (4, 5)], [(1, 3, 4), (1, 3, 5), (1, 4, 5), (3, 4, 5)], [(1, 3, 4, 5)]]

#排列組合

python內建的排列組合函式有四個:

product 笛卡爾積  (有放回抽樣排列)

permutations 排列  (不放回抽樣排列)

combinations 組合,沒有重複  (不放回抽樣組合)

combinations_with_replacement 組合,有重複  (有放回抽樣組合)

permutations 在數學中可以理解為:a(3|4)=4*3*2=24

combinations 在數學中可以理解為:c(3|4)=c(1|4)=4

例題如下:

from itertools import combinations ,permutations

list01 = [1, 2, 3, 5,]

a = list(combinations(list01,3))

b = list(permutations(list01,3))

print(a) #[(1, 2, 3), (1, 2, 5), (1, 3, 5), (2, 3, 5)]

print(b) #[(1, 2, 3), (1, 2, 5), (1, 3, 2), (1, 3, 5), (1, 5, 2), (1, 5, 3), (2, 1, 3), (2, 1, 5), (2, 3, 1), (2, 3, 5), (2, 5, 1), (2, 5, 3), (3, 1, 2), (3, 1, 5), (3, 2, 1), (3, 2, 5), (3, 5, 1), (3, 5, 2), (5, 1, 2), (5, 1, 3), (5, 2, 1), (5, 2, 3), (5, 3, 1), (5, 3, 2)]

print(len(b)) #24

python 排列組合

from scipy.special import comb,perm 計算排列數 a perm 3,2 計算組合數 c comb 45,2 print int a int c 6 990方法 說明種類 permutations 排列不放回抽樣排列 combinations 組合,沒有重複 不放回抽...

迭代出元素的排列組合

排列組合 如果你想要找出某個序列的排列組合情況,有以下三個函式可以供你使用 from itertools import permutations,combinations,combinations with replacement my items c1 c2 c3 permutations ite...

python實現排列組合

排列組合是組合學最基本的概念。所謂排列,就是指從給定個數的元素中取出指定個數的元素進行排序。組合則是指從給定個數的元素中僅僅取出指定個數的元素,不考慮排序。itertools參考文件 import itertools itertools.combinations iterable,r 引數說明 it...