将值连接到字符串 - 错误

时间:2012-11-30 12:52:12

标签: python string dictionary

我有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']

1 个答案:

答案 0 :(得分:1)

createHouses中,您已为某些字典键添加了列表,例如: -

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

因此,您无法在list中将strx['fn']+" "+x['sn']对象连接起来。由于x['fn']会为您提供list个对象。

所以,将它们改为: -

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

等等。