擅长:python、mysql、java
<p>这段代码假设属性总是在变量上</p>
<pre><code>'''
The common behaviour of picking an attribute on adult
like pedisrem and doing ( adult.weights * <attribute of adults> == 1).sum()/1e6
has been achieved using list comprehension, we store those attributes
in equalsToOne array and iterate over them.
'''
multiplier = 'weight'
equalsToOne = ['pedisrem', 'pedisout', 'pedisdrs', 'pedisphy', 'pediseye', 'pedisear']
total_cognitive_m, total_mobility_m, total_self_care_m, \
total_physical_m, total_vision_m, total_hearing_m =
[(getattr(adults, multiplier) * getattr(adults, var) == 1).sum()/1e6
for var in equalsToOne]
total_disabled_m = (adults.weight * adults.disabled).sum()/1e6
</code></pre>