第1回の練習問題の解答例¶
問題1
12 + 22 - (3 × 7) ÷ 2
12 + 22 - (3 * 7) / 2
23.5
問題2
sent = ["Wars","not","make","one","great"]を文字列に変換して出力する。
sent = ["Wars","not","make","one","great"]
" ".join(sent)
'Wars not make one great'
問題3
問題2で文字列に変換したものをリストに変換する。
sent2 = " ".join(sent)
sent2.split()
['Wars', 'not', 'make', 'one', 'great']
問題4
a = "Use the force."とb="Feel it."を結合して"Use the force. Feel it."にする。
a = "Use the force."
b="Feel it."
a + " " + b
'Use the force. Feel it.'
問題5
"You don't know the power of the darkside."の語数を出力する。
sent3 = "You don't know the power of the darkside."
len(sent3.split())
8
問題6
"But you can't stop the change, any more than you can stop the suns from setting."の単語の種類数を出力する。
sent4 = "But you can't stop the change, any more than you can stop the suns from setting."
L = sent4.split()
M = set(L)
len(M)
13
問題7
以下のデータの平均値(データの総和を個数で割る)を求めましょう。
L = [12,25,32,65,44,89,35]
L = [12,25,32,65,44,89,35]
sum(L)/len(L)
43.142857142857146
問題8
以下のリストの"C"を出力しなさい。
M = [["A","B",["C","D"],"E"],"F",["G","H"],"I"]
M = [["A","B",["C","D"],"E"],"F",["G","H"],"I"]
M[0][2][0]
'C'
問題9
以下のリストのHを出力しなさい。
M = [["A","B",["C","D"],"E"],"F",["G","H"],"I"]
M = [["A","B",["C","D"],"E"],"F",["G","H"],"I"]
M[2][1]
'H'
問題10
以下の文、XとYのJaccard係数を求めなさい。
X = "The mischievous dog and curious cat had a playful chase on the soft carpet, leaving tufts of fur flying in their wake. As the cat leapt onto the windowsill, the dog skidded to a stop, lapping up the spilled milk from a knocked-over glass, adding to the chaos of their impromptu game."
Y = "The cat, with its tail fluffed up in excitement, eagerly lapped at the bowl of milk on the carpet while the dog watched on, wagging its tail in anticipation, hoping for a share of the treat."
X = "The mischievous dog and curious cat had a playful chase on the soft carpet, leaving tufts of fur flying in their wake. As the cat leapt onto the windowsill, the dog skidded to a stop, lapping up the spilled milk from a knocked-over glass, adding to the chaos of their impromptu game."
Y = "The cat, with its tail fluffed up in excitement, eagerly lapped at the bowl of milk on the carpet while the dog watched on, wagging its tail in anticipation, hoping for a share of the treat."
X_split = set(X.split(" "))
Y_split = set(Y.split(" "))
inter_set = X_split.intersection(Y_split)
union_set = X_split.union(Y)
len(inter_set) / len(union_set)
0.13846153846153847