1

我有 14 个字典,它们都包含相同的信息键,但值不同。我正在尝试构建一个函数,当字典被列为函数中的参数时,它将组合一个句子。

错误是:

TypeError: can only concatenate list (not "str") to list

这是代码:

def createhouses(x): 
    count = 0
    for i in [f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14]:
                    i["sn"] = legendary[count]
                    i["fn"] = [legendaryfn[count]]
                    i["family"] = [hProfession[random.randint(0, len(hProfession)-1)]]
                    i["house"] = [houseGen()]
                    i["fortune"] = [prosperity[random.randint(0, len(prosperity)-1)]]
                    i["tort"] = random.randint(0, 1)
                    count+=1
createhouses(1)

以及引发错误的代码:

def houseHistory(x):
        print x['fn']+" "+x['sn']
4

1 回答 1

1

createHouses中,您已经为一些字典键放置了列表,例如:-

i["fn"] = [legendaryfn[count]]
i["house"] = [houseGen()]

因此,您不能将alist与. 既然给了你一个对象。strx['fn']+" "+x['sn']x['fn']list

因此,将它们更改为: -

i["fn"] = legendaryfn[count]
i["house"] = houseGen()

等等。

于 2012-11-30T12:54:37.857 回答