2017-07-17 110 views
-2

將mongoengine db查詢爲列表我想將它們附加到可迭代的新列表。我當前的代碼:將列表附加到python中的新列表作爲對象

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) 

回答

1

在Python中,你可以簡單地做data += other_doc而不是ca lling附加。

所以完整的代碼是:

data=[] 
other_doc = Document.objects(bank="boe_dd4a95f6ec1c41ba47239fe6fd688b8cc1232c3d25a68b76836172d99164cb82") 
data += other_doc 
other_doc_1 = Document.objects(_id="boe_585cb87956f09c48c999f90617e69038d3e8e0ceadca2b6030495d4126f4ab5d") 
data += other_doc_1 
+1

謝謝,我會接受你的答案時,StackOverflow讓我! –

1

你可以做data += other_docdata.extend(other_doc)。 Extend將列表添加到其他現有列表的末尾。