Python中的itertools.product
生活随笔
收集整理的這篇文章主要介紹了
Python中的itertools.product
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
例子1:import itertools
a = itertools.product([1,2,3],[100,200]) print(a) for item in itertools.product([1,2,3],[100,200]):print(item)輸出如下:
<itertools.product object at 0x000001DC4A92B828> (1, 100) (1, 200) (2, 100) (2, 200) (3, 100) (3, 200)例子2:
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學(xué)習教程和PDF電子書! ''' vp_pairs = itertools.product(range(5), range(5)) print(vp_pairs) for item in itertools.product(range(5), range(5)):print(item)輸出結(jié)果:
<itertools.product object at 0x000001FB2773B870> (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (1, 0) (1, 1) (1, 2) (1, 3) (1, 4) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 0) (3, 1) (3, 2) (3, 3) (3, 4) (4, 0) (4, 1) (4, 2) (4, 3) (4, 4)總結(jié):product(A, B)函數(shù),返回A、B中的元素的笛卡爾積的元組。product(list1, list2) 依次取出list1中的每1個元素,與list2中的每1個元素,組成元組,
然后,將所有的元組組成一個列表,返回。
總結(jié)
以上是生活随笔為你收集整理的Python中的itertools.product的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python中数据的保存和读取
- 下一篇: Python初学的几个迷惑点