2012-09-18 39 views
2

可能重複:
Power set and Cartesian Product of a set pythonPython中找到一個列表的所有組合

劃傷的老問題。我想出了一切。現在我有一個更瘋狂的問題。以下是我應該得到:

輸入:得分列表([ 「一」, 「S」, 「M」, 「T」, 「P」])

輸出:[[ 'A', 1],[ '是',4],[ '在',2],[ '垃圾郵件',8]]

此I/O工作很大,但如果我添加這樣的第六元件:

輸入: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:]) 
+1

錯誤信息丟失...請附上 – mawueth

+6

'<發生器對象allperm at 0x0000000002AA8438>'不是錯誤信息,它是通過調用'allperm()'返回的生成器的文本表示形式。迭代此檢索值。 – kindall

+0

由於@ kindall表示只是嘗試列表(allperm('abc')) – Odomontois

回答

4

這是家庭作業,或者您可以使用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'] 
+0

這是hw的一部分,所以我不能使用itertools,但這不是整個任務。這只是我遇到困難的部分 – user1681664

2

可能不是最可讀之一,但這裏是另一個解決方案,使用itertoolsanswer

>>> 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'] 
+0

請注意,使用'sum()'列表是不鼓勵的,因爲它具有二次性能 –

+0

@gnibbler是的..這就是爲什麼我鏈接到其他答案:) – Lipis

1

較早的答案顯示itertools包的使用,但如果你不想使用它(作業是你唯一的原因),我發現this算法是最容易實現的算法。

相關問題