栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

生成具有重复元素的列表排列

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

生成具有重复元素的列表排列

该网页看起来很有希望。

def next_permutation(seq, pred=cmp):    """Like C++ std::next_permutation() but implemented as    generator. Yields copies of seq."""    def reverse(seq, start, end):        # seq = seq[:start] + reversed(seq[start:end]) +         #       seq[end:]        end -= 1        if end <= start: return        while True: seq[start], seq[end] = seq[end], seq[start] if start == end or start+1 == end:     return start += 1 end -= 1    if not seq:        raise StopIteration    try:        seq[0]    except TypeError:        raise TypeError("seq must allow random access.")    first = 0    last = len(seq)    seq = seq[:]    # Yield input sequence as the STL version is often    # used inside do {} while.    yield seq[:]    if last == 1:        raise StopIteration    while True:        next = last - 1        while True: # Step 1. next1 = next next -= 1 if pred(seq[next], seq[next1]) < 0:     # Step 2.     mid = last - 1     while not (pred(seq[next], seq[mid]) < 0):         mid -= 1     seq[next], seq[mid] = seq[mid], seq[next]     # Step 3.     reverse(seq, next1, last)     # Change to yield references to get rid of     # (at worst) |seq|! copy operations.     yield seq[:]     break if next == first:     raise StopIteration    raise StopIteration>>> for p in next_permutation([int(c) for c in "111222"]):...     print p... [1, 1, 1, 2, 2, 2][1, 1, 2, 1, 2, 2][1, 1, 2, 2, 1, 2][1, 1, 2, 2, 2, 1][1, 2, 1, 1, 2, 2][1, 2, 1, 2, 1, 2][1, 2, 1, 2, 2, 1][1, 2, 2, 1, 1, 2][1, 2, 2, 1, 2, 1][1, 2, 2, 2, 1, 1][2, 1, 1, 1, 2, 2][2, 1, 1, 2, 1, 2][2, 1, 1, 2, 2, 1][2, 1, 2, 1, 1, 2][2, 1, 2, 1, 2, 1][2, 1, 2, 2, 1, 1][2, 2, 1, 1, 1, 2][2, 2, 1, 1, 2, 1][2, 2, 1, 2, 1, 1][2, 2, 2, 1, 1, 1]>>>

2017-08-12

七年后,这是一个更好的算法(为清楚起见,更好):

from itertools import permutationsdef unique_perms(series):    return {"".join(p) for p in permutations(series)}print(sorted(unique_perms('1122')))


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646599.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号