前提・実現したいこと
月ごとと年ごとの果物の収穫高を比較するプログラムを書いています。
比較した結果を反映した文章に起こしたいです。
上昇が見られたものとそうでないもの(下降傾向)をまとめようとしています。
発生している問題・エラーメッセージ
コード1の実行結果では、タプル型ではpopができないとのエラーがでており、
Traceback (most recent call last): File "untitled.py", line 61, in <module> month_index = month_data.pop(-1) AttributeError: 'tuple' object has no attribute 'pop'
該当のソースコード
コード1
python
1fruits = [ #foodptice category 2 "かき", 3 "りんご", 4 "みかん" 5] 6 7index = { 8 "2017": { 9 "1月": [901, 997, 1003], 10 "2月": [918, 984, 1005], 11 "3月": [891, 985, 1007] 12 }, 13 "2018": { 14 "1月": [885, 1006, 1010], 15 "2月": [877, 975, 1006], 16 "3月": [928, 999, 1006] 17 } 18} 19 20def month_change(m1, m2, m3): 21 22 month_status = [0]*len(fruits) 23 for idx_cat,cat in enumerate(fruits): 24 print(idx_cat,cat) 25 month_status[idx_cat] = (cat, 100*(m2[idx_cat] - m1[idx_cat])/m1[idx_cat], 26 100*(m3[idx_cat] - m2[idx_cat])/m2[idx_cat]) 27 28 return tuple(month_status) 29 30month_data = month_change(index['2018']['1月'], 31 index['2018']['2月'], 32 index['2018']['3月']) 33 34#for a single tuple 35res1 = () 36for rows in month_data: 37 res1 = res1 + rows 38 39def year_change(y1, y2): 40 months = ['1月', '2月', '3月'] 41 year_status = [0]*len(fruits) 42 for idx_cat,cat in enumerate(fruits): 43 year_furuits = [cat] + [0]*len(months) 44 for idx_month,month in enumerate(months): 45 print(idx_cat, cat, idx_month, month) 46 year_furuits[idx_month+1] = 100*(y2[month][idx_cat] 47 - y1[month][idx_cat])/y1[month][idx_cat] 48 49 year_status[idx_cat] = tuple(year_furuits) 50 51 return tuple(year_status) 52 53year_data = year_change(index['2017'],index['2018']) 54 55#for a single tuple 56res2 = () 57for rows in year_data: 58 res2 = res2 + rows 59 60 61month_index = month_data.pop(-1) 62year_index = year_data.pop(-1) 63 64def sentence(idx_data, factor_data): 65 if idx_data[-1] > 0: 66 contributor = [] 67 discounter = [] 68 for factor in factor_data: 69 if factor[-1] > 0: 70 contributor.append(factor) 71 else: 72 discounter.append(factor) 73 74 contributor.sort(key=lambda data:data[-1]-data[-2], reverse=True) 75 discounter.sort(key=lambda data:data[-1]-data[-2]) 76 contributor_fragments = [] 77 78 contributor_fragments = [] 79 for idx, contrib in enumerate(contributor): 80 prev_time = '' 81 if idx == 0: 82 prev_time = "2月に" 83 sentence = "{} ({:.1f} % から {:.1f} %{})".format(contrib[0].lower(), contrib[-1], contrib[-2], prev_time) 84 contributor_fragments.append(sentence) 85 86 print(contributor_fragments) 87 contributor_sentence = '; '.join(contributor_fragments[:-1]) + 'と' + contributor_fragments[-1] + '.' 88 89 discounter_fragments = [] 90 # the completion is left as an exercise 91 for idx, contrib in enumerate(contributers): 92 prev_time = "%)" 93 senetence 94 95 sentence1 = "上昇が見られたのは、" + contributor_sentence 96 if len(discounter_fragments): 97 sentence1 = sentence1 + "一方、" + discounter_sentence 98 return sentence1 99 else: 100 # idx_data[-1] <= 0 に関して明記しなくてはならないか 101 return '' 102 103sentence1 = sentence(year_index, year_data) 104
###試したこと
タプル型ではなく、リスト型に変更し、「sentence1」を出力したところ、
>>> sentence1 ''
と出力され、文章の出力には至りませんでした。
コード2
python
1fruits = [ #foodptice category 2 "かき", 3 "りんご", 4 "みかん" 5] 6 7index = { 8 "2017": { 9 "1月": [901, 997, 1003], 10 "2月": [918, 984, 1005], 11 "3月": [891, 985, 1007] 12 }, 13 "2018": { 14 "1月": [885, 1006, 1010], 15 "2月": [877, 975, 1006], 16 "3月": [928, 999, 1006] 17 } 18} 19 20def month_change(m1, m2, m3): 21 22 month_status = [0]*len(fruits) 23 for idx_cat,cat in enumerate(fruits): 24 print(idx_cat,cat) 25 month_status[idx_cat] = (cat, 100*(m2[idx_cat] - m1[idx_cat])/m1[idx_cat], 26 100*(m3[idx_cat] - m2[idx_cat])/m2[idx_cat]) 27 28 return month_status 29 30month_data = month_change(index['2018']['1月'], 31 index['2018']['2月'], 32 index['2018']['3月']) 33 34def year_change(y1, y2): 35 months = ['1月', '2月', '3月'] 36 year_status = [0]*len(fruits) 37 for idx_cat,cat in enumerate(fruits): 38 year_furuits = [cat] + [0]*len(months) 39 for idx_month,month in enumerate(months): 40 print(idx_cat, cat, idx_month, month) 41 year_furuits[idx_month+1] = 100*(y2[month][idx_cat] 42 - y1[month][idx_cat])/y1[month][idx_cat] 43 44 year_status[idx_cat] = tuple(year_furuits) 45 46 return year_status 47 48year_data = year_change(index['2017'],index['2018']) 49 50 51month_index = month_data.pop(-1) 52year_index = year_data.pop(-1) 53 54def sentence(idx_data, factor_data): 55 if idx_data[-1] > 0: 56 contributor = [] 57 discounter = [] 58 for factor in factor_data: 59 if factor[-1] > 0: 60 contributor.append(factor) 61 else: 62 discounter.append(factor) 63 64 contributor.sort(key=lambda data:data[-1]-data[-2], reverse=True) 65 discounter.sort(key=lambda data:data[-1]-data[-2]) 66 contributor_fragments = [] 67 68 contributor_fragments = [] 69 for idx, contrib in enumerate(contributor): 70 prev_time = '' 71 if idx == 0: 72 prev_time = "2月に" 73 sentence = "{} ({:.1f} % から {:.1f} %{})".format(contrib[0].lower(), contrib[-1], contrib[-2], prev_time) 74 contributor_fragments.append(sentence) 75 76 print(contributor_fragments) 77 contributor_sentence = '; '.join(contributor_fragments[:-1]) + 'と' + contributor_fragments[-1] + '.' 78 79 discounter_fragments = [] 80 # the completion is left as an exercise 81 for idx, contrib in enumerate(contributers): 82 prev_time = "%)" 83 senetence 84 85 sentence1 = "上昇が見られたのは、" + contributor_sentence 86 if len(discounter_fragments): 87 sentence1 = sentence1 + "一方、" + discounter_sentence 88 return sentence1 89 else: 90 # idx_data[-1] <= 0 について何か明記しなくてはならない? 91 return '' 92 93sentence1 = sentence(year_index, year_data)
補足情報(FW/ツールのバージョンなど)
Python 3.6.3
回答1件
あなたの回答
tips
プレビュー