• 締切済み

pythonコードについて

以下のpythonコードに関して質問があります。なぜ一度Falseになったatsplitがforループ内で再びTrueになるのでしょうか?以下にコードと結果を掲載しております。 def split_string(source,splitlist): output = [] atsplit = True for char in source:    if char in splitlist:      atsplit = True      print atsplit    else:       if atsplit:          output.append(char)          atsplit = False          print atsplit       else:          output[-1] = char return output out = split_string("This is a test-of the,string separation-code!"," ,!-") print out #>>> ['This', 'is', 'a', 'test', 'of', 'the', 'string', 'separation', 'code'] False True False True False True False True False True False True False True False True False True ['This', 'is', 'a', 'test', 'of', 'the', 'string', 'separation', 'code']tsplit = True

関連するQ&A

  • pythonの問題ー解き方を教えて頂きたいです。

    プログラムのコードとどう考えればいいのかを教えていただきたいです。 言語はpythonです。 よろしくお願いします。 Write a function called genAvg(numList, precision) that takes in a list numbers and finds the average of the numbers. It will then return a string that says something like: The average is 1.234 The output format of the average is specified by the precision parameter that is given to the function. The parameter defines how many digits to the right of the decimal place to round to. So, if the precision is 2, then the output format will round to the hundredths. This parameter will be an integer greater or equal to 0. Use the .format method of strings to make this output string. You may need to create an initial string with some string concatenation operations before using the .format method since the precision is not a fixed value. Example: genAvg([1,2,5], 4) will return the string The average is 2.6667

  • コード(Python)の解説をして頂きたいです

    このコードはスペースが2つ以上ある場合スペースを一つだけにする、そして文の最初と最後にスペースがあった場合そのスペースを消すというコードだということは理解出来たのですが、flgはなんの略なのかということと、ans = ans[1:] if ans[0] == ' ' else ans ans = ans[:-1] if ans[-1] == ' ' else ans このコードのelse ansはどのような働きをしているのか分かりません。 もし宜しければ解説をして頂きたいです。 def removeExtraSpaces(theString): ans = '' flg = True for s in theString: if s == ' ': if flg: ans += s flg = False continue flg = True ans += s ans = ans[1:] if ans[0] == ' ' else ans ans = ans[:-1] if ans[-1] == ' ' else ans return ans

  • Pythonのコードの解説をお願いします

    このコードはスペースが2つ以上ある場合スペースを一つだけにする、そして文の最初と最後にスペースがあった場合そのスペースを消すというものだとういうことは理解出来たのですが、なんでこういうコードを書いたぬぬかついんのか、 def removeExtraSpaces(theString): ans = '' flg = True for s in theString: if s == ' ': if flg: ans += s flg = False continue flg = True ans += s ans = ans[1:] if ans[0] == ' ' else ans ans = ans[:-1] if ans[-1] == ' ' else ans return ans これを解説して

  • 単体テストのやり方

    単体テストってどうやってするのですか?教えてください。 例えば、この関数だったらどういうテストコードを書けばいいですか? bool isStart(char c){ if(c == 'Y'){ return true; } else if(c == 'N'){ return false; } }

  • python: ストアする値を更新するコード

    pythonコードの質問です 入力は、画像のExcelシートのA列のようになっています 1行目:その後入力される単語の数 2行明以降:テキスト(ここでは野菜名) これに対して次のコードで「判定」をかけました #----------------------- #数値入力(入力される単語数) t = int(input()) flag = 0 store = 0 i = 0 for i in range(t): #野菜名判定 txt = input() #Tomatoの入力を1として変数flagに代入 if txt.count("Tomato") == 1: flag = 1 store = flag print("iは " + str(i) + "-----") print("判定 " + str(flag == 1 & (store - i) > 0)) print("storeは " + str(store)) i += 1 else: flag = 0 print("iは " + str(i) + "-----") print("判定 " + str(flag == 1 & (store - i) > 0)) print("storeは " + str(store)) i += 1 #----------------------- 2番め以降の入力を for i in range(t)で回し、"Tomato"がみつかると、flagに1を立てます そしてflagの値を、次に"Tomato"が出現するまでstoreに代入して保存 flag == 1 (Tomatoが出現した入力)and (store - i) > 0 となった場合にTrue それ以外はFalse 判定をさせようとしています "Tomato"が入力されても、一定期間はTrue判断にしたくないため、 (store - i) > 0 でジャッジしています ところがi == 7のとき、storeの値が本来storeで8になるべきところが、1のまま変化がおきていないため、本来"判定"がTrueであるべきがFalseとなります store値を上の条件「(store - i) > 0」で更新させたい つまり i が 7のとき、store は 8 ですが、コードのどこが間違っているのでしょうか 他のジャッジ方法でもかまいません

  • 演算子is... 初学者 Python 3

    毎度おせわさまです。Pythonに関する質問です。 今回は演算子isについて。 ネット上の初心者むけサイトで独学に励んでいるのですが、演算子isは、左辺と右辺のオブジェクトが同じオブジェクトだった場合には「True」を返します、とありました。 変数を、 str1 = "555" str2 = "555" として、 print str1 == str21 #1 print str1 is str2 #2 とすると #1は、オブジェクトの値が同じなのでTrueを返すが、#2はオブジェクトが同一ではないのでFalseを返す、と。 なるほど、なるほど。と思いながら実際に書いてみると、#1も#2もTrueを返すのですが・・・・・ バージョンの違いでしょうか?なんでしょうか???今使ているのはPython2.7です。 ご回答よろしくお願いいたします。

  • 決まった文字を探すloop

    こんにちは、いつもお世話になっています。 入力した文章の中にEが入っていなければEを無視した文章である。 同じ文章でSが入っていなければSを無視した文章である。 と表示するプログラムなのですが、このプログラムを実行するとランタイムエラーになります これはループがおかしいのでしょうか。。それともIF文がおかしいのでしょうか。 こんな初歩的な質問ですいません。 boolean lipogramE = true; boolean lipogramS = true; Scanner sc2 =new Scanner(System.in); System.out.println("Enter a sentence "); String input2 = sc2.nextLine(); for(int i =0; i<input2.length();i++){ char e =input.charAt(i); if (e == 'e' || e=='E'){ lipogramE = false; } else if(e =='s'|| e== 'S'){ lipogramS =false; } } if (lipogramE == false){ System.out.println("This input is not a lipogram avoiding E"); }else{ System.out.println("This input is a lipogram avoiding E"); } if(lipogramS == false){ System.out.println("This input is not a lipogram avoiding S"); }else{ System.out.println("This input is a lipogram avoiding S"); } エラーメッセージは Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2 at java.lang.String.charAt(Unknown Source) at WordPlay.main(WordPlay.java:69) です。

    • ベストアンサー
    • Java
  • PHPでの変数の扱い方

    PHPで(1)、(2)共にtrueになります。 (1)、(2)についてどうしてこうなるのか簡単に解説してもらえませんか。 また(1)が正しく動作するようにするにはどう修正すればいいですか。 (1) $value=0; if($value == '-'){ print("true"); }else{ print("false"); } (2) $value='-'; if($value == 0){ print("true"); }else{ print("false"); }

    • ベストアンサー
    • PHP
  • 文字コードチェックについて

    文字コードのチェックについて質問です。 検索した所、下記の質問で同じような質問がでていたので参考に作ってみたのですが java.lang.ArrayIndexOutOfBoundsException になってしまい動作しません。 charに変換するときに2byte使う条件が違っているのだと思うのですがよくわかりませんでした。 http://okwave.jp/qa1754723.html ↑参考にした質問 入力された文字に対象となる文字コードが含まれているかをUnicodeではなくてSJISのコードで調べたいのですがどうすればよいか教えてくれませんか? うまく動作しなかったのは下記のコードです。 引数で与えられた文字列にSJISの8740~879c、ed40~effc、fa40~fc4b(機種依存文字と外字) が含まれていたらエラーにするようなメソッドです。 --- private boolean checkChar(String target) { byte charArray[] = charArray = target.getBytes("MS932"); for (int i = 0; i < charArray.length; i++) { byte charByte = charArray[i]; char targetChar; if (charByte >= 128) { targetChar = (char) charByte; } else { targetChar = (char) (charByte * 0x100 + charArray[i + 1]); i++; } if (0x8740 <= targetChar && targetChar <= 0x879c) { // エラー処理 return false; } if (0xed40 <= targetChar && targetChar <= 0xeffc) { // エラー処理 return false; } if(0xfa40 <= targetChar && targetChar <= 0xec4b) { // エラー処理 return false; } } return true; } ---

    • ベストアンサー
    • Java
  • 改行コード

    いつもお世話になります。 あるバッファからsprintf()でバッファに格納したデータを ファイルにfprintf()で書き込むと、改行コードが混入します。 char b[5][128]; for( int p=0; p<i;p++ ){ sprintf(b[p]"TEST_CODE,%s,NAME\n", data[p]); fprintf(fp, "%s", b[p]); } ファイルには、 TEST_CODE,123 ,NAME TEST_CODE,456 ,NAME と%sの後に改行コードが入ります。 TEST_CODE,123,NAME TEST_CODE,456,NAME のようにしたいです。 何か方法はありますでしょうか? 環境がLinux環境のため、sedコマンドで行う方法も 考えましたが、コマンド自体イマイチわかりません。 関数またはコマンドでの方法がありましたら よろしくお願い致します。

専門家に質問してみよう