Permalink
Browse files

添加一个 Python 列表推导式的实例,用 LISP 风格的结构计算条件求和。

  • Loading branch information...
1 parent 7fc74fe commit 888b600777172690ac86e352cdd624869be10bb6 @MarchLiu MarchLiu committed Feb 23, 2012
Showing with 20 additions and 0 deletions.
  1. +20 −0 python/sumit.py
View
20 python/sumit.py
@@ -0,0 +1,20 @@
+'''
+找出一个锯齿数组里长度大于5的子数组
+在符合要求的子数组里的数据里找出所有偶数
+如果数据小于10的话乘以2,大于10的除以2
+最后统计符合要求的数据的和
+'''
+inputdata = [
+ [2,8,9,13,72,67,88,35,44],
+ [33,28,47,2,10,45,66,92],
+ [22,34,60,43,0,72,52],
+ [10,11,53,58]
+ ]
+
+def sumIt(_input):
+ return sum([sum([item *2 if item < 10 else item/2
+ for item in data_area])
+ for data_area in _input
+ if len(data_area)>5])
+
+print sumIt(inputdata)

0 comments on commit 888b600

Please sign in to comment.