Python 将值存储在元组而不是字典中
目前,我正在使用以下脚本从10.000多个数据库获取数据:Python 将值存储在元组而不是字典中,python,dictionary,tuples,Python,Dictionary,Tuples,目前,我正在使用以下脚本从10.000多个数据库获取数据: def get_data(cursor): cursor.execute("SELECT * FROM COMPANIES") identity, closing_date, owner_identity = cursor.fetchall() return { "identity": identity, "closing_date": closing_date,
def get_data(cursor):
cursor.execute("SELECT * FROM COMPANIES")
identity, closing_date, owner_identity = cursor.fetchall()
return {
"identity": identity,
"closing_date": closing_date,
"owner_identity": owner_identity
}
def collect_databases_data(options):
options["databases"]["data"] = [
get_data(connection.cursor())
for connection in options["databases"].values()
]
return options
CompanyData = collections.namedtuple('CompanyData', 'identity, closing_date, owner_identity')
data = CompanyData(cursor.fetchone())
return data
for company in options["databases"]["data"]:
do_something_with(company.identity, company.owner_identity)
然后我反复浏览字典列表:
for data in options["databases"]["data"]:
# i do something here with identity, closing_date and owner_identity
我正在考虑更改脚本以返回元组,而不是字典:
def get_data(cursor):
cursor.execute("SELECT * FROM COMPANIES")
return cursor.fetchall()
def collect_databases_data(options):
options["databases"]["data"] = [
get_data(connection.cursor())
for connection in options["databases"].values()
]
return options
然后我可以:
for identity, closing_date, owner_identity in options["databases"]["data"]:
# I do something here with identity, closing_date and owner_identity
这会更快(有时我会有20000本字典),但没有解释就无法阅读。这被认为是一种不好的做法吗?我应该避免吗?我看到Python程序员喜欢列表和元组,但我还不知道他们是否也使用元组来存储数据。考虑在get\u data
函数中使用
您可以这样声明一个:
def get_data(cursor):
cursor.execute("SELECT * FROM COMPANIES")
identity, closing_date, owner_identity = cursor.fetchall()
return {
"identity": identity,
"closing_date": closing_date,
"owner_identity": owner_identity
}
def collect_databases_data(options):
options["databases"]["data"] = [
get_data(connection.cursor())
for connection in options["databases"].values()
]
return options
CompanyData = collections.namedtuple('CompanyData', 'identity, closing_date, owner_identity')
data = CompanyData(cursor.fetchone())
return data
for company in options["databases"]["data"]:
do_something_with(company.identity, company.owner_identity)
然后,您可以创建一个如下所示的:
def get_data(cursor):
cursor.execute("SELECT * FROM COMPANIES")
identity, closing_date, owner_identity = cursor.fetchall()
return {
"identity": identity,
"closing_date": closing_date,
"owner_identity": owner_identity
}
def collect_databases_data(options):
options["databases"]["data"] = [
get_data(connection.cursor())
for connection in options["databases"].values()
]
return options
CompanyData = collections.namedtuple('CompanyData', 'identity, closing_date, owner_identity')
data = CompanyData(cursor.fetchone())
return data
for company in options["databases"]["data"]:
do_something_with(company.identity, company.owner_identity)
然后像这样访问它:
def get_data(cursor):
cursor.execute("SELECT * FROM COMPANIES")
identity, closing_date, owner_identity = cursor.fetchall()
return {
"identity": identity,
"closing_date": closing_date,
"owner_identity": owner_identity
}
def collect_databases_data(options):
options["databases"]["data"] = [
get_data(connection.cursor())
for connection in options["databases"].values()
]
return options
CompanyData = collections.namedtuple('CompanyData', 'identity, closing_date, owner_identity')
data = CompanyData(cursor.fetchone())
return data
for company in options["databases"]["data"]:
do_something_with(company.identity, company.owner_identity)
考虑在get_data
函数中使用
您可以这样声明一个:
def get_data(cursor):
cursor.execute("SELECT * FROM COMPANIES")
identity, closing_date, owner_identity = cursor.fetchall()
return {
"identity": identity,
"closing_date": closing_date,
"owner_identity": owner_identity
}
def collect_databases_data(options):
options["databases"]["data"] = [
get_data(connection.cursor())
for connection in options["databases"].values()
]
return options
CompanyData = collections.namedtuple('CompanyData', 'identity, closing_date, owner_identity')
data = CompanyData(cursor.fetchone())
return data
for company in options["databases"]["data"]:
do_something_with(company.identity, company.owner_identity)
然后,您可以创建一个如下所示的:
def get_data(cursor):
cursor.execute("SELECT * FROM COMPANIES")
identity, closing_date, owner_identity = cursor.fetchall()
return {
"identity": identity,
"closing_date": closing_date,
"owner_identity": owner_identity
}
def collect_databases_data(options):
options["databases"]["data"] = [
get_data(connection.cursor())
for connection in options["databases"].values()
]
return options
CompanyData = collections.namedtuple('CompanyData', 'identity, closing_date, owner_identity')
data = CompanyData(cursor.fetchone())
return data
for company in options["databases"]["data"]:
do_something_with(company.identity, company.owner_identity)
然后像这样访问它:
def get_data(cursor):
cursor.execute("SELECT * FROM COMPANIES")
identity, closing_date, owner_identity = cursor.fetchall()
return {
"identity": identity,
"closing_date": closing_date,
"owner_identity": owner_identity
}
def collect_databases_data(options):
options["databases"]["data"] = [
get_data(connection.cursor())
for connection in options["databases"].values()
]
return options
CompanyData = collections.namedtuple('CompanyData', 'identity, closing_date, owner_identity')
data = CompanyData(cursor.fetchone())
return data
for company in options["databases"]["data"]:
do_something_with(company.identity, company.owner_identity)
您是否尝试过使用NamedTuple来表示行?这应该比字典更轻,但仍嵌入描述。您是否尝试使用NamedTuple表示行?这应该比一本字典更轻,但仍然嵌入了描述。