1
に値を格納する、私はこのようなスクリプトを使用して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,
"owner_identity": owner_identity
}
def collect_databases_data(options):
options["databases"]["data"] = [
get_data(connection.cursor())
for connection in options["databases"].values()
]
return options
をそして私は、辞書のリストを反復処理しています:
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
これは高速で(たぶん私は20.000の辞書を持つことができますが)説明なしでは判読できません。これは悪い習慣と考えられますか?私は避けるべきですか? Pythonプログラマーはリストやタプルが大好きですが、データを格納するためにタプルを使用するかどうかはまだ分かりません。
あなたがNamedTupleを使用してみましたが行を表現するには?これは辞書よりも軽量でなければならないが、説明はまだ埋め込まれている。 – Joe