标签: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