V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lostapple
V2EX  ›  Python

请教各位大侠一个 排列组合的问题,非常感谢!

  •  
  •   lostapple · 2016-08-30 18:28:14 +08:00 · 1441 次点击
    这是一个创建于 2812 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1. a 是一个列表,其元素是列表, 例: a = [[2,3], [3,4,5,6], [9], [88,77]]
    2. a 列表的长度不确定
    3. a 列表中的元素也是长度不确定的列表

    取 a 列表中的 num 个元素(也是列表),每个元素中取一个字符,与其他元素中的字符进行排列组合

    向各位大侠请教,怎样设计代码才更 pythonic? 有没有更好的处理方法?

    thanks for yr time !

    '''

    # using python3.5
    
    import itertools
    import copy
    
    # 对两个列表中元素进行排列
    def listmlist(list1, list2):
        for a in list1:
            for b in list2:
                yield str(a) + str(b)
            	yield str(b) + str(a)
    
    # 取列表中所有的元素进行排列,采用递归
    def alllist(u):
        for i in u:
            temp = copy.deepcopy(u)
            temp.remove(i)
            if len(temp) > 1:
                return set(listmlist(i, alllist(temp)))
            else:
                return set(listmlist(i, temp[0]))
    
    # 取列表中 num 个元素进行排列
    def permute(u, num):
        result = set()
    	# 取 2 个元素的情况
        if num == 2:
            for g in itertools.combinations(u, r=num):
                result.update(set(listmlist(*g)))
            return result
    	# 取 1 个元素的情况
        if num == 1:
            for g in itertools.combinations(u, r=num):
                result.update(set(g[0]))
            return result
    
    	# 取所有元素的情况,调用 alllist()
        if len(u) == num:
            return alllist(u)  
    
    	# 取两个元素以上的情况
        for i in u:
            temp = copy.deepcopy(u)
            temp.remove(i)
    
            for g in itertools.combinations(temp, r=num-1):
                if len(g) > 2:
                    result.update(makepasswd(list(g), num=len(g)))   #这个地方总觉得有问题,算是递归吗?
                else:
                    result.update(set(listmlist(i, listmlist(*g))))
        return result
    
    if __name__ == '__main__':
    	a =	[[2,3], [3,4,5,6], [9], [88,77]]
    	print(permute(a, 2))
    	print(permute(a, 4))
    	print(permute(a, 1))
    

    '''

    2 条回复    2016-08-31 09:07:52 +08:00
    hitmanx
        1
    hitmanx  
       2016-08-30 18:49:21 +08:00   ❤️ 1
    output = [s for s in itertools.product(*a)]

    花了 2 分钟,拿 google 一搜就有了"python nested lists permutation"..
    lostapple
        2
    lostapple  
    OP
       2016-08-31 09:07:52 +08:00
    @hitmanx 谢谢,没注意 product 方法,现成的方法确实方便啊,继续努力
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   6154 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 02:44 · PVG 10:44 · LAX 19:44 · JFK 22:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.