您可以構造sqlite
數據庫表並查詢它們以獲取所需結果。
import sqlite3, operator
reference = [[1, 0, 0], [2, 0, 10], [3, 0, 20], [4, 0, 30], [5, 0, 40]]
temperature = [[0, 0, 100], [0, 10, 110], [0, 20, 120], [0, 30, 130], [0, 40, 140]]
一對幫助者 - 我喜歡使用它們,因爲它使後續的代碼可讀。
reference_coord = operator.itemgetter(1,2)
ref = operator.itemgetter(0)
temperature_coord = operator.itemgetter(0,1)
temp = operator.itemgetter(2)
創建一個數據庫(在內存中)
con = sqlite3.connect(":memory:")
兩種方法可以解決這個,保存在單獨的表中的所有信息,或者建立一個單一的表,只有數據你想
每個清單一張表
con.execute("create table reference(coordinate TEXT PRIMARY KEY, reference INTEGER)")
con.execute("create table temperature(coordinate TEXT PRIMARY KEY, temperature INTEGER)")
# fill the tables
parameters = [(str(reference_coord(item)), ref(item)) for item in reference]
con.executemany("INSERT INTO reference(coordinate, reference) VALUES (?, ?)", parameters)
parameters = [(str(temperature_coord(item)), temp(item)) for item in temperature]
con.executemany("INSERT INTO temperature(coordinate, temperature) VALUES (?, ?)", parameters)
查詢的數據的兩個表需要
cursor = con.execute('SELECT reference.reference, temperature.temperature FROM reference, temperature WHERE reference.coordinate = temperature.coordinate')
print(cursor.fetchall())
表,結合了數據在兩個名單
con.execute("create table data(coordinate TEXT PRIMARY KEY, reference INTEGER, temperature INTEGER)")
建設只用數據你所關心的表約
parameters = [(str(reference_coord(item)), ref(item)) for item in reference]
con.executemany("INSERT INTO data(coordinate, reference) VALUES (?, ?)", parameters)
parameters = [(temp(item), str(temperature_coord(item))) for item in temperature]
con.executemany("UPDATE data SET temperature=? WHERE coordinate=?", parameters)
簡單的查詢,因爲表只有你想要的
cursor2 = con.execute('SELECT reference, temperature FROM data')
print(cursor2.fetchall())
con.close()
結果:
>>>
[(1, 100), (2, 110), (3, 120), (4, 130), (5, 140)]
[(1, 100), (2, 110), (3, 120), (4, 130), (5, 140)]
>>>
一旦你的數據到數據庫中是相當容易從中提取信息,如果一個文件DB來代替一個數據庫的數據庫可以持久記憶。
如果外部庫可以接受,pandas具有類似的功能,是一個很棒的軟件包。
定義「大量數據」 - 您是否需要多臺機器?或者只是尋找在單臺機器上運行的相對高效的解決方案?換句話說,它是否是大數據問題? – amit 2015-02-08 10:51:40
這可以通過嵌套for循環來完成。你還可以分享你迄今爲止所嘗試過的嗎? – 2015-02-08 10:55:07