开发者社区> 问答> 正文

Python编程求组合问题的function应该怎么弄?

Python编程求组合问题的function应该怎么弄?

展开
收起
游客ahv54x37wvm7u 2021-10-31 12:14:25 471 0
1 条回答
写回答
取消 提交回答
  • 没办法循环,但可以嵌套。 下面是代码跟测试结果 root@MBP:tmp# cat /tmp/py.py #!/usr/bin/env python

    -- coding: utf-8 --

    def Combination(l): list1=[1,2,3,4] list2=[5,6,7] list3=[8,9] list4=[10,11,12,13] def comb(ll): if len(ll) == 1: for e in ll[0]: yield [e] else: t = ll.pop(0) for e in comb(ll): for n in t: yield [n] + e v = vars() res = [v['list'+str(e+1)] for e in l] for e in comb(res): print e Combination([0,1]) root@MBP:tmp# python /tmp/py.py [1, 5] [2, 5] [3, 5] [4, 5] [1, 6] [2, 6] [3, 6] [4, 6] [1, 7] [2, 7] [3, 7] [4, 7]

    2021-10-31 12:14:52
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载