嘻嘻羊 ✿
6 months ago
只有我這麼愛 list comprehension 嗎
latest #24
掰噗~ 好奇
6 months ago
哪一位聰明的人來回答一下吧?
嘻嘻羊 ✿
6 months ago
new_list = [f(item) for item in old_list]
嘻嘻羊 ✿
6 months ago
不覺得很棒嗎!
立即下載
嘻嘻羊 ✿
6 months ago
比起
new_list = []
for item in old_list:
new_list.append(f(item))
嘻嘻羊 ✿
6 months ago
啊縮排不見了
嘻嘻羊 ✿
6 months ago
不客氣的說我覺得比 list.map(f) 美啊
嘻嘻羊 ✿
6 months ago
雖然 list.map 才是主流啦
crosswalk
6 months ago
map 我可以接受,reduce 就蠻醜的

list comprehension 就是優雅的代表啊!
Jamway۞
6 months ago
這時候就要問
Jamway۞
6 months ago
List.map (lots)
Jamway۞
6 months ago
List comprehension (lots)
Jamway۞
6 months ago
直接for loop (lots)
Jamway۞
6 months ago
Reduce有比map好嗎(lots)
Jamway۞
6 months ago
天意
hatershater
6 months ago
我才正要說用timeit來測,但抽籤才是正解!
crosswalk
6 months ago
那個 for loop 在 interview 出現的話的確是大凶無誤
🤣🤣🤣🤣
噗神的功能越來越多了
hatershater
6 months ago
叫chatgpt幫測,他還幫我改code XDDD 但最後的答案是
List comprehension (Method 2) is usually the fastest and most Pythonic. map() (Method 3) can be slightly slower or similar, depending on the context and Python implementation. For loop (Method 1) is generally the slowest because of the overhead of method calls and append.
嘻嘻羊 ✿
6 months ago
噗神連這個也會,太厲害了吧(拜
嘻嘻羊 ✿
6 months ago
chatgpt 真的會下去跑嗎,還是他生 code 你要自己跑
嘻嘻羊 ✿
6 months ago
啊 python 是 list comprehension 是主流(還是只有我自己覺得)
但像是 java javascript 都是 list.map
go 倒是 Map(list, func),我果然是不會寫 go
Jamway۞
6 months ago
我在想如果連續問噗神一整個token表裡的吉凶來生成語句是不是會比LLM更讓人信服,根本高科技碟仙
hatershater
6 months ago
CC_27: 他生了code,我昨天忘了叫他跑,現在叫他跑過了。
For loop with append(): 0.368 seconds
List comprehension: 0.219 seconds
map() function: 0.204 seconds
reduce() with summing: 0.175 seconds (not directly comparable to the others)
hatershater
6 months ago
Summary:
Fastest list creation: map() and list comprehension, with map() slightly ahead here.

Slowest: The traditional for loop.

reduce() is fastest here but not doing the same operation (it reduces to a single value, not a list).
back to top