在代碼審查的挑戰,我最近有下面的代碼名爲unpythonic:Python的v Unpythonic
def word_count(string):
wc_dict = {}
word = ""
index = 0
while index < len(string):
if string[index].isalnum():
word = word + string[index]
index += 1
else:
if word:
if word.lower() in wc_dict:
wc_dict[word.lower()] += 1
else:
wc_dict[word.lower()] = 1
word = ""
index += 1
if word:
if word.lower() in wc_dict:
wc_dict[word.lower()] += 1
else:
wc_dict[word.lower()] = 1
return wc_dict
我還提交不同的代碼,我用Ruby寫了一個挑戰,有這樣的解決方案稱爲「Python化」 。
代碼是什麼意思是Pythonic/Unpythonic?
Unpythonic = Pythetic :-) – paxdiablo 2014-09-24 05:07:34
你可以直接迭代字符串,你知道。這可能是我發現的一件事。 – Makoto 2014-09-24 05:09:21
[谷歌搜索「pythonic」](https://www.google.com/search?q=pythonic)給出了一些非常明確的結果解釋該術語。 – user2357112 2014-09-24 05:13:01