【问题标题】:TypeError: must be str, not dict in PythonTypeError:必须是 str,而不是 Python 中的 dict
【发布时间】:2017-12-22 01:40:18
【问题描述】:

我正在关注 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

【问题讨论】:

  • 看看pandas,它非常适合管理数据集和处理诸如对相关项目进行分组之类的事情。
  • @Barmar 我试图根据编辑的部分声明一个对象数组,但我收到了一些错误消息。你介意看看吗?
  • 哦,对不起,我的错。我习惯了 JavaScript 中的对象数组。它应该是 t.freqSet 并且它现在正在工作。现在我要删除重复项和“frozenset”字样
  • @Barmar 嘿,我在尝试插入 Firebase 时遇到了另一个错误。你有什么想法为什么会这样?
  • 您希望+ {conseqStr: t.conf} 做什么?您不能将字典连接到字符串。

标签: python arrays list loops firebase


【解决方案1】:

解决了编辑部分:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多