在python中将列表作为对象附加到新列表
作为列表查询mongoengine db我想将它们附加到一个新的列表中,该列表是可编辑的。我当前的代码:在python中将列表作为对象附加到新列表,python,mongodb,list,iterable,Python,Mongodb,List,Iterable,作为列表查询mongoengine db我想将它们附加到一个新的列表中,该列表是可编辑的。我当前的代码: data=[] other_doc = Document.objects(bank="boe_dd4a95f6ec1c41ba47239fe6fd688b8cc1232c3d25a68b76836172d99164cb82") data.append(other_doc) other_doc_1 = Document.objects(_id="boe_585cb87956f09c48c999
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)
在python中,您可以简单地执行
data+=other\u 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
您可以执行
data+=other\u doc
或data.extend(other\u doc)
。“扩展”会在其他现有列表的末尾添加一个列表。谢谢,如果StackOverflow允许,我将接受您的回答!