Python Function examples

Comienza Ya. Es Gratis
ó regístrate con tu dirección de correo electrónico
Python Function examples por Mind Map: Python Function examples

1. Function to create frequency table for given data set and column index

1.1. def freq_table(index, data_set): frequency_table = {} for row in data_set[1:]: value = row[index] if value in frequency_table: frequency_table[value] += 1 else: frequency_table[value] = 1 return frequency_table ratings_ft = freq_table(data_set=apps_data, index=7)

2. extract data

2.1. def extract(data_set, index): column = [] for row in data_set[1:]: value = row[index] column.append(value) return column

3. sum list

3.1. def find_sum(a_list): a_sum = 0 for element in a_list: a_sum += float(element) return a_sum

4. find length of list

4.1. def find_length(a_list): length = 0 for element in a_list: length += 1 return length

5. find mean

5.1. def mean(data_set, index): list = extract(data_set, index) return find_sum(list) / find_length(list)

5.2. requires extact data, sum list and find length of list