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 - 工作),只需点击左边的复选标记;) –