2017-04-25 3 views
1

私はPythonでMatlab matファイルを読み込んでいます。このファイルには、3つの配列:tom, dick and harryが含まれています。 Pythonでは、この配列リストの操作を行うforループを使用します。私はデバッグに持っており、3つのすべてのvarlistforループのためにアクセスすることを望んでいないことを今Pythonリストの特定の変数へのアクセス

import scipy.io as sio 
mat_contents = sio.loadmat('names.mat') # with arrays tom, dick and harry 
varlist = ['tom', 'dick', 'harry'] 
for w in varlist: 
    cl = mat_contents[w] 
    # some more operations in the loop 

:以下は、デモ・コードです。 harryのforループのみを実行するには?私はが私にharryを手に入れたことを知っていますが、私はループをforのために単独で得ることができませんでした。あなたのコメントを受けて

+0

あなたは意味 '変数リスト[2:3]'? – ewcz

+3

'w ==" harry "の場合:' do for operations ... 'を実行します。それが助けにならない場合は、入力と希望の出力を指定してください、それはより良い質問を理解するのに役立ちます。 – JkShaw

+0

@jyotish:ありがとう。私は 'for 'と' if'を[generator expressions](http://stackoverflow.com/a/6981771/1977614)を使って組み合わせて動作させることができました。私はまだこれが道だと感じています。私は誰かがその質問に対して直接答えを出すことを願っています。 –

答えて

1

:今、単一の変数で制御:

import scipy.io as sio 
mat_contents = sio.loadmat('names.mat') # with arrays tom, dick and harry 
varlist = ['tom', 'dick', 'harry'] 

# set it to -1 to disable it and use all arrays 
debug_index = -1 

# or set it to an index to only use that array 
debug_index = 1 

for w in [varlist[debug_index]] if debug_index + 1 else varlist: 
    cl = mat_contents[w] 
    # some more operations in the loop 
+0

これは目的を果たします。しかし、それは私が探しているものではありません。私は 'tom'のためにデバッグしたいと言います。インデックスを変更し、それぞれの名前を入力しないように簡単にすることはできますか? –

+0

確かに、上記の更新を参照してください。 –

関連する問題