python-itertools的使用
程序员文章站
2024-02-12 13:05:04
...
简介
迭代生成器工具包 itertools
更多功能请参考:http://www.wklken.me/posts/2013/08/20/python-extra-itertools.html
使用
排序:permutations
组合:combinations
# 排序:permutations
# 组合:combinations
from itertools import permutations
from itertools import combinations
col_list = ['col1', 'col2', 'col3']
print('所有排序:')
for value in permutations(col_list,2):
print(list(value))
print("所有组合:")
for value in combinations(col_list,2):
print(list(value))
结果如下:
所有排序:
['col1', 'col2']
['col1', 'col3']
['col2', 'col1']
['col2', 'col3']
['col3', 'col1']
['col3', 'col2']
所有组合:
['col1', 'col2']
['col1', 'col3']
['col2', 'col3']