質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

1回答

1263閲覧

pythonの配列検索、エラー修正

FLY2525

総合スコア12

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2017/05/21 11:48

test6sample.txtからランダムに4組の文章をとってきて
1つ目の文にあり、かつ2つ目の文にも存在するフレーズを出力し次のように表現したいコードを書きたいのですが、59行目でたまにエラーを吐きます。str1[word_line+b+1]が存在しませんと出るのですが58行目でエラーを回避してるつもりができていません。

やりたいこと
2つの文章がこうだとする
1つ目
abcxxxacxxx
2つ目
zzzabczzzzz
理想の出力
[[abc,xxx,a,c,xxx][1,0,1,1,0]]

こんな感じに出力したいのですが、文字列を取得したときに改行も含んでしまうため、コードを書くのにすごく複雑になってしまいました。

python

1 2# coding: utf-8 3import random 4 5 6for x in range(4): 7 line_num = len(open("test6sample.txt").readlines()) 8 num_1 = 0 9 num_2 = 0 10 while num_1 == num_2: 11 num_1 = random.randint(0,line_num - 1) 12 num_2 = random.randint(0,line_num - 1) 13 #num_1 = 7 14 #num_2 = 8 15 f = open("test6sample.txt", "r") 16 str1 = f.readlines()[num_1] 17 f.close() 18 f = open("test6sample.txt", "r") 19 str2 = f.readlines()[num_2] 20 f.close() 21 22 #単語検出プログラム 23 word_lines = 0 24 word_len = 0 25 word_else = [] 26 print(len(str1), str1) 27 print(len(str2), str2) 28 open("test6-2.txt","a").write("[[") 29 while word_lines < len(str1): 30 word_len = 0 31 #print("point1","wl=",word_lines,"wn=",word_len,str1[word_lines]) 32 for b in range(len(str1)-word_lines): 33 word_len += 1 34 if str1[word_lines:word_lines+b] in str2 and str1[word_lines:word_lines+b] != "": 35 #print("point2","wl=",word_lines,"wn=",word_len,str1[word_lines]) 36 if str1[word_lines:word_lines+b] in str2 and not(str1[word_lines:word_lines+b+1] in str2): 37 #print("point3","wl=",word_lines,"wn=",word_len,str1[word_lines]) 38 open("test6.txt","a").write(str1[word_lines:word_lines+b] + "\n") 39 if str1[word_lines+b] != "\n": 40 #print("point4","wl=",word_lines,"wn=",word_len,str1[word_lines]) 41 open("test6-2.txt","a").write(str1[word_lines:word_lines+b]+",") 42 word_else.append(1) 43 else: 44 #print("point5","wl=",word_lines,"wn=",word_len,str1[word_lines]) 45 open("test6-2.txt","a").write(str1[word_lines:word_lines+b]) 46 word_else.append(1) 47 #print("出力1: ",str1[word_lines:word_lines+word_len]) 48 word_lines = word_lines + b - 1 49 elif str1[word_lines+b] == "\n": 50 open("test6-2.txt","a").write(str1[word_lines:word_lines+b]) 51 word_lines = word_lines + b - 1 52 word_else.append(1) 53 else: 54 #print("point6","wl=",word_lines,"wn=",word_len,str1[word_lines]) 55 if str1[word_lines:word_lines+b] in str2 and not(str1[word_lines:word_lines+b+1] in str2) and str1[word_lines+b] != "\n": 56 #print("point7","wl=",word_lines,"wn=",word_len,str1[word_lines]) 57 open("test6-2.txt","a").write(str1[word_lines+b]) 58 #print("出力2: ",str1[word_lines+word_len]) 59 if str1[word_lines+b] != "\n": 60 if str1[word_lines+b+1] in str2 and str1[word_lines+b] != "\n": 61 #print("point8","wl=",word_lines,"wn=",word_len,str1[word_lines]) 62 if str1[word_lines+b+1] != "\n": 63 #print("point9","wl=",word_lines,"wn=",word_len,str1[word_lines]) 64 open("test6-2.txt","a").write("@") 65 word_else.append(0) 66 else: 67 #print("point0","wl=",word_lines,"wn=",word_len,str1[word_lines]) 68 word_else.append(0) 69 word_lines += 1 70 open("test6-2.txt","a").write("]") 71 open("test6-2.txt","a").write("," + str(word_else)) 72 open("test6-2.txt","a").write("]\n") 73open("test6-2.txt","a").write("\n") 74 75 76 77 78 79 80 81 82

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

こんな感じに出力したいのですが、文字列を取得したときに改行も含んでしまうため、コードを書くのにすごく複雑になってしまいました。

  • 文字列から改行コードを削除する用途にはstr.rstripが使えます。

Python

1with open("test6sample.txt", "r") as f: 2 str1 = f.readlines()[num_1].rstrip('\r\n') 3with open("test6sample.txt", "r") as f: 4 str2 = f.readlines()[num_2].rstrip('\r\n')
  • test6sample.txtの内容が1行だけの時に、

Python

1while num_1 == num_2: 2 num_1 = random.randint(0,line_num - 1) 3 num_2 = random.randint(0,line_num - 1)

random.randint(0, line_num - 1)がどちらも0を返すため、while num_1 == num_2:の条件にて無限ループになります。

  • while word_lines < len(str1):のループ内の処理が長いので、関数化/クラス化してみてはー。

投稿2017/05/21 20:00

編集2017/05/22 06:00
umyu

総合スコア5846

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問