TypeError:必须是str,而不是Python中的dict

时间:2017-12-22 01:40:19

标签: python arrays list loops firebase

我正在关注this tutorial并尝试提取某些值以插入Firebase。

如何实际将某些类型组合在一起,删除重复项并插入具有所需结构的Firebase?例如:

household cleaning
    kitchen appliance: conf
    air freshener: conf
    laundry & ironing needs: conf
kitchen appliance
    household cleaning: conf
    ...

因为我打算做类似的事情,对于每种类型,我将相关类型与其conf一起并插入到Firebase中。但截至目前,我不知道如何实现它。到目前为止我尝试的是:

result = list()
calcConf()
    result.append((freqSet-conseq, conseq, conf))
for freqSet in result:
    for item in freqSet:
        print(item)

它确实拆分了每个相关类型:

set({'household cleaning'})
set({'kitchen appliance'})
0.5087719298245614
set({'kitchen appliance'})
set({'household cleaning'})
0.4172661870503597
set({'household cleaning'})
set({'air freshener'})
0.7105263157894738

但我不知道如何进一步将所需的结构插入到Firebase中。以前我在JavaScript中使用对象数组,但我不确定它是否也适用于Python。有什么想法吗?

谢谢!

修改

我尝试使用对象数组:

class parts:
    def __init__(self, s1, s1, s3):
        self.s1= s1
        self.s2= s2
        self.s3= s3

然后,我按原样填充结果列表:

result.append(parts(freqSet, conseq, conf))

当我尝试插入firebase时:

for t in result:
    fstr= t.freqSet.strip('set({})')
    cstr= t.conseq.strip('set({})')
    print(fstr, cstr, t.conf)

    firebase.patch('/test/' + fstr+ {cstr: t.conf})  

我收到此错误消息:

 firebase.patch('/marketBasketAnalysis/' + fstr + {cstr: t.conf})
TypeError: must be str, not dict

1 个答案:

答案 0 :(得分:0)

通过以下方式解决了编辑部分:

firebase.patch('/test/' + fstr , {cstr: t.conf})