LoginSignup
1
1

More than 3 years have passed since last update.

言語処理100本ノックやってみる 00~04

Last updated at Posted at 2019-08-21

1日目
自然言語処理を今日から初めてやっていく。pythonの知識はほとんど忘れてしまっているので他のサイトを利用しながらコツコツ進めていこうと思う!

00. 文字列の逆順

文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.

自分の解答

A="stressed"
for i in range(len(A)-1,-1,-1): #(開始値,終端値(含まない),ステップ)
    print(A[i],end="")
#end="" によって改行を無くせる!

先人の解答

A = 'stressed'
#[::-1]とすると逆順に並べ替えられたオブジェクトが取得できる
new_A=A[::-1]
print(new_A)
#出力結果
desserts

01. 「パタトクカシーー」

「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.

自分の解答

A="パタトクカシーー"
for i in range(0,7,2):
    print(A[i],end="")

先人の解答

A="パタトクカシーー"
new_A=A[::2]
print(new_A)
#出力結果
パトカー

02. 「パトカー」+「タクシー」=「パタトクカシーー」

「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.

自分の解答

A="パトカー"
B="タクシー"

for i in range(0,max(len(A),len(B))):#max関数で文字数が大きいほうに終端を合わせた
    print(A[i]+B[i],end="")

先人の解答

st1="パトカー"
st2="タクシー"
st3=""
for a,b in zip(st1,st2):
  st3=st3+a+b
print(st3)
#出力結果
パタトクカシーー

03. 円周率

"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.

自分の解答(,を取り除く方法が分からなかった)

A="Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
A_list=A.replace(",","")#写譜
A_list=A_list.replace(".","")#写譜
A_list=A_list.split() #()に何も書かないと、空白を基準に文字列を分けてくれる
#同時にリスト作成も行ってくれる!

B_list=[]#リスト作成

for i in range(0,len(A_list)):
    B_list.append(len(A_list[i]))

print(B_list)

先人の解答

st="Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
st=st.replace(",","")
st=st.replace(".","")
st=st.split()

list = []

for word in st:
    list.append(len(word))

print (list)
#出力結果
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]

新たに知ったこと

replace関数を用いることで、不要な文字を削除、または変換できる!
~使い方~
置換対象の文字列 . replace("既存の(不要)文字","新たに変換したい新規の文字")

04. 元素記号

"Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."という文を単語に分解し,1, 5, 6, 7, 8, 9, 15, 16, 19番目の単語は先頭の1文字,それ以外の単語は先頭に2文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.

自分の解答(1文字目、2文字目だけを取り出す方法が分からなかった)

num=[1, 5, 6, 7, 8, 9, 15, 16, 19] #配列のインデックス番号に合わせるために1ずつ減らした
for i in range(0,len(num)):
    num[i]=(num[i]-1)

A="Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
A=A.replace(".","")
A_list=A.split()
B={}

for i in range(0,len(A_list)):
    j=i
    if j in num:
        B[(A_list[j])[0:1]]=j+1 #写譜
    else :
        B[(A_list[j])[0:2]]=j+1 #写譜

print(B)

先人の解答

num_first_only = (1, 5, 6, 7, 8, 9, 15, 16, 19)
target = 'Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.'
result = {}

words = target.split(' ')
for (num, word) in enumerate(words, 1):
    if num in num_first_only:
        result[word[0:1]] = num
    else:
        result[word[0:2]] = num

print(result)
#出力結果
{'H': 1, 'He': 2, 'Li': 3, 'Be': 4, 'B': 5, 'C': 6, 'N': 7, 'O': 8, 'F': 9, 'Ne': 10, 'Na': 11, 'Mi': 12, 'Al': 13, 'Si': 14, 'P': 15, 'S': 16, 'Cl': 17, 'Ar': 18, 'K': 19, 'Ca': 20}

新たに知ったこと

①辞書に要素を追加する方法

a={"チョコ":20, "マカロン":15}の状態で
a["キャンディー]=50と入力すると、
a={"チョコ":20, "マカロン":15,"キャンディー":50}が作成できる!

②部分文字列取得方法

s="ほっかいどう"の状態で
s1=s[0:4]とインデックスを指定することで
s1="ほっかい"が作成できる

③値の有無を判定する方法

list1=["Ace","King","Queen"]の状態で
chk="Ace" in list1inを使うことで
True or False を得られる!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1