質問編集履歴

1

ついき

2018/05/09 06:01

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,173 @@
1
+ 追記
2
+
3
+ ```ここに言語を入力
4
+
5
+ # DICT
6
+
7
+ fruit = {
8
+
9
+ "apple": {
10
+
11
+ "color": "red",
12
+
13
+ "size": "small",
14
+
15
+ "price": "100"
16
+
17
+ },
18
+
19
+ "orange": {
20
+
21
+ "color": "orange",
22
+
23
+ "size": "middle",
24
+
25
+ "price": "200"
26
+
27
+ },
28
+
29
+ "grape": {
30
+
31
+ "color": "purple",
32
+
33
+ "size": "big",
34
+
35
+ "price": "300"
36
+
37
+ },
38
+
39
+ }
40
+
41
+
42
+
43
+ ##################################################################
44
+
45
+ # 1:appleのpriceだけを取り出し変数に格納する時どのように描くか?
46
+
47
+ price_of_delicious_apple = fruit["apple"]["price"]
48
+
49
+ print("price_of_delicious_apple:", price_of_delicious_apple)
50
+
51
+
52
+
53
+ ##################################################################
54
+
55
+ # 2:apple、orange、grape全てのpriceを取り出す時どのように描くか?
56
+
57
+ all_three_prices = []
58
+
59
+ for name, properties in fruit.items():
60
+
61
+ all_three_prices.append(properties["price"])
62
+
63
+ print("all_three_prices", all_three_prices)
64
+
65
+
66
+
67
+ # 別の書き方 リスト内包表記
68
+
69
+ all_three_prices = []
70
+
71
+ all_three_prices = [properties["price"] for name, properties in fruit.items()]
72
+
73
+ print("all_three_prices", all_three_prices)
74
+
75
+
76
+
77
+ ##################################################################
78
+
79
+ # 3:appleの配下color、size、price変数に格納する時どのように描くか?
80
+
81
+ apple = fruit["apple"]
82
+
83
+ color, size, price = apple["color"], apple["size"], apple["price"]
84
+
85
+ print(color,size,price)
86
+
87
+
88
+
89
+ apple = fruit["apple"]
90
+
91
+ print("apple", apple)
92
+
93
+
94
+
95
+ # 取り出したappleの要素であるprice値を取り出す
96
+
97
+ apple_price = apple["price"]
98
+
99
+ print("apple_price", apple_price)
100
+
101
+
102
+
103
+ ##################################################################
104
+
105
+ # 4:もしappleだったらpriceを200、もしorangeだったらprice500、もしgrapeだったら800に変更する
106
+
107
+ target = "apple_or_orange_ora_grape"
108
+
109
+ if target == "apple":
110
+
111
+ fruit[target]["price"] = "200"
112
+
113
+ elif target == "orange":
114
+
115
+ fruit[target]["price"] = "500"
116
+
117
+ elif target == "orange":
118
+
119
+ fruit[target]["price"] = "500"
120
+
121
+
122
+
123
+ # nameの文字列をチェックすればok
124
+
125
+ for name, properties in fruit.items():
126
+
127
+ if name == "apple":
128
+
129
+ properties["price"] = 200
130
+
131
+ elif target == "orange":
132
+
133
+ properties["price"] = 500
134
+
135
+ elif target == "orange":
136
+
137
+ properties["price"] = 800
138
+
139
+ print("price",name)
140
+
141
+
142
+
143
+ ##################################################################
144
+
145
+ # 5:もしappleのcolorがredでありかつ、grapeのcolorのpurpleが同じだったら、print("xxx")実施する
146
+
147
+ if (fruit["apple"]["color"] == "red") and (fruit["grape"]["color"] == "purple"):
148
+
149
+ print("xxx")
150
+
151
+
152
+
153
+ ##################################################################
154
+
155
+ # 6:もしappleのsizeに”sma”が含まれていて、grapeのsizeに"bi"が含まれていれば、print("xxx")実施する
156
+
157
+ if ("sma" in fruit["apple"]["size"]) and ("bi" in fruit["grape"]["size"]):
158
+
159
+ print("xxx")
160
+
161
+
162
+
163
+ ```
164
+
165
+
166
+
167
+
168
+
169
+
170
+
1
171
  お世話になります。
2
172
 
3
173