0
我在這裏有一個作業:這個練習的哪種方式會更「Pythonic」?
給定一個int數組,返回數組中的數字9。
array_count9([1, 2, 9]) → 1
array_count9([1, 9, 9]) → 2
array_count9([1, 9, 9, 3, 9]) → 3
我有兩個想法,這一點,一個是:
def array_count9(nums):
count = 0
list1 = [x for x in nums if x==9]
return len(list1)
另:
def array_count9(nums):
count = 0
for n in nums:
if n==9:
count +=1
return count
但我不知道哪一種方式會更Python,在以下方面性能,清晰度......?非常感謝
@TruongTroll如果這個答案是對你有幫助,請不要忘了[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an - 工作),只需點擊左邊的複選標記;) –