码迷,mamicode.com
首页 > 其他好文 > 详细

Statistics and Linear Algebra 1

时间:2016-11-30 07:40:16      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:index   numbers   racket   follow   pen   assign   function   use   style   

1. Add a value to each element in a list:

  degrees_zero = [f + 459.67 for f in fahrenheit_degrees]

2. Assign the index of a list into the list:

  survey_responses = ["none", "some", "a lot", "none", "a few", "none", "none"]

  survey_scale = ["none", "a few", "some", "a lot"]

  survey_numbers = [survey_scale.index(response) for response in survey_responses]

  average_smoking = sum(smoke_dic)/len(smoke_dic)

3. Categorize scales: here we need to filter gender list into categorizes and find the corresponse saving.  

  gender = ["male", "female", "female", "male", "male", "female"]
  savings = [1200, 5000, 3400, 2400, 2800, 4100]

//////////////////////////////////Solutions one////////////////////////////////////////

  male_saving_list = []

  for i in range(len(gender)):
    if gender[i] == "male":
      male_saving_list.append(savings[i])
      male_savings = sum(male_savings_list)/ len(male_savings_list)

//////////////////////////////////Solutions two////////////////////////////////////////

  female_saving_list = [savings[i] for i in range(len(gender)) if gender[i] == "female"] # Here we use square bracket to replace append function. savings[i] is the value we want, if conditions have to follow the for loop like solution one.
  female_savings = sum(female_saving_list)/len(female_saving_list)

4. 

Statistics and Linear Algebra 1

标签:index   numbers   racket   follow   pen   assign   function   use   style   

原文地址:http://www.cnblogs.com/kingoscar/p/6116145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!