如果我正确理解itertools
"combinatoric iterators"文档,则想法是为每个常见的组合迭代提供一组标准函数。
但是我今天想念一个。我需要遍历具有重复项的每个ordered组合。
itertools
产量
combination_with_replacement('abcd', 4)
但是(即使结果按元组排序),这些组合也不是ordered。
我希望从理想的('a', 'a', 'a', 'a')
('a', 'a', 'a', 'b')
('a', 'a', 'a', 'c')
('a', 'a', 'a', 'd')
('a', 'a', 'b', 'b')
('a', 'a', 'b', 'c')
('a', 'a', 'b', 'd')
('a', 'a', 'c', 'c')
('a', 'a', 'c', 'd')
... etc
中获得更多结果,因为我需要区别对待
ordered_combination_with_replacement('abcd', 4)
换句话说:今天的订单很重要。
itertool是否提供这样的迭代?为什么不呢,或者为什么我错过了呢?迭代这些的标准方法是什么?我需要自己编写这个通用迭代器吗?
要总结一些评论,(至少)有两种方法可以做到这一点:
('a', 'a', 'a', 'a')
('a', 'a', 'a', 'b')
('a', 'a', 'b', 'a')
('a', 'b', 'a', 'a')
('b', 'a', 'a', 'a')
('a', 'a', 'a', 'c')
('a', 'a', 'c', 'a')
... etc
和
itertools.combinations_with_replacement("abcd", 4)
均产生以下所需结果:
itertools.product("abcd", repeat=4)