diff --git "a/\345\255\243\346\200\273\347\273\223 PROJECT/Guided Project_ Profitable App Profiles for the App Store and Google Play Markets/data_analysis_seasonal_conclusion_project.py" "b/\345\255\243\346\200\273\347\273\223 PROJECT/Guided Project_ Profitable App Profiles for the App Store and Google Play Markets/data_analysis_seasonal_conclusion_project.py" index f81e85c..4b3b1d5 100644 --- "a/\345\255\243\346\200\273\347\273\223 PROJECT/Guided Project_ Profitable App Profiles for the App Store and Google Play Markets/data_analysis_seasonal_conclusion_project.py" +++ "b/\345\255\243\346\200\273\347\273\223 PROJECT/Guided Project_ Profitable App Profiles for the App Store and Google Play Markets/data_analysis_seasonal_conclusion_project.py" @@ -1,297 +1,458 @@ -#引入 csv 库的 reader() 函数 -from csv import reader - -#引入 explore_data() function -def explore_data(dataset, start, end, rows_and_columns = False): - dataset_slice = dataset[start:end] - for row in dataset_slice: #打印列表全部内容 - print(row) - print('\n') # 行间空 - - if rows_and_columns: - print('Number of rows:',len(dataset)) # 行数 - print('Number of columns',len(dataset[0])) # 列数 - -# _*_ coding:utf-8 _*_ -#Apple dataset -open_fileA = open('AppleStore.csv',encoding='UTF-8') -read_fileA = reader(open_fileA) -ios = list(read_fileA) -ios_T_head = ios[0] -ios = ios[1:] #去掉首行的每列名称 - -#Google dataset -open_fileG = open('googleplaystore.csv',encoding='UTF-8') -read_fileG = reader(open_fileG) -android = list(read_fileG) -android_T_head = android[0] -android = android[1:] #去掉各自表格首行的每列名称 - -#有一行数据报错,确认错误 -print(android[10472]) -print('\n') -print(android[10473]) -print('\n') -print(android_T_head) - -#在删除掉错误数据前后确认一下表格行数变化 -print(len(android)) -del android[10472] #删掉这组有问题的数据 -print(len(android)) - -#除去重复的数据组 -#除去 Android 的重复数据组 -duplicate_apps = [] -unique_apps = [] - -for app in android: - name = app[0] - if name in unique_apps: - duplicate_apps.append(name) - else: - unique_apps.append(name) - - -dup_num = len(duplicate_apps) - -print('\n') -print('number of duplicated apps:',dup_num ) -print('\n') -print('Expected length:', (len(android) - dup_num)) -print('\n') - -#用新建 dictionary 除去重复数据组 - -reviews_max = {} - -for app in android: - name = app[0] - n_reviews = float(app[3]) - - if name in reviews_max and reviews_max[name] < n_reviews: - reviews_max[name] = n_reviews - - elif name not in reviews_max: - reviews_max[name] = n_reviews - - -#检查一下 reviews_max 中元素数量是不是对 -print('Expected length:', len(android) - 1181) -print('\n') -print('Actual number:',len(reviews_max)) - -#开始清除重复应用数据 -android_clean = [] -already_added = [] - -for app in android: - name = app[0] - n_reviews = float(app[3])#这里利用了数据组的第四列 id 的独一无二来区分相同 name 的重复数据组 - - if (reviews_max[name] == n_reviews) and (name not in already_added): - android_clean.append(app) - already_added.append(name) - -explore_data(android_clean, 0, 2, True) #检验元素数量是否正确 - -#除去非英语 app 的数据 -def is_english(string): - non_ascii = 0 - - for character in string: - if ord(character) > 127: #利用英语字母的 ASCII 编码小于127 - non_ascii += 1 - - if non_ascii > 3: - return False - else: - return True - -#选出英语应用 -android_english = [] -ios_english = [] - -for app in android_clean: - name = app[0] - if is_english(name): - android_english.append(app) - -for app in ios: - name = app[2]#出过一个乌龙,注意序列号 - if is_english(name): - ios_english.append(app) - -print('android English Apps samples and total number:') -print('\n') -explore_data(android_english, 0, 2, True) -print('\n') -print('ios English Apps samples and total number:') -print('\n') -explore_data(ios_english, 0, 2, True) - -#选出免费应用 -android_final = [] -ios_final = [] - -for app in android_english: - price = app[7] - if price == '0': - android_final.append(app) - -for app in ios_english: - price = app[5] - if price == '0': - ios_final.append(app) - -print('Number of android free English Apps:',len(android_final)) -print('Number of ios free English Apps:',len(ios_final)) -print('\n') - -#clean data end______________ - -# data analysis of genre -def freq_table(dataset, index): - table = {} - total = 0 - - for row in dataset: - total += 1 - value = row[index] - if value in table: - table[value] += 1 - else: - table[value] = 1 - - table_percentages = {} - - for genre in table: - percentage = (table[genre] / total) * 100 - table_percentages[genre] = percentage - - return table_percentages - - -def display_table(dataset, index): - table = freq_table(dataset, index) - table_display = [] - for key in table: - key_val_as_tuple = (table[key], key) - table_display.append(key_val_as_tuple) - - table_sorted = sorted(table_display, reverse = True) - for entry in table_sorted: - print(entry[1], ':', entry[0]) - -#Content Rating -print('Content Rating:') -display_table(ios_final, -5) -print('\n') - -#category -print('Category:') -display_table(android_final, 1) -print('\n') - -#Genres -print('Genres:') -display_table(android_final, -4) -print('\n') - -#average number of installs for each ios app genre -genres_ios = freq_table(ios_final, -5) - -for genre in genres_ios: - total = 0 - len_genre = 0 - for app in ios_final: - genre_app = app[-5] - if genre_app == genre: - n_ratings = float(app[6]) - total += n_ratings - len_genre += 1 - avg_n_ratings = total / len_genre - print(genre, ':', avg_n_ratings) -print('\n') - -#user rating -for app in ios_final: - if app[-5] == 'Navigation': - print(app[2], ':', app[6]) # print name and number of ratings -print('\n') - -#Reference -for app in ios_final: - if app[-5] == 'Reference': - print(app[2], ':', app[6]) -print('\n') - -#Most Popular Apps by Genre on Google Play -display_table(android_final, 5) # the Installs columns -print('\n') - -# name + procentage -categories_android = freq_table(android_final, 1) - -for category in categories_android: - total = 0 - len_category = 0 - for app in android_final: - category_app = app[1] - if category_app == category: - n_installs = app[5] - n_installs = n_installs.replace(',', '') - n_installs = n_installs.replace('+', '') - total += float(n_installs) - len_category += 1 - avg_n_installs = total / len_category - print(category, ':', avg_n_installs) -print('\n') - -# -for app in android_final: - if app[1] == 'COMMUNICATION' and (app[5] == '1,000,000,000+' - or app[5] == '500,000,000+' - or app[5] == '100,000,000+'): - print(app[0], ':', app[5]) -print('\n') - -# If we removed all the communication apps -# that have over 100 million installs, -# the average would be reduced roughly ten times: - -under_100_m = [] - -for app in android_final: - n_installs = app[5] - n_installs = n_installs.replace(',', '') - n_installs = n_installs.replace('+', '') - if (app[1] == 'COMMUNICATION') and (float(n_installs) < 100000000): - under_100_m.append(float(n_installs)) - -print(sum(under_100_m) / len(under_100_m)) -print('\n') - -# -for app in android_final: - if app[1] == 'BOOKS_AND_REFERENCE': - print(app[0], ':', app[5]) -print('\n') - -# -for app in android_final: - if app[1] == 'BOOKS_AND_REFERENCE' and (app[5] == '1,000,000,000+' - or app[5] == '500,000,000+' - or app[5] == '100,000,000+'): - print(app[0], ':', app[5]) -print('\n') - -# -for app in android_final: - if app[1] == 'BOOKS_AND_REFERENCE' and (app[5] == '1,000,000+' - or app[5] == '5,000,000+' - or app[5] == '10,000,000+' - or app[5] == '50,000,000+'): - print(app[0], ':', app[5]) - -if __name__ == '__main__': - fire.Fire() \ No newline at end of file +#!/usr/bin/python3 + +import getopt + +import sys + +from csv import reader + + +# +file_handle = open('result.txt',mode='w') + +def explore_data(dataset, start, end, rows_and_columns = False): + dataset_slice = dataset[start:end] + for row in dataset_slice: #打印列表全部内容 + print(row) + print('\n') # 行间空 + + DS = str(row) + file_handle.write(DS) + file_handle.write('\n') + + if rows_and_columns: + print('Number of rows:',len(dataset)) # 行数 + Row = ['Number of rows:',str(len(dataset))] + file_handle.writelines(Row) + print('Number of columns:',len(dataset[0])) # 列数 + Col = ['Number of columns:',str(len(dataset[0]))] + file_handle.writelines(Col) + +def freq_table(dataset, index): + table = {} + total = 0 + + for row in dataset: + total += 1 + value = row[index] + if value in table: + table[value] += 1 + else: + table[value] = 1 + + table_percentages = {} + + for genre in table: + percentage = (table[genre] / total) * 100 + table_percentages[genre] = percentage + + return table_percentages + + +def display_table(dataset, index): + table = freq_table(dataset, index) + table_display = [] + for key in table: + key_val_as_tuple = (table[key], key) + table_display.append(key_val_as_tuple) + + table_sorted = sorted(table_display, reverse = True) + for entry in table_sorted: + print(entry[1], ':', entry[0]) + G = [str(entry[1]),':', str(entry[0])] + file_handle.writelines(G) + file_handle.write('\n') + +#UTF-8 编码以防万一 +#_*_ coding:utf-8 _*_ + + +if __name__ == '__main__': + opts, args = getopt.getopt(sys.argv[1:], 'A:B:', ['Table_A=','Table_B='])#可以输入两个其他时期的 Applestore 和 Googleplay 的数据表格进行分析 + + Table_A = 'AppleStore.csv' + Table_B = 'googleplaystore.csv' + + for key, value in opts: + + if key in ['-A','--Table_A']: + Table_A = value + + if key in ['-B','--Table_B']: + Table_B = value + + # 读取 Applestore dataset + open_fileA = open( Table_A ,encoding='UTF-8') + read_fileA = reader(open_fileA) + ios = list(read_fileA) + ios_T_head = ios[0] + ios = ios[1:] #去掉首行的每列名称 + + #读取 Googleplay dataset + + open_fileG = open( Table_B ,encoding='UTF-8') + read_fileG = reader(open_fileG) + android = list(read_fileG) + android_T_head = android[0] + android = android[1:] #去掉各自表格首行的每列名称 + + +print('##导入数据完成##\n ##开始对数据的清理##') +file_handle.write('##开始对数据的清理##\n\n') + +#得知 Googleplay dataset 中有一行数据是错误的,确认错误位置 +print('【确认 Googleplay 中的错误数据行位置】') +file_handle.write('【确认 Googleplay 中的错误数据行位置】\n') +print(android[10472]) +print('\n') +A2 = [str(android[10472])] +file_handle.writelines(A2) +file_handle.write('\n\n') + +print(android[10473]) +print('\n') +A3 = [str(android[10473])] +file_handle.writelines(A3) +file_handle.write('\n\n') +print(android_T_head) +ATH = [str(android_T_head)] +file_handle.writelines(ATH) +file_handle.write('\n\n') + +#在删除掉错误数据前后确认一下表格行数变化 +print('【在删除掉错误数据前后确认一下表格行数变化】') +file_handle.write('【在删除掉错误数据前后确认一下表格行数变化】\n') +print(len(android)) +LA = str(len(android)) +file_handle.write(LA) +file_handle.write('\n\n') +del android[10472] #删掉这组有问题的数据 +print(len(android)) +print('\n') +LA = str(len(android)) +file_handle.write(LA) +file_handle.write('\n\n') + +#除去 android 的重复数据组 +print('【检查并清除 Googleplay 的重复数据组】') +file_handle.write('【检查并清除 Googleplay 的重复数据组】\n\n') + +duplicate_apps = [] +unique_apps = [] + +for app in android: + name = app[0] + if name in unique_apps: + duplicate_apps.append(name) + else: + unique_apps.append(name) + + +dup_num = len(duplicate_apps) + +print('number of duplicated apps:',dup_num ) +print('\n') +NAD = ['number of duplicated apps:',str(dup_num)] +file_handle.writelines(NAD) +file_handle.write('\n') +print('Expected length:', (len(android) - dup_num)) +print('\n') +EALD = ['Expected length:',str((len(android) - dup_num))] +file_handle.writelines(EALD) +file_handle.write('\n\n') + +#用新建 dictionary 除去重复数据组 + +reviews_max = {} + +for app in android: + name = app[0] + n_reviews = float(app[3]) + + if name in reviews_max and reviews_max[name] < n_reviews: + reviews_max[name] = n_reviews + + elif name not in reviews_max: + reviews_max[name] = n_reviews + + + +#检查一下 reviews_max 中元素数量是不是正确 +print('Expected length:', len(android) - 1181) +print('\n') +EAL = ['Expected length:',str((len(android) - 1181))] +file_handle.writelines(EAL) +file_handle.write('\n\n') +print('Actual number:',len(reviews_max)) +ANL= ['Actual number:',str(len(reviews_max))] +file_handle.writelines(ANL) +file_handle.write('\n\n') + + + +#开始清除重复应用数据 +android_clean = [] +already_added = [] + +for app in android: + name = app[0] + n_reviews = float(app[3])#这里利用了数据组的第四列 id 的独一无二来区分相同 name 的重复数据组 + + if (reviews_max[name] == n_reviews) and (name not in already_added): + android_clean.append(app) + already_added.append(name) + + +explore_data(android_clean, 0, 2, True) #检验元素样本和数量是否正确 +file_handle.write('\n\n') + +#除去非英语 app 的数据 +def is_english(string): + non_ascii = 0 + + for character in string: + if ord(character) > 127: #利用英语字母的 ASCII 编码小于127 + non_ascii += 1 + + if non_ascii > 3: + return False + else: + return True + +#选出英语应用 +print('【选出英语应用】') +file_handle.write('【选出英语应用】\n') + +android_english = [] +ios_english = [] + +for app in android_clean: + name = app[0] + if is_english(name): + android_english.append(app) + +for app in ios: + name = app[2]#出过一个乌龙,注意序列号 + if is_english(name): + ios_english.append(app) + +print('android English Apps samples and total number:\n') +file_handle.write('android English Apps samples and total number:\n') +explore_data(android_english, 0, 2, True) +print('\n') +file_handle.write('\n') +print('ios English Apps samples and total number:\n') +file_handle.write('ios English Apps samples and total number:\n\n') +explore_data(ios_english, 0, 2, True) + +#选出英语应用里的免费应用 +print('【选出免费的英语应用】') +file_handle.write('【选出免费的英语应用】\n') + +android_final = [] +ios_final = [] + +for app in android_english: + price = app[7] + if price == '0': + android_final.append(app) + +for app in ios_english: + price = app[5] + if price == '0': + ios_final.append(app) + +print('Number of android free English Apps:',len(android_final)) +NFEA = ['Number of android free English Apps:',str(len(android_final))] +file_handle.writelines(NFEA) +file_handle.write('\n') +print('Number of ios free English Apps:',len(ios_final)) +print('\n') +NFEI = ['Number of ios free English Apps:',str(len(ios_final))] +file_handle.writelines(NFEI) +file_handle.write('\n\n') + +print('##结束对数据的清理##\n') +file_handle.write('##结束对数据的清理##\n\n') + +# +print('##开始分析数据##\n') +file_handle.write('##开始分析数据##\n\n') + +# data analysis of genre app 热门种类百分比 + +#Content Rating 苹果应用内容分级占比 +print('【Applestore 应用不同分类占比(%)】') +file_handle.write('【Applestore 应用不同分类占比(%)】\n') +display_table(ios_final, -5) +print('\n') +file_handle.write('\n') + +#category 安卓应用分级占比 +print('【Googleplay 应用分级占比(%)】') +file_handle.write('【Googleplay 应用分级占比(%)】\n') +display_table(android_final, 1) +file_handle.write('\n') + +#Genres 安卓应用内容占比 +print('\n【Googleplay 应用内容各自占比(%)】') +file_handle.write('【Googleplay 应用内容各自占比(%)】\n') +display_table(android_final, -4) + +#average number of installs for each ios app genre 苹果各个应用内容的应用的平均下载量 +print('\n【Applestore 不同应用分类应用的平均下载量】') +file_handle.write('\n【Applestore 不同应用分类应用的平均下载量】\n') + +genres_ios = freq_table(ios_final, -5) + +for genre in genres_ios: + total = 0 + len_genre = 0 + for app in ios_final: + genre_app = app[-5] + if genre_app == genre: + n_ratings = float(app[6]) + total += n_ratings + len_genre += 1 + avg_n_ratings = total / len_genre + + print(genre, ':', avg_n_ratings) + GIOS = [genre, ':',str(avg_n_ratings)] + file_handle.writelines(GIOS) + file_handle.write('\n\n') +print('\n') + +#user rating +print('【Applestore 导航类各应用的评价人数】') +file_handle.write('【Applestore 导航类各应用的评价人数】\n') +for app in ios_final: + if app[-5] == 'Navigation': + print(app[2],':', app[6]) # print name and number of ratings + AAA = [str(app[2]),':', str(app[6])] + file_handle.writelines(AAA) + file_handle.write('\n\n') +print('\n') + + +#Reference +print('【Applestore 参考类各应用的评价人数】') +file_handle.write('【Applestore 参考类各应用的评价人数】\n') +for app in ios_final: + if app[-5] == 'Reference': + print(app[2], ':', app[6]) + AAA = [str(app[2]),':', str(app[6])] + file_handle.writelines(AAA) + file_handle.write('\n\n') +print('\n') + +#Most Popular Apps by Genre on Google Play +print('【Googleplay 应用下载数量级占比(%)】') +file_handle.write('【Googleplay 应用下载数量级占比(%)】\n') +display_table(android_final, 5) # the Installs columns +print('\n') +file_handle.write('\n') + +# name + procentage +categories_android = freq_table(android_final, 1) +print('【Googleplay 不同分类应用平均下载量(%)】\n') +file_handle.write('【Googleplay 不同应用分类应用平均下载量(%)】\n') + +for category in categories_android: + total = 0 + len_category = 0 + for app in android_final: + category_app = app[1] + if category_app == category: + n_installs = app[5] + n_installs = n_installs.replace(',', '') + n_installs = n_installs.replace('+', '') + total += float(n_installs) + len_category += 1 + avg_n_installs = total / len_category + print(category, ':', avg_n_installs) + + CA = [category, ':', str(avg_n_installs)] + file_handle.writelines(CA) + file_handle.write('\n\n') +print('\n') + +# +print('【Googleplay 通信类应用高下载量软件及其下载数量级】\n') +file_handle.write('【Googleplay 通信类应用高下载量软件及其下载数量级】\n') +for app in android_final: + if app[1] == 'COMMUNICATION' and (app[5] == '1,000,000,000+' + or app[5] == '500,000,000+' + or app[5] == '100,000,000+'): + print(app[0], ':', app[5]) + AA = [app[0], ':', str(app[5])] + file_handle.writelines(AA) + file_handle.write('\n\n') +print('\n') + +#这个程序是为了看一下如果去掉那些超过一亿下载量的应用之后样本中Googleplay应用的平均下载量 + +under_100_m = [] + +for app in android_final: + n_installs = app[5] + n_installs = n_installs.replace(',', '') + n_installs = n_installs.replace('+', '') + if (app[1] == 'COMMUNICATION') and (float(n_installs) < 100000000): + under_100_m.append(float(n_installs)) + +print(sum(under_100_m) / len(under_100_m)) +print('\n') +SL = str(sum(under_100_m) / len(under_100_m)) +file_handle.write('【如果去掉那些超过一亿下载量的应用之后通信类应用样本中Googleplay应用的平均下载量】:\n') +file_handle.write(SL) +file_handle.write('\n\n') + + +# +print('【Googleplay 参考类软件及其下载数量级】\n') +file_handle.write('【Googleplay 参考类软件及其下载数量级】\n') + +for app in android_final: + if app[1] == 'BOOKS_AND_REFERENCE': + print(app[0], ':', app[5]) + AA = [app[0],':', app[5]] + file_handle.writelines(AA) + file_handle.write('\n\n') +print('\n') + +# +print('【Googleplay 参考类高下载量应用及其下载数量级】\n') +file_handle.write('【Googleplay 参考类高下载量应用及其下载数量级】\n') + +for app in android_final: + if app[1] == 'BOOKS_AND_REFERENCE' and (app[5] == '1,000,000,000+' + or app[5] == '500,000,000+' + or app[5] == '100,000,000+'): + print(app[0], ':', app[5]) + AA = [app[0],':', app[5]] + file_handle.writelines(AA) + file_handle.write('\n\n') +print('\n') + +# +print('【Googleplay 通信类软件中高下载量软件及其下载数量级】\n') +file_handle.write('【Googleplay 通信类软件中高下载量软件及其下载数量级】\n') + + +for app in android_final: + if app[1] == 'BOOKS_AND_REFERENCE' and (app[5] == '1,000,000+' + or app[5] == '5,000,000+' + or app[5] == '10,000,000+' + or app[5] == '50,000,000+'): + print(app[0], ':', app[5]) + AA = [app[0],':', app[5]] + file_handle.writelines(AA) + file_handle.write('\n\n') +print('\n') + + +print('##结束数据分析##\n请在当前文件夹内查看 result.txt 获取分析结果') +file_handle.write('##结束数据分析##') +file_handle.close() + + diff --git "a/\345\255\243\346\200\273\347\273\223 PROJECT/Guided Project_ Profitable App Profiles for the App Store and Google Play Markets/the_main_function_test/testB.py" "b/\345\255\243\346\200\273\347\273\223 PROJECT/Guided Project_ Profitable App Profiles for the App Store and Google Play Markets/the_main_function_test/testB.py" new file mode 100644 index 0000000..d366217 --- /dev/null +++ "b/\345\255\243\346\200\273\347\273\223 PROJECT/Guided Project_ Profitable App Profiles for the App Store and Google Play Markets/the_main_function_test/testB.py" @@ -0,0 +1,2 @@ +import testmain +testmain.test() diff --git "a/\345\255\243\346\200\273\347\273\223 PROJECT/Guided Project_ Profitable App Profiles for the App Store and Google Play Markets/the_main_function_test/testmain.py" "b/\345\255\243\346\200\273\347\273\223 PROJECT/Guided Project_ Profitable App Profiles for the App Store and Google Play Markets/the_main_function_test/testmain.py" new file mode 100644 index 0000000..0f7b285 --- /dev/null +++ "b/\345\255\243\346\200\273\347\273\223 PROJECT/Guided Project_ Profitable App Profiles for the App Store and Google Play Markets/the_main_function_test/testmain.py" @@ -0,0 +1,9 @@ +def test(): + print("the world is a lie") + +if __name__ == "__main__": + print("lazy boy") + +if __name__ == "__main__": + print("I am a Hero") + diff --git "a/\347\254\254\345\205\255\345\221\250py\347\273\203\344\271\240\346\226\207\344\273\266/object_oriented Python.py" "b/\347\254\254\345\205\255\345\221\250py\347\273\203\344\271\240\346\226\207\344\273\266/object_oriented Python.py" new file mode 100644 index 0000000..325c178 --- /dev/null +++ "b/\347\254\254\345\205\255\345\221\250py\347\273\203\344\271\240\346\226\207\344\273\266/object_oriented Python.py" @@ -0,0 +1,107 @@ +# +l = [1, 2, 3] +s = "string" +d = {"a": 1, "b": 2} +print(type(l)) +print(type(s)) +print(type(d)) + +# +class NewList(): + pass + +# +class NewList(DQ): + pass + +newlist_1 = NewList() +print(type(newlist_1)) + +# +class NewList(DQ): + def first_method(): + return "This is my first method" + +newlist = NewList() + +# +class NewList(DQ): + def first_method(self): + return "This is my first method" + +newlist = NewList() +result = newlist.first_method() + +# +class NewList(DQ): + def return_list(self, input_list): + return input_list + +newlist = NewList() +result = newlist.return_list([1, 2, 3]) + +# +class NewList(DQ): + """ + A Python list with some extras! + """ + def __init__(self, initial_state): + self.data = initial_state + +my_list = NewList([1, 2, 3, 4, 5]) +print(my_list.data) + +# +class NewList(DQ): + """ + A Python list with some extras! + """ + def __init__(self, initial_state): + self.data = initial_state + + def append(self, new_item): + """ + Append `new_item` to the NewList + """ + self.data = self.data + [new_item] + +my_list = NewList([1, 2, 3, 4, 5]) +print(my_list.data) +my_list.append(6) +print(my_list.data) + +# +class NewList(DQ): + """ + A Python list with some extras! + """ + def __init__(self, initial_state): + self.data = initial_state + self.calc_length() + + def calc_length(self): + """ + A helper function to calculate the .length + attribute. + """ + length = 0 + for item in self.data: + length += 1 + self.length = length + + def append(self, new_item): + """ + Append `new_item` to the NewList + """ + self.data = self.data + [new_item] + self.calc_length() + +fibonacci = NewList([1, 1, 2, 3, 5]) +print(fibonacci.length) + +fibonacci.append(8) +print(fibonacci.length) + + + + diff --git "a/\347\254\254\345\205\255\345\221\250py\347\273\203\344\271\240\346\226\207\344\273\266/working_with_dates_and_times_in_Python.py" "b/\347\254\254\345\205\255\345\221\250py\347\273\203\344\271\240\346\226\207\344\273\266/working_with_dates_and_times_in_Python.py" new file mode 100644 index 0000000..c20c20a --- /dev/null +++ "b/\347\254\254\345\205\255\345\221\250py\347\273\203\344\271\240\346\226\207\344\273\266/working_with_dates_and_times_in_Python.py" @@ -0,0 +1,71 @@ +# +from csv import reader +opened_file = open('potus_visitors_2015.csv') +read_file = reader(opened_file) +potus = list(read_file) +potus = potus[1:] + +# +import datetime as dt + +ibm_founded = dt.datetime(1911, 6, 16) +man_on_moon = dt.datetime(1969, 7, 20, 20, 17) + +date_format = "%m/%d/%y %H:%M" +for row in potus: + start_date = row[2] + start_date = dt.datetime.strptime(start_date, date_format) + row[2] = start_date + +# +visitors_per_month = {} + +for row in potus: + month_dt = row[2] + month_str = month_dt.strftime("%B, %Y") + if month_str not in visitors_per_month: + visitors_per_month[month_str] = 1 + else: + visitors_per_month[month_str] += 1 + +# +appt_times = [] + +for row in potus: + appt_dt = row[2] + appt_t = appt_dt.time() + appt_times.append(appt_t) + +# +min_time = min(appt_times) +max_time = max(appt_times) + +# +dt_1 = dt.datetime(1981, 1, 31) +dt_2 = dt.datetime(1984, 6, 28) +dt_3 = dt.datetime(2016, 5, 24) +dt_4 = dt.datetime(2001, 1, 1, 8, 24, 13) +answer_1 = dt_2 - dt_1 +answer_2 = dt_3 + dt.timedelta(days=56) +answer_3 = dt_4 - dt.timedelta(seconds=3600) + +# +for row in potus: + end_date = row[3] + end_date = dt.datetime.strptime(end_date, "%m/%d/%y %H:%M") + row[3] = end_date +appt_lengths = {} + +for row in potus: + start_date = row[2] + end_date = row[3] + length = end_date - start_date + if length not in appt_lengths: + appt_lengths[length] = 1 + else: + appt_lengths[length] += 1 + +min_length = min(appt_lengths) +max_length = max(appt_lengths) + +