简体   繁体   中英

Getting an error in while appending an unpacked list in Python

I was trying to construct a function that returns all the ways the target string can be formed using the list of strings

For example, for allConstruct('aa', ['a','aa','aaa']), I get [['a', 'a'], ['aa']] as output. But when I pass allConstruct('aaa', ['a','aa','aaa']), I get the following error:

"result.append(*targetWays)

TypeError: append() takes exactly one argument (2 given)"

def allConstruct(target, words, memo={}):
    if target == '':
        return [[]]
    
    result =[]
    for word in words:
        if target.find(word)==0:
            suffix = target[len(word):]
            suffixWays = allConstruct(suffix, words)
            targetWays = list(map(lambda way: [word, *way],suffixWays))
            result.append(*targetWays)
            
    return result

Try result.extend(targetWays) . This adds all elements in list to the result.

Or if you want to add the list itself just remove * just like this: result.append(targetWays) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM