作者thanksgive (乾溫)
看板Python
標題[問題] 如何較快從key,value list建立 dict list
時間Mon Oct 21 16:10:05 2019
大家好~
現在有個需求,
當rows多的時候,會變得很慢~
目前的做法有點慢,
不曉得大家有無比較快的解法?
謝謝~
columns是欄位名稱
rows裡面每個元素都是一個tuple,該tuple個元素其值按位置剛好對應於columns名稱
columns: list of str
rows: list of tuple
def foo(columns, rows):
result = [dict(zip(columns, r)) for r in rows]
return result
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.250.17.41 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1571645407.A.A4D.html
※ 編輯: thanksgive (111.250.17.41 臺灣), 10/21/2019 16:11:12
→ IAMPF: rows多是多多 慢是多慢? 10/21 17:00
推 jiyu520: pandas? 10/21 18:22
推 outshaker: namedtuple 這玩意你有聽過嗎? 10/21 19:49
推 TitanEric: pandas感覺超適合 10/21 22:04