(1)python查字典快还是列表快?
答:在Python中,字典是通过哈希表实现的。也就是说,字典是一个数组,而数组的索引是键经过哈希函数处理后得到的。哈希函数的目的是使键均匀地分布在数组中。列表是通过偏移读取,字典是通过键,因此字典块。(前提是针对同一问题)
(2)pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection的原因和解决方案
import sys
import re
print(sys.path.append(os.path.abspath(__file__).rsplit('/', 2)[0]))
from utils.dbutils import DBconnection
db_keggdrug = DBconnection("keggdrug") # connect db:keggdrug
db_DrugKB = DBconnection("DrugKB") # connect db:DrugKB
print("MongoDB connected successfully!")
##########################################################################################
def insert_kegg_ddi():
"""将keggdrug.interaction重新插入keggdrug.ddi表里"""
ddi_dict = {}
ddi_dict.update({'keggdrug-url': ddi_item['kegg_url'],
'keggdrug-ddi-url': ddi_item['interaction']['ddi_url']})
for ddi_item in db_keggdrug.mdbi['interactions'].find().batch_size(5):
if 'interaction' in ddi_item.keys():
for item in ddi_item['interaction']['partner']:
if 'dr_id' in item.keys():
ddi_dict = {}
ddi_dict.update({'keggdrug-url': ddi_item['kegg_url'],
'keggdrug-ddi-url': ddi_item['interaction']['ddi_url']})
ddi_dict.update({'keggdrug-ddi-id': [ddi_item['drug_id'], item['dr_id']]})
ddi_dict.update({'keggdrug-ddi-name': [ddi_item['drug_name'], item['drug_name']]})
ddi_dict.update({'metabolism': item['medic_enzyme']})
ddi_dict.update({'statistical-parameter': item['statis_para']})
print(ddi_dict)
db_keggdrug.mdbi['ddi'].insert_one(ddi_dict)
print('Done!')
# ==================================================================================
if __name__ == '__main__':
insert_kegg_ddi()
报错如下:
Traceback (most recent call last):
File "/home/cqfnenu/PsyDrugKB/keggdrugs/test.py", line 49, in <module>
insert_kegg_ddi()
File "/home/cqfnenu/PsyDrugKB/keggdrugs/test.py", line 43, in insert_kegg_ddi
db_keggdrug.mdbi['dddi'].insert_one(ddi_dict)
File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 698, in insert_one
session=session),
File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 612, in _insert
bypass_doc_val, session)
File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 600, in _insert_one
acknowledged, _insert_command, session)
File "/usr/local/lib/python3.6/dist-packages/pymongo/mongo_client.py", line 1492, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "/usr/local/lib/python3.6/dist-packages/pymongo/mongo_client.py", line 1385, in _retry_with_session
return func(session, sock_info, retryable)
File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 597, in _insert_command
_check_write_command_response(result)
File "/usr/local/lib/python3.6/dist-packages/pymongo/helpers.py", line 221, in _check_write_command_response
_raise_last_write_error(write_errors)
File "/usr/local/lib/python3.6/dist-packages/pymongo/helpers.py", line 202, in _raise_last_write_error
raise DuplicateKeyError(error.get("errmsg"), 11000, error)
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: keggdrug.dddi index: _id_ dup key: { _id: ObjectId('5fe15f835e9129725cac95d9') }
问题分析:
创建字典时,放在了for循环外。
传递给mongodb的一直是都是同一个dict对象,所以mongo保存时会出现 "_id" 重复的问题。
所以,只需要把创建的字典放到循环里面,即可解决。
import os
import sys
print(sys.path.append(os.path.abspath(__file__).rsplit('/', 2)[0]))
from utils.dbutils import DBconnection
db_keggdrug = DBconnection("keggdrug") # connect db:keggdrug
db_DrugKB = DBconnection("DrugKB") # connect db:DrugKB
print("MongoDB connected successfully!")
##########################################################################################
def insert_kegg_ddi():
"""将keggdrug.interaction重新插入keggdrug.ddi表里"""
for ddi_item in db_keggdrug.mdbi['interactions'].find().batch_size(5):
if 'interaction' in ddi_item.keys():
for item in ddi_item['interaction']['partner']:
if 'dr_id' in item.keys():
ddi_dict = {}
ddi_dict.update({'keggdrug-url': ddi_item['kegg_url'],
'keggdrug-ddi-url': ddi_item['interaction']['ddi_url']})
ddi_dict.update({'keggdrug-ddi-id': [ddi_item['drug_id'], item['dr_id']]})
ddi_dict.update({'keggdrug-ddi-name': [ddi_item['drug_name'], item['drug_name']]})
ddi_dict.update({'metabolism': item['medic_enzyme']})
ddi_dict.update({'statistical-parameter': item['statis_para']})
print(ddi_dict)
db_keggdrug.mdbi['dddi'].insert_one(ddi_dict)
print('Done!')
# ==================================================================================
if __name__ == '__main__':
insert_kegg_ddi()
注:这个会不定时更新!
(3)AttributeError:module tensorflow no attribute app解决办法
这是因为tensorflow版本问题,服务器端是tensorflow2.0,而源代码是tensorflow1.几写的,所以有错误。
解决办法:法(1)将import tensorflow as tf 改为 import tensorflow.compat.v1 as tf
法(2)用tf.compat.v1.flags
替换 tf.app.flags
(4)
tf.placeholder() is not compatible with eager execution的解决方法
解决方法:在代码头部添加
tf.compat.v1.disable_eager_execution()