迭代器product
来源:互联网 发布:数据分析师主要做什么 编辑:程序博客网 时间:2024/06/06 19:29
from itertools import productpinyin_list = [[u'shei shi', u'shui shi'], [u'kai fa'], [u'jiao cheng']]keys = []for i in range(len(pinyin_list)): tmp_keys = [item for item in pinyin_list[i]] # print tmp_keys if i >= 1: keys = [' '.join(item) for item in product(keys, tmp_keys)] # print keys else: keys = tmp_keysprint keys
product(iter1, iter2, ... iterN, [repeat=1]):创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数。1 def product(*args, **kwds):2 # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy3 # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 1114 pools = map(tuple, args) * kwds.get('repeat', 1)5 result = [[]]6 for pool in pools:7 result = [x+[y] for x in result for y in pool]8 for prod in result:9 yield tuple(prod)神奇的迭代器。。。
0 0
- 迭代器product
- product
- Product
- Product
- Product
- Product
- Max product
- 2510: Product
- 10106 - Product
- JOJ2510:Product
- Product line
- 10106 - Product()
- uva10106 - Product
- 10106 - Product
- 10106 - Product
- 5309 Product
- Product Management
- 10106 - Product
- 算法概论---思维导图
- Android里面所应用到的注册权限
- 百度地图API
- 版权控制SCM
- Codeforces Round #235 (Div. 2) / 410D Roman and Numbers (带有整除性质的数位DP)
- 迭代器product
- sort命令
- Linux重定向介绍
- stringstream用法
- 连网
- AOJ-AHU-OJ-453 棋盘问题(位压缩)
- 【主席树】2012集训队互测 Middle
- Android - 使用资源(resource)
- JavaSE 反射Reflection初学