将mongoengine数据库作为列表查询我想将它们附加到可迭代的新列表中。我目前的代码:
data=[]
other_doc = Document.objects(bank="boe_dd4a95f6ec1c41ba47239fe6fd688b8cc1232c3d25a68b76836172d99164cb82")
data.append(other_doc)
other_doc_1 = Document.objects(_id="boe_585cb87956f09c48c999f90617e69038d3e8e0ceadca2b6030495d4126f4ab5d")
data.append(other_doc_1)
输出:
[[Document boe_dd4a95f6ec1c41ba47239fe6fd688b8cc1232c3d25a68b76836172d99164cb82: date=2017-03-22 12:00:00, bank=Bank boe: name=Bank of England], [Document boe_585cb87956f09c48c999f90617e69038d3e8e0ceadca2b6030495d4126f4ab5d: date=2017-04-13 09:00:00, bank=Bank boe: name=Bank of England]]
期望的输出:
[Document boe_dd4a95f6ec1c41ba47239fe6fd688b8cc1232c3d25a68b76836172d99164cb82: date=2017-03-22 12:00:00, bank=Bank boe: name=Bank of England, Document boe_585cb87956f09c48c999f90617e69038d3e8e0ceadca2b6030495d4126f4ab5d: date=2017-04-13 09:00:00, bank=Bank boe: name=Bank of England]
所以我可以运行这个:
for i in other_doc:
doc = str(other_doc.extracted_text)
doc_tokens = tokenizer.tokenize(doc)
print(doc_tokens)
答案 0 :(得分:1)
在python中,您只需执行data += other_doc
而不是调用append。
所以完整的代码是:
data=[]
other_doc = Document.objects(bank="boe_dd4a95f6ec1c41ba47239fe6fd688b8cc1232c3d25a68b76836172d99164cb82")
data += other_doc
other_doc_1 = Document.objects(_id="boe_585cb87956f09c48c999f90617e69038d3e8e0ceadca2b6030495d4126f4ab5d")
data += other_doc_1
答案 1 :(得分:1)
您可以data += other_doc
或data.extend(other_doc)
。 Extend将列表添加到另一个现有列表的末尾。