【问题标题】:Python find ALL combinations of a list [duplicate]Python查找列表的所有组合[重复]
【发布时间】:2012-09-18 23:09:57
【问题描述】:

可能重复:
Power set and Cartesian Product of a set python

解决老问题。我想通了一切。现在我有一个更疯狂的问题。这是我应该得到的:

输入:scoreList(["a", "s", "m", "t", "p"])

输出:[['a', 1], ['am', 4], ['at', 2], ['spam', 8]]

这个 I/O 工作得很好,但是如果我像这样添加第 6 个元素:

输入:scoreList(["a", "s", "m", "t", "p", "e"])

程序像疯了一样出故障。请告诉我如何解决这个问题。感谢任何帮助

我的代码:

from itertools import chain, combinations

def ind(e,L):
    if L==[] or L=="":
        return 0
    elif L[0]==e:
        return 0
    else:
        return ind(e,L[1:])+1

def letterScore(letter, scorelist):
    if scorelist[0][0] == letter:
        return scorelist[0][1]
    elif (len(scorelist) == 1) and (scorelist[0][0] != letter):
        return 'lol. stop trying to crash my program'
    else:
        return letterScore(letter, scorelist[1:])

scorelist = [ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2], ["h", 4], ["i", 1], ["j", 8], ["k", 5], ["l", 1], ["m", 3], ["n", 1], ["o", 1], ["p", 3], ["q", 10], ["r", 1], ["s", 1], ["t", 1], ["u", 1], ["v", 4], ["w", 4], ["x", 8], ["y", 4], ["z", 10] ]

def wordScore(S, scorelist):
    if (len(S) == 1):
        return letterScore(S[0],scorelist)
    elif (letterScore(S[0],scorelist) == 'lol. stop trying to crash my program'):
        return 'you really want to crash me, dont you'
    else:
        return letterScore(S[0],scorelist) + wordScore(S[1:], scorelist)


def perm(l):
    sz = len(l)
    if sz <= 1:
        return [l]
    return [p[:i]+[l[0]]+p[i:]
        for i in xrange(sz) for p in perm(l[1:])]


from itertools import combinations, permutations

def findall(my_input):
    return [''.join(p) for x in range(len(my_input)) for c in combinations(my_input, x+1)
            for p in permutations(c)]

d = ["a", "am", "cab", "apple", "at", "bat", "bar", "babble", "can", "foo", "spam", "spammy", "zzyzva"]

def match(lis): 
    return match2(findall(lis))

def match2(lis): 
    if lis == []:
        return []
    elif(len(d) != ind(lis[0],d)):
        return [lis[0]] + match2(lis[1:])
    else:
        return match2(lis[1:])

def scoreList(lis):
    return match3(match(lis))

def match3(lis):
    if (lis == []):
        return []
    else:
        return [[lis[0],wordScore(lis[0],scorelist)]] + match3(lis[1:])

【问题讨论】:

  • 错误信息丢失...请附上
  • &lt;generator object allperm at 0x0000000002AA8438&gt; 不是错误消息,它是调用allperm() 返回的生成器的文本表示。迭代它以检索值。
  • 正如@kindall 所说,试试 list(allperm('abc'))

标签: python for-loop


【解决方案1】:

这是作业吗,还是可以用itertools?

>>> my_input = ['a','b','c']
>>> from itertools import combinations, permutations
>>> [''.join(p) for x in range(len(my_input)) for c in combinations(my_input, x+1)
                for p in permutations(c)]
['a', 'b', 'c', 'ab', 'ba', 'ac', 'ca', 'bc', 'cb', 'abc', 'acb', 'bac', 'bca', 'cab', 'cba']

【讨论】:

  • 这是硬件的一部分,所以我不能使用 itertools,但这不是整个任务。这只是我遇到问题的部分
【解决方案2】:

可能不是最易读的,但这是另一种解决方案,使用itertools 和这个answer

>>> from itertools import permutations
>>> inpt = ['a', 'b', 'c']
>>> sum([map(''.join, list(permutations(inpt, l + 1))) for l in xrange(len(inpt))], [])
['a', 'b', 'c', 'ab', 'ac', 'ba', 'bc', 'ca', 'cb', 'abc', 'acb', 'bac', 'bca', 'cab', 'cba']

【讨论】:

  • 请注意,不鼓励在列表上使用sum(),因为它具有二次性能
  • @gnibbler 是的..这就是为什么我链接到另一个答案:)
【解决方案3】:

较早的答案显示了itertools 包的用法,但如果您不想使用它(功课是您这样做的唯一原因),我发现this 算法最容易实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多